如何防止PowerShell窗口关闭,以便查看错误?
我正在创建本地PowerShell模块下载程序脚本。模块和脚本保存在网络共享上。使用以下方法调用脚本:如何防止PowerShell窗口关闭,以便查看错误?,powershell,Powershell,我正在创建本地PowerShell模块下载程序脚本。模块和脚本保存在网络共享上。使用以下方法调用脚本: & '\\net\DSFShare\Shared Data\Powershell Modules\Install-MyModuleManager.ps1' 它将脚本复制到标准配置文件模块文件夹,然后从模块文件夹运行Install.ps1安装。ps1可根据需要提升自身。在提升窗口关闭之前,会弹出一个红色错误,但是窗口关闭得太快,我看不到错误。我怎样才能找出错误是什么 downloade
& '\\net\DSFShare\Shared Data\Powershell Modules\Install-MyModuleManager.ps1'
它将脚本复制到标准配置文件模块文件夹,然后从模块文件夹运行Install.ps1<代码>安装。ps1可根据需要提升自身。在提升窗口关闭之前,会弹出一个红色错误,但是窗口关闭得太快,我看不到错误。我怎样才能找出错误是什么
downloader脚本使用以下命令调用安装程序:
$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
}
注意,如果从PowerShell中填充$installerPath
,并使用和$installerPath
调用它,我看不到错误
我已经检查了应用程序、系统、Windows PowerShell和安全事件日志。没有任何与此相关的错误
脚本所做的只是创建一个事件源。如果要运行它,可以使用:
Remove-EventLog -Source 'My.Module.Manager'
之后,将其移除。以下是脚本:
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..."
}
我使用的是PowerShell 4.0。您基本上有三个选项来防止PowerShell控制台窗口关闭,这就是我所描述的
PowerShell-NoExit“C:\SomeFolder\SomeScript.ps1”
读取主机-提示“按Enter键退出”
在.ps1文件上单击鼠标右键,然后选择“使用打开” 注册表项:
HKEY\U CLASSES\U ROOT\Applications\powershell.exe\shell\open\command
默认值:
“C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe”“%1”
所需值:
“C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe”&\%1”
● 使用PowerShell运行右键单击.ps1文件并选择运行PowerShell(根据安装的Windows操作系统和更新显示) 注册表项:
HKEY\U CLASSES\U ROOT\Microsoft.PowerShellScript.1\Shell\0\Command
默认值:
“C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe”“-Command”“如果((Get ExecutionPolicy)-ne'AllSigned'){Set ExecutionPolicy-Scope Process Bypass};&“%1”
所需值:
“C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe”-NoExit”-Command”“if((Get-ExecutionPolicy)-ne'AllSigned'){Set-ExecutionPolicy-Scope Process Bypass};&\%1”
try
{
# Do your script's stuff
}
catch
{
Write-Error $_.Exception.ToString()
Read-Host -Prompt "The above error occurred. Press Enter to exit."
}
也很简单:
开始睡眠10
这将使PowerShell窗口等待,直到您按下Enter键(而不是任何键):
最简单、最简单的方法是使用
-NoExit
param执行特定脚本
1.按以下键打开运行箱:
赢+R
2.然后在输入提示中键入:
PowerShell -NoExit "C:\folder\script.ps1"
然后执行。启动提升的PowerShell提示符并从那里运行脚本,这样脚本退出时窗口不会关闭。谢谢,但是如果我从提升的提示符运行下载程序脚本(&'\\net\DSFShare\Shared Data\PowerShell Modules\Install MyModuleManager.ps1'),我看不到错误。Install.ps1正确地检测到它已经在运行,并继续创建事件源,这是它应该做的。没有出现错误。这是否回答了您的问题?这是一个很好的答案,在自动关闭前等待10秒,这通常已经足够好了。对我来说,“往往足够好”正是原因所在。我之所以使用start sleep,只是为了看看有什么地方出了问题。当我运行自动任务时,我不想按下按钮。耽搁一下就够了。另外,类似的回答是:在使用
暂停
时,似乎需要按回车
继续。其他的钥匙似乎不起作用。两者都不是;-)@斯坦·奥斯梅尔:是的。接得好!谢谢你。我会更新它。我总是使用一个变通方法,cmd/c pause
,它不会响应任何一个键。我从你的博客下载了reg文件,但它不起作用(在Windows 10 x64上):ps1窗口在出现错误时退出。重新打开→ Windows PowerShell
:建议的更改在保持窗口打开方面似乎没有任何作用(尽管它有助于使用嵌入空格的脚本路径,但更好的修复方法是使用-file
)。
PowerShell -NoExit "C:\folder\script.ps1"