enter-pssession invoke-command, when to use?

限于喜欢 提交于 2019-11-30 09:58:47

Enter-PSSession - Since this is an interactive session you type what you want at the console and immediately see the results in the console.(just like CMD). If its just 2 servers then you can use enter-pssession but it is always going to be serial meaning you do something on one server then you move onto another.

New-PSSession - creates a persistent connection to a remote server and is generally used when you have a series of commands to run on multiple servers at various stages of a larger script\workflow.

Example:

$s1, $s2 = New-PSSession -ComputerName Server1,Server2
Get-Service -Name Bits                #on localhost
Invoke-Command -session $s1 -scriptblock { # remote commands here }
Get-Process                           #on localhost
Invoke-Command -session $s1 -scriptblock { # remote commands here }
Remove-pSSession -session $s1 #on localhost

if you just want to stop\start a couple of services then you can do this without opening a persistent connection.

Example:

Invoke-Command -ComputerName (Get-Content Machines.txt) -ScriptBlock {Stop-Service -Name Bits}

The Enter-PSSession cmdlet starts an interactive session with a single remote computer.

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