Running remote scripts in powershell

社会主义新天地 提交于 2020-01-24 20:04:28

问题


I've successfully got winrm working and I'm able to run Enter-PSSession my-machine in the shell and subsequently enter commands. However, when I try to run a script that starts up a remote session, all subsequent calls are run on the local machine. For instance:

PS> test.ps1

Contents of test.ps1

Enter-PSSession remote-pcname
gc env:computername

prints out local-pcname instead of remote-pcname any idea why the script file is not honoring the remote session? It is definitely successfully connecting because when the script finishes I'm returned to the shell prompt of the remote machine.


回答1:


The short answer is: Enter-PSSession is intended for interactive use. If you want to execute commands on a remote system from a script, use invoke-command.

A similiar thread on the Technet forums is here: http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/90e92d4e-716b-4b4d-956f-d38645e5c035




回答2:


For me it work as documented. Enter-PSSession start an interactive session, it's for interactive use.

So to execute a script you can use New-PSSession to create a session and Invoke-Command using the remote session you created with New-PSSession.



来源:https://stackoverflow.com/questions/6119121/running-remote-scripts-in-powershell

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