Powershell 调用命令的问题 [英] Issues with Powershell Invoke-Command

查看:87
本文介绍了Powershell 调用命令的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Powershell 在远程服务器上安装应用程序.这是我正在使用的脚本:

I am trying to get an application to install on a remote server using powershell. Here is the script I am using:

$cred = 获取凭据
$s = New-PSSession -ComputerName $ServerName -Credential $cred

$cred = Get-Credential
$s = New-PSSession -ComputerName $ServerName -Credential $cred

Invoke-Command -Session $s -ScriptBlock {Start-Process -FilePath "c:\windows\system32\msiexec.exe" -ArgumentList "/i \\computer\e$\installer.msi/qn" -Wait}

Invoke-Command -Session $s -ScriptBlock {Start-Process -FilePath "c:\windows\system32\msiexec.exe" -ArgumentList "/i \\computer\e$\installer.msi /qn" -Wait}

Remove-PSSession -ComputerName $ServerName

Remove-PSSession -ComputerName $ServerName

如果我直接在远程电脑上运行以下,它执行的很漂亮:

If I run the following on the remote computer directly, it executes beautifully:

Start-Process -FilePath "c:\windows\system32\msiexec.exe" -ArgumentList "/i \\computer\e$\installer.msi/qn" -Wait

Start-Process -FilePath "c:\windows\system32\msiexec.exe" -ArgumentList "/i \\computer\e$\installer.msi /qn" -Wait

但是当我作为 Invoke-Command 的一部分远程运行它时,PS 会话被打开,脚本运行,msiexec 在远程计算机上启动,然后 PS 会话关闭但应用程序永远不会安装并且 msiexec 永远不会关闭.

But when I run it remotely as a part of the Invoke-Command, the PS Session is opened, the script runs, msiexec starts on the remote computer, then the PS Session closes but the application never installs and msiexec never closes.

任何帮助将不胜感激.

谢谢,

扎克

推荐答案

您需要先在本地复制包.一旦开始远程处理,您将无法再进行 UNC.

You'll need to copy the package locally first. Once you start remoting you can no longer UNC.

目的地可以是服务器/计算机上的任何地方.我使用温度,但随你喜欢.
我也喜欢使用 $env:windir\temp,以防万一.

The destination can be anywhere on the server/computer. I use the temp but it's whatever you like.
Also I like to use $env:windir\temp, just in case.

    Copy-item "\\servershare\File.msi" -conatiner -recurse `
               \\$Computer\c$\windows\temp\

    Invoke-Command -Computername $Computer -credential $cred -ScriptBlock {
        Start-Process -FilePath `
        "c:\windows\system32\msiexec.exe" `
        -ArgumentList "/i `
        \\computer\e$\installer.msi /qn" -Wait
        }

希望能帮到你.

这篇关于Powershell 调用命令的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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