Working remotely (Advanced)
There have been multiple modifications to PSRemoting
with the release of Windows PowerShell Version 3.0. To perform remoting activities with PowerShell, we need to execute the Enable-PSRemoting
CMDLET on remote computers.
Getting ready
Enable-PSRemoting
: This CMDLET in fact starts the WinRM service, sets it to the type automatic, creates a firewall exception, and prepares a session environment to perform remote tasks.-SkipNetworkProfileCheck [<SwitchParameter>]
: Server versions of Windows have remote access from the local subnet by default, but if we are working with a client version and the computer is in the public network, we won't have remote access from the local subnet. In such cases, we need to either use theSkipNetworkProfileCheck
parameter, or we need to create a firewall rule manually by using theSet-NetFilewallRule
CMDLET of theNetSecurity
default module.Disable-PSRemoting
: This CMDLET only prevents remote access to the computers. You need to manually stop and disable the WinRM service and also need to disable firewall exceptions for remote communications.
Tip
By default, Windows Server 2012 is enabled with PSRemoting
, but for lower versions of operating systems, use Enable-PSRemoting
.
How to do it...
Execute the following command statement, by performing the step:
The following command statement executes DiskInventory.ps1
scripts on computers specified in servers.txt
, and it maintains the disconnected session named DiskInventory
within the scope of the current console:
PS C:\>Invoke-Command -ComputerName (Get-Content C:\servers.txt) –SessionName DiskInventory –InDisconnectedSession -FilePath \\Scripts\DiskInventory.ps1 -NoNewScope
How it works...
There are a few new parameters introduced with the Invoke-Command
CMDLET.
-EnableNetworkAccess [<SwitchParameter>]
: This parameter supplies a security token to loopback sessions. This token allows you to run commands in a loopback session from a local computer and get data from remote computers. This parameter only works with loopback sessions.Note
A loopback session is a PowerShell session that is created on the local computer. To create a loopback session, use the
–ComputerName
parameter with the.
orlocalhost
value.-InDisconnectedSession [<SwitchParameter>]
: This parameter facilitates us to run a command statement or a script in the disconnected session. With this parameter,Invoke-Command
creates a newPSSession
on remote computers and starts the execution ofScriptBlock
or a script specified with theFilePath
parameter. Then, it disconnects the session, and the execution happens in the disconnected session in the background.-NoNewScope [<SwitchParameter>]
: By default,Invoke-Command
is executed in the command's scope. Using this parameter, we makeInvoke-Command
execute in the current console's scope instead of the command's own scope.-SessionName <String[]>
: This parameter is only applicable while using theInDisconnectedSession
parameter. We can explicitly provide a name to the disconnected session by using this parameter.
There's more…
You can learn to use a local variable in the remote PowerShell session by going through the following explanation.
Remoting local variable via $Using
We can leverage local variables into remote sessions by the Using
keyword with variable names that are introduced with Windows PowerShell v3.0. Refer to the following example:
PS C:\> $PSCred = Get-Credential PSDomain\PSAdmin PS C:\> Invoke-Command -ComputerName DC01,Member01,Member02 -ScriptBlock {Restart-Computer -Credential $Using:PSCred}
The preceding command statements create the PSCred
local variable containing the PSDomain\PSAdmin
credential along with Password
. The second line of the command statement utilizes the PSCred
variable and the Using
keyword in remote sessions to reboot machines.