如何防止 PowerShell 窗口关闭以便我可以看到错误? [英] How can prevent a PowerShell window from closing so I can see the error?

查看:36
本文介绍了如何防止 PowerShell 窗口关闭以便我可以看到错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个本地 PowerShell 模块下载程序脚本.模块和脚本保存在网络共享上.该脚本使用以下方式调用:

I'm creating a local PowerShell module downloader script. The module and the script are held on a network share. The script is invoke using:

& '\\net\DSFShare\Shared Data\Powershell Modules\Install-MyModuleManager.ps1'

它将脚本复制到标准配置文件模块文件夹,然后从模块文件夹运行 Install.ps1.如果需要,Install.ps1 会提升自身.就在提升的窗口关闭之前,弹出一个红色错误,但窗口关闭得太快,我看不到错误.我怎样才能找出错误是什么?

It copies the script to the standard profile modules folder and then runs Install.ps1 from the module folder. Install.ps1 elevates itself, if needed. Just before the elevated window closes, a red error pops up, but the window closes too quickly for me to see the error. How can I find out what the error is?

下载程序脚本使用以下命令调用安装程序:

The downloader script invokes the installer using:

$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,我没有看到错误.

Note, if from PowerShell, I populate $installerPath and call it using & $installerPath, I don't see the error.

我检查了应用程序、系统、Windows PowerShell 和安全事件日志.没有与此相关的任何错误.

I've checked the Application, System, Windows PowerShell, and Security event logs. There aren't any errors relating to this.

脚本所做的就是创建一个事件源.如果你想运行它,你可以使用:

All the script does is create an event source. If you want to run it, you can use:

Remove-EventLog -Source 'My.Module.Manager'

之后,删除它.这是脚本:

afterwards, to remove it. Here's the script:

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.

I'm using PowerShell 4.0.

推荐答案

你基本上有三个选项来防止 PowerShell 控制台窗口关闭,我描述了 在我的博文中有更详细的介绍.

You basically have three options to prevent the PowerShell Console window from closing, that I describe in more detail in my blog post.

  1. 一次性修复:从 PowerShell 控制台运行脚本,或使用 -NoExit 开关启动 PowerShell 进程.例如,PowerShell -NoExit "C:\SomeFolder\SomeScript.ps1"

  1. One-time Fix: Run your script from the PowerShell Console, or launch the PowerShell process using the -NoExit switch. E.g., PowerShell -NoExit "C:\SomeFolder\SomeScript.ps1"

按脚本修复:在脚本文件的末尾添加输入提示.例如,Read-Host -PromptPress Enter to exit"

Per-script Fix: Add a prompt for input to the end of your script file. E.g., Read-Host -Prompt "Press Enter to exit"

全局修复:更改您的注册表项以在脚本完成运行后始终保持 PowerShell 控制台窗口打开.这是需要更改的两个注册表项:

Global Fix: Change your registry key to always leave the PowerShell Console window open after the script finishes running. Here's the two registry keys that would need to be changed:

● 打开方式 → Windows PowerShell
当您右键单击 .ps1 文件并选择打开方式

● Open With → Windows PowerShell
When you right-click a .ps1 file and choose Open With

注册表项: HKEY_CLASSES_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 操作系统和更新显示).

● Run with PowerShell
When you right-click a .ps1 file and choose Run with PowerShell (shows up depending on which Windows OS and Updates you have installed).

注册表项: 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'"

期望值:

"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoExit "-Command" "if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & \"%1\""

如果您不想手动修改注册表项,可以从我的博客下载一个 .reg 文件.

You can download a .reg file from my blog to modify the registry keys for you if you don't want to do it manually.

听起来您可能想要使用选项 #2.您甚至可以将整个脚本包装在一个 try 块中,并且仅在发生错误时提示输入,如下所示:

It sounds like you likely want to use option #2. You could even wrap your whole script in a try block, and only prompt for input if an error occurred, like so:

try
{
    # Do your script's stuff
}
catch
{
    Write-Error $_.Exception.ToString()
    Read-Host -Prompt "The above error occurred. Press Enter to exit."
}

这篇关于如何防止 PowerShell 窗口关闭以便我可以看到错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆