Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

enter-pssession invoke-command, when to use?

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?

like image 414
Ninja Avatar asked Jan 05 '16 19:01

Ninja


People also ask

When would you use enter-PSSession instead of invoke-command?

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.

What is enter-PSSession is used for?

The Enter-PSSession cmdlet starts an interactive session with a single remote computer. During the session, the commands that you type run on the remote computer, just as if you were typing directly on the remote computer. You can have only one interactive session at a time.

Why we use invoke-command?

The Invoke-Command cmdlet runs commands on a local or remote computer and returns all output from the commands, including errors. Using a single Invoke-Command command, you can run commands on multiple computers. To run a single command on a remote computer, use the ComputerName parameter.

How do you pass arguments to invoke a command?

To pass the argument in the Invoke-command, you need to use -ArgumentList parameter. For example, we need to get the notepad process information on the remote server.


1 Answers

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}
like image 104
Kiran Avatar answered Oct 05 '22 21:10

Kiran