0

我正在编写一个 powershell 脚本,通过生成的 MSI 将 .NET 4 Windows 服务部署到 2008 服务器。全新安装运行良好,但是当我重新运行并尝试卸载它时,脚本在尝试卸载时挂起。我调用 msiexec,它在目标机器上运行(我可以看到卸载运行时启动的进程)。卸载和安装代码之间的唯一区别是日志名称和传递给 msiexec 的 /x 命令。

这是我拥有的代码:

function UninstallService ($serverName, $fileName)
{
    write "Start uninstall service."

    $msiNamePath = "C:\MsiDeployment\" + $fileName
    $processArgs = @("/i", $msiNamePath, "/x", "/qn", "/norestart", "/l", "c:\msiuninstall.log")

    # Create session
    $session = New-PSSession -ComputerName $serverName

    # Enter session
    Enter-PSSession $session

    # Do uninstall
    Invoke-Command -Session $session -ScriptBlock { param($pArgs,$rootDir) Start-Process -FilePath "$rootDir\msiexec.exe" -ArgumentList $pArgs -Wait } -Args $processArgs,("$env:systemroot\system32")

    # Close session    
    Exit-PSSession
    Remove-PSSession $session

    if (!$?) { throw "Could not uninstall the service remotely on machine " + $serverName }

    write "End uninstall service."
}

如果我终止服务器上正在运行的 msiexec,脚本会继续处理(由于检查服务是否已卸载,稍后会失败)。我猜有一些提示正在寻找用户输入(可能是 UAC),但我并不完全确定。我在卸载时没有得到日志文件,但安装会写入日志文件。

4

2 回答 2

0

Enter-PSSession仅用于交互式使用,而不是在脚本中。因此,基本上,您的脚本在Enter-PSSession $session.

从您的脚本中删除以下行,一切都应该按预期工作。

 # Enter session
    Enter-PSSession $session

  # Close session    
    Exit-PSSession

您需要的只是Invoke-Command.

于 2013-06-15T05:47:26.257 回答
0

其实,我想通了这个问题。我在参数中留下了 /i 标志,而它应该只是 /x 标志。现在工作正常。

该标志是 msiexec 抛出错误页面,即使将 qn 标志传递给它也是如此。不确定它是否应该这样做。

于 2013-06-17T14:07:24.420 回答