Issues with Powershell Invoke-Command

橙三吉。 提交于 2020-08-23 06:56:08

问题


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

$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}

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

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.

Any help would be appreciated.

Thanks,

Zach


回答1:


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

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
        }

I hope it helps.



来源:https://stackoverflow.com/questions/25407353/issues-with-powershell-invoke-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!