0

我正在尝试在远程计算机上运行以下命令来卸载以前版本的产品,然后再安装另一个版本。这是使用 MsiExec.exe 进行卸载。

每当我调用 Start-Process 时,该进程实际上会运行并且产品会在远程计算机上卸载,但我会抛出以下异常。如果该产品尚未安装且 Start-Process 行未运行,则远程命令可以正常工作而不会引发异常。(即它实际上搜索注册表,没有找到产品,并返回 -1 而不抛出异常) 问题仅在调用 Start-Process 时出现。

这是我的脚本代码...

$UninstallScriptBlock = {
    param ( [string]$InstallerProductName )

    $ErrorActionPreference = "Stop";

    $UninstallRegPath = "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
    $ProductCode = Get-ChildItem -Path $UninstallRegPath | foreach { if ($_.GetValue("DisplayName") -eq $InstallerProductName) { [System.IO.Path]::GetFileName($_); } }
    if ([string]::IsNullOrEmpty($ProductCode))
    {
        $UninstallRegPath = "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall";
        $ProductCode = Get-ChildItem -Path $UninstallRegPath | foreach { if ($_.GetValue("DisplayName") -eq $InstallerProductName) { [System.IO.Path]::GetFileName($_); } }
    }
    if ([string]::IsNullOrEmpty($ProductCode))
    {
        return -1;
    }

    $Process = Start-Process -Wait -Passthru -FilePath "MsiExec.exe" -ArgumentList "/X", $ProductCode, "/qn";
    return $Process.ExitCode;
}

[int]$UninstallReturnCode = Invoke-Command -ComputerName $Server -ScriptBlock $UninstallScriptBlock -ArgumentList $InstallerProductName -SessionOption (New-PSSessionOption -OperationTimeout 0);

和抛出的异常......

Processing data for a remote command failed with the following error message: The I/O operation has been aborted because of either a thread exit or an application request. For more information, see the about_Remote_Troubleshooting Help topic.
At [Undisclosed]
+     [int]$UninstallReturnCode = Invoke-Command -ComputerName $Server -ScriptBloc ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : OperationStopped: ([UndisclosedServerName]:String) [], PSRemotingTransportException
+ FullyQualifiedErrorId : JobFailure
+ PSComputerName        : [UndisclosedServerName]

和格式错误...

ErrorCode                   : 995
TransportMessage            : The I/O operation has been aborted because of either a thread exit or an application request.

ErrorRecord                 : Processing data for a remote command failed with the following error message: The I/O
                              operation has been aborted because of either a thread exit or an application request. For
                              more information, see the about_Remote_Troubleshooting Help topic.
StackTrace                  :
WasThrownFromThrowStatement : False
Message                     : Processing data for a remote command failed with the following error message: The I/O
                              operation has been aborted because of either a thread exit or an application request. For
                              more information, see the about_Remote_Troubleshooting Help topic.
Data                        : {}
InnerException              :
TargetSite                  :
HelpLink                    :
Source                      :
4

1 回答 1

1

我能找到的最佳答案是我的卸载正在重置 IIS,这导致我的 Powershell Remoting 连接被切断。

这就是我作为解决方法所做的:

  1. 从 Start-Process 中删除 -Wait 并立即关闭 Powershell Remoting 会话。
  2. Powershell Remoting 会话关闭后,进入 Start-Sleep 等待卸载完成(猜测卸载需要多长时间加上一些填充)。
  3. 阅读卸载日志文件中的文本“删除成功或错误状态:XXXX”。其中 XXXX 是卸载过程的返回码。
于 2013-03-08T14:56:45.010 回答