enter-pssession invoke-command, when to use?
Asked Answered
C

2

8

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 start services.

Do I need to use enter-pssession?

Updates: Here's what my script needs to do.

on server1, I need to stop and start two services. on server2, I need to stop and start just one service.

# foreach for server 1 since I need to stop and start two services. created a session for server 1
foreach($service in $services){

    $session = New-PSSession -ComputerName $serverName -Credential $cred
    Invoke-Command -Session $session -ScriptBlock {param($service) Stop-Service -Name $service} -ArgumentList $service
    remove-pssession -session $session

}

# created a session for server 2. I need to stop and start just one service in server 2
$session = New-PSSession -ComputerName $serverName -Credential $cred
Invoke-Command -Session $session -ScriptBlock {param($service) Stop-Service -Name $service} -ArgumentList $service
remove-pssession -session $session

is this the right way to do it?

Comeback answered 5/1, 2016 at 19:12 Comment(1)
keeping the (remote) state? not opening and closing a new connection? -- like in: do i have to start a new PS console session (locally) on each command? PS: ...no idea, just guessing. a lamer here playing with powershell for fun, lets see what the gurus have to say :)Amanita
I
12

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}
Indigestion answered 6/1, 2016 at 0:49 Comment(2)
Hi Kiran, thank you for very much for your help. I have a question when running Invoke-Command to stop services on a remote server. Invoke-Command -Session $remoteSession -ScriptBlock {param($service) Stop-Service -Name $service} -ArgumentList $service when I run this I always get [WARNING] waiting for service 'blahblah' to finish stopping... is there a way to not display this warning message? I tried warningaction - silently continue and tried setting warningpreference = 'silentlycontinue' but none of them seems to workComeback
welcome Jae...if you set stop-service bits -warning 'Silentlycontinue' then you should not see any warning messages because this overrides the global setting...i tested this on my machine with powershell V4 and it works as expected so not sure why it isnt working for you...are you using PS V2?Indigestion
R
0

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

Rafaelita answered 5/1, 2016 at 20:24 Comment(4)
thats sorta kinda obvious, aint it? the question seems to be about when to start a remote session first and when just to do a sequence of invoke-commandsAmanita
oops, didnt notice the ENTER-pssession part, please ignore me, sorryAmanita
Thank you Adrian, I just updated my post. Could you take a look? ThanksComeback
enter-pssession doesn't seem to work very wellMensuration

© 2022 - 2024 — McMap. All rights reserved.