我正在创建一个本地PowerShell模块下载程序脚本.模块和脚本保存在网络共享上.使用以下命令调用脚本:
& '\\net\DSFShare\Shared Data\Powershell Modules\Install-MyModuleManager.ps1'
Run Code Online (Sandbox Code Playgroud)
它将脚本复制到标准配置文件模块文件夹,然后从模块文件夹运行Install.ps1. Install.ps1
如果需要,自我提升.在高架窗口关闭之前,会弹出一个红色错误但窗口关闭太快,我无法看到错误.如何找出错误是什么?
下载程序脚本使用以下命令调用安装程序:
$installerPath = [IO.Path]::Combine($LocalModulePath, 'Install.ps1')
Write-Host "Installer path: $installerPath"
if (Test-Path $installerPath) {
Write-Host 'Install.ps1 exists. Running Install.ps1'
& $installerPath
}
Run Code Online (Sandbox Code Playgroud)
注意,如果来自PowerShell,我填充$installerPath
并使用它调用它& $installerPath
,我没有看到错误.
我已经检查了应用程序,系统,Windows PowerShell和安全事件日志,并且没有任何与此相关的错误.
所有脚本都是创建一个事件源.如果要运行它,可以使用:
Remove-EventLog -Source 'My.Module.Manager'
Run Code Online (Sandbox Code Playgroud)
之后,将其删除.这是脚本:
Write-Host "Installing module..."
$eventSource = 'My.Module.Manager'
try {
$sourceExists = [System.Diagnostics.EventLog]::SourceExists($eventSource)
} catch [Security.SecurityException] {
Write-Verbose "Caught 'SecurityException': $_.Exception.Message"
}
if ($sourceExists) {
Write-Host "...installation complete..."
} else {
#region ----- Ensure-ProcessIsElevated -----
if ($Verbose) {
$VerbosePreference = "Continue"
}
if ($Debug) {
$DebugPreference = "Continue"
}
Write-Debug "Command line is ___$($MyInvocation.Line)___"
Write-Verbose "Entering script body"
if ($ScriptPath) {
Set-Location $ScriptPath
Write-Verbose "Working directory: $pwd"
}
If (-Not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
Write-Warning "This script must be run with elevated privileges."
Write-Warning "Restarting as an elevated process."
Write-Warning "You will be prompted for authorization."
Write-Warning "You may click 'No' and re-run manually, if you prefer."
If ((Get-WmiObject Win32_OperatingSystem | select BuildNumber).BuildNumber -ge 6000) {
Write-Verbose "This is a UAC-enabled system. Elevating ..."
$CommandLine = "$($MyInvocation.Line.Replace($MyInvocation.InvocationName, $MyInvocation.MyCommand.Definition)) -ScriptPath $pwd"
Write-Verbose "CommandLine: $CommandLine"
Start-Process -FilePath PowerShell.exe -Verb Runas -ArgumentList "$CommandLine"
} else {
Write-Verbose "The system does not support UAC: an elevated process cannot be started."
Write-Warning "This script requires administrative privileges. Please re-run with administrative account."
}
Break
}
Write-Verbose "The script is now running with elevated privileges."
#endregion ----- Ensure-ProcessIsElevated -----
New-EventLog -LogName Application -Source $eventSource
Write-Host "...installation complete..."
}
Run Code Online (Sandbox Code Playgroud)
我正在使用PowerShell 4.0,但它没有标签.
dea*_*dog 94
您基本上有3个选项可以阻止PowerShell控制台窗口关闭,我在博客文章中有更详细的描述.
PowerShell -NoExit "C:\SomeFolder\SomeScript.ps1"
Read-Host -Prompt "Press Enter to exit"
全局修复:更改注册表项以在脚本完成运行后始终打开PowerShell控制台窗口.这是需要更改的2个注册表项:
●打开方式→Windows PowerShell
右键单击.ps1文件并选择"打开方式"
注册表项: HKEY_CLASSES_ROOT\Applications\powershell.exe\shell\open\command
默认值:
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "%1"
Run Code Online (Sandbox Code Playgroud)
期望值:
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "& \"%1\""
Run Code Online (Sandbox Code Playgroud)
●使用PowerShell运行
右键单击.ps1文件并选择"使用PowerShell运行"(根据您安装的Windows操作系统和更新显示).
注册表项: HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\0\Command
默认值:
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-Command" "if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & '%1'"
Run Code Online (Sandbox Code Playgroud)
期望值:
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoExit "-Command" "if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & \"%1\""
Run Code Online (Sandbox Code Playgroud)如果您不想手动执行此操作,可以从我的博客下载.reg文件以修改注册表项.
听起来你可能想要使用选项#2.您甚至可以将整个脚本包装在try块中,并且只在发生错误时提示输入,如下所示:
try
{
# Do your script's stuff
}
catch
{
Write-Error $_.Exception.ToString()
Read-Host -Prompt "The above error occurred. Press Enter to exit."
}
Run Code Online (Sandbox Code Playgroud)
Amr*_*haa 24
这将使powershell窗口等到你按任意键:
pause
Run Code Online (Sandbox Code Playgroud)
更新一
感谢Stein.它是Enter键而不是任何键.
小智 14
也简单易行:
Start-Sleep 10
Run Code Online (Sandbox Code Playgroud)
最简单的方法是使用-NoExit
param 执行您的特定脚本。
1.按以下键打开运行框:
赢+R
2.然后在输入提示中输入:
PowerShell -NoExit "C:\folder\script.ps1"
Run Code Online (Sandbox Code Playgroud)
并执行。
归档时间: |
|
查看次数: |
90348 次 |
最近记录: |