enter-pssession invoke-command, when to use?

后端 未结 2 963
猫巷女王i
猫巷女王i 2021-01-01 00:43

I am writing a script to stop and start services in two remote servers. Here\'s my question,

in my script I did new-pssession and used invoke-command to stop and sta

相关标签:
2条回答
  • 2021-01-01 00:54

    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}
    
    0 讨论(0)
  • 2021-01-01 01:05

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

    0 讨论(0)
提交回复
热议问题