Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Instant Windows PowerShell Guide

You're reading from  Instant Windows PowerShell Guide

Product type Book
Published in Nov 2013
Publisher Packt
ISBN-13 9781849686785
Pages 86 pages
Edition 1st Edition
Languages
Author (1):
Harshul Patel Harshul Patel
Profile icon Harshul Patel
Toc

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 the SkipNetworkProfileCheck parameter, or we need to create a firewall rule manually by using the Set-NetFilewallRule CMDLET of the NetSecurity 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 . or localhost 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 new PSSession on remote computers and starts the execution of ScriptBlock or a script specified with the FilePath 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 make Invoke-Command execute in the current console's scope instead of the command's own scope.

  • -SessionName <String[]>: This parameter is only applicable while using the InDisconnectedSession 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.

You have been reading a chapter from
Instant Windows PowerShell Guide
Published in: Nov 2013 Publisher: Packt ISBN-13: 9781849686785
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}