首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何防止PowerShell窗口关闭,以便我可以看到错误?

如何防止PowerShell窗口关闭,以便我可以看到错误?
EN

Stack Overflow用户
提问于 2014-07-03 06:10:40
回答 4查看 167.2K关注 0票数 65

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

代码语言:javascript
运行
复制
& '\\net\DSFShare\Shared Data\Powershell Modules\Install-MyModuleManager.ps1'

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

下载程序脚本使用以下方法调用安装程序:

代码语言:javascript
运行
复制
$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和安全事件日志。没有任何与此相关的错误。

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

代码语言:javascript
运行
复制
Remove-EventLog -Source 'My.Module.Manager'

然后,移除它。下面是剧本:

代码语言:javascript
运行
复制
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。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-07-07 22:44:33

您基本上有三个选项可以防止PowerShell控制台窗口关闭,这是我对在我的博客文章中有更多的细节的描述。

  1. 一次性修复:从PowerShell控制台运行脚本,或者使用-NoExit开关启动PowerShell进程。例如,PowerShell -NoExit "C:\SomeFolder\SomeScript.ps1"
  2. 按脚本修改:在脚本文件的末尾添加一个输入提示。例如,Read-Host -Prompt "Press Enter to exit"
  3. 全局修复:将注册表项更改为在脚本运行后始终保持PowerShell控制台窗口处于打开状态。以下是两个需要更改的注册表项: 用打开→窗口 右键单击.ps1文件并选择Open 注册表密钥: 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文件并选择Run (显示取决于已安装的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'“ 期望值: "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe“-NoExit "-Command”if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass };&\“%1\”

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

听起来您可能希望使用选项2。您甚至可以将整个脚本包装在一个try块中,并且只有在出现错误时才提示输入,如下所示:

代码语言:javascript
运行
复制
try
{
    # Do your script's stuff
}
catch
{
    Write-Error $_.Exception.ToString()
    Read-Host -Prompt "The above error occurred. Press Enter to exit."
}
票数 137
EN

Stack Overflow用户

发布于 2014-08-18 11:59:15

也很简单和容易:

代码语言:javascript
运行
复制
Start-Sleep 10
票数 16
EN

Stack Overflow用户

发布于 2020-05-15 09:07:46

最简单、最简单的方式是使用-NoExit param执行您的特定脚本。

1.按以下方式打开运行箱:

Win +R

2.然后键入输入提示符:

代码语言:javascript
运行
复制
PowerShell -NoExit "C:\folder\script.ps1"

然后执行。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24546150

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档