This chapter considers Remoting and the PowerShell Gallery. Remoting involves the running of PowerShell commands and scripts on remote computers. The PowerShell Gallery is a central repository of PowerShell content and component of PowerShell.
In the first part of this chapter, we will look at Remoting; in the second part, we will consider the PowerShell Gallery.
Remoting
Remote commands may be run on many computers or  a single computer through the use of either persistent or temporary connections. Two key parameters in PowerShell remoting are ComputerName and Session. The ComputerName parameter is designed for situations where a single command or several unrelated commands are run on one or many computers. The Session parameter establishes a persistent connection to a remote computer. This chapter will consider a set of illustrations that will demonstrate the various methods used in executing remote commands.
After attempting these basic commands, help topics can divulge additional information about the cmdlets used when running the following commands. The help topics also explain and supply details and descriptions on how to modify these commands based on user needs and details.
Interactive Sessions
The simplest approach to executing a remote command is by starting an interactive session with a remote computer. At the start of the session, the remote commands execute on the computer that is remote similar to how direct commands on that remote computer would execute. In such an interactive session, only a single computer may connect. Using the cmdlet Enter-PSSession will start the interactive session. For example, to start an interactive session with computer Server01, use the following command:
>Enter-PSSession Server01
The Server01 computer connection is indicated by a command prompt change:
Server01\PS
At this stage, Server01 commands may be typed. To close the interactive session, use:
>Exit-PSSession
ComputerName Parameter Cmdlets
Many cmdlets include a parameter called ComputerName, which helps obtain objects from remote computers. Since WS-Management-based remoting within PowerShell is not used by this type of cmdlet, its ComputerName parameter can be used within all computers with PowerShell running. The remoting requirements of the system as well as PowerShell remoting configuration are not required on the computers.
The cmdlets that use the parameter ComputerName are shown in Table 6 below.
To illustrate, the following command displays the services that are on the remote computer Server01:
>Get-Service -ComputerName Server01
In most cases, cmdlets supporting remoting exclusive of distinctive configurations include a ComputerName parameter but not a Session parameter. These may be displayed in the session with this command:
>Get-Command | Where-Object {
$_.Parameters.Keys -contains 'ComputerName' -and
$_.Parameters.Keys -notcontains 'Session'
}
This will generate the following output (of which the first few lines are shown):
PS C:\Test> Get-Command | Where-Object {
>>
>> $_.Parameters.Keys -contains 'ComputerName' -and
>>
>> $_.Parameters.Keys -notcontains 'Session'
>>
>> }
CommandType
Name
Version
Source
-----------
----
-------
------
Cmdlet
Add-Computer
3.1.0.0
Microsoft.PowerShell.Management
Cmdlet
Clear-EventLog
3.1.0.0
Microsoft.PowerShell.Management
Cmdlet
Get-EventLog
3.1.0.0
Microsoft.PowerShell.Management
Cmdlet
Get-HotFix
3.1.0.0
Microsoft.PowerShell.Management
Cmdlet
Get-Process
3.1.0.0
Microsoft.PowerShell.Management
Cmdlet
Get-PSSession
3.0.0.0
Microsoft.PowerShell.Core
Cmdlet
Get-Service
3.1.0.0
Microsoft.PowerShell.Management
Cmdlet
Get-WmiObject
3.1.0.0
Microsoft.PowerShell.Management
Cmdlet
Invoke-WmiMethod
3.1.0.0
Microsoft.PowerShell.Management
Running Remote Commands
The cmdlet Invoke-Command is used to execute other commands within remote computers. A limited number of unrelated commands or a single command may be run using the parameter ComputerName within the Invoke-Command cmdlet to denote the computers that are remote. The parameter ScriptBlock may be used to define the command.
To illustrate, this command executes a command designated Get-Culture within the computer Server01:
>Invoke-Command -ComputerName Server01 -ScriptBlock {Get-Culture}
Persistent Connection
When the cmdlet Invoke-Command ComputerName parameter is used, PowerShell will establish a connection only for the purposes of the command. The connection is closed as soon as the command has executed. All functions or variables the command defined are lost. The cmdlet New-PSSession can be used to create a connection that is persistent to a computer that is remote. To illustrate, this command is used to create PSSessions on computers ServerA and ServerB, and then store them in the variable $s:
>$s = New-PSSession -ComputerName ServerA, ServerB
Running Commands in PSSession
A PSSession is used to execute a remote collection of commands that share data. This data may take the shape of variable values, aliases, and functions. PSSession commands may be executed using the parameter Session of the cmdlet Invoke-Command. To illustrate, the following command uses the cmdlet Invoke-Command to execute the command Get-Process within the PSSessions established on computers ServerA and ServerB. The processes are saved in a variable $p by the command in each of the PSSessions.
>Invoke-Command -Session $s -ScriptBlock {$p = Get-Process}
Since a persistent connection is used by the PSSession, another command may be executed within the identical PSSession using the variable $p. This command generates a count of the processes saved in the variable $p.
>Invoke-Command -Session $s -ScriptBlock {$p.count}
Running Remote Commands on Many Computers
A command that is remote may be run on many computers by typing their computer names within the ComputerName parameter value of the Invoke-Command cmdlet using a comma to separate computer names. To illustrate, this command is used to run a command designated Get-Culture within three computers:
>Invoke-Command -ComputerName S1, S2, S3 -ScriptBlock {Get-Culture}
A command may also be run in many PSSessions. These commands create PSSessions on three computers and then executes a command designated Get-Culture within each PSSession:
>$s = New-PSSession -ComputerName S1, S2, S3
>Invoke-Command -Session $s -ScriptBlock {Get-Culture}
A local computer may be included in the computer list by typing its name, in which case the local computer’s designation will be localhost or the dot symbol:
>Invoke-Command -ComputerName S1, S2, S3, localhost -ScriptBlock {Get-Culture}
Remotely Running a Script
A script that is local may be run on computers that are remote by using the InvokeCommand cmdlet FilePath parameter. To illustrate, this command executes the script Sample.ps1 on two computers:
>Invoke-Command -ComputerName Server01, Server02 -FilePath C:\Test\Sample.ps1
The script execution results are sent to the computer that is local. It is not necessary to copy files.
Stopping Remote Commands
A remote command may be interrupted by pressing CTRL+C. The request for an interruption is sent to the computer that is remote where it stops the command that is remote.
PowerShell Gallery
The PowerShell content central repository is known as PowerShell Gallery or simply Gallery. Within it, DSC resources and modules containing commands may be found. It is also possible to find scripts. The Gallery packages are authored within the PowerShell community and Microsoft.
PowerShellGet
The module PowerShellGet has cmdlets for publishing, installing, discovering, and updating PowerShell packages from the central repository and other private repositories. These packages house artifacts such as DSC resources, modules, scripts, and role capabilities.
Getting Started: PowerShell Gallery
Gallery package installations require an updated version of PowerShellGet, which is part of PowerShell7.0. MacOS, Linux, and Windows all support PowerShellGet operation.
Latest version of PowerShellGet
Install the updated version of the NuGet provider before looking to update PowerShellGet. This is done with the following commands:
>Install-PackageProvider -Name NuGet -Force
>Exit
Then, run these commands:
>Install-Module -Name PowerShellGet -Force
>Exit
The Update-Module cmdlet should be used to obtain updated versions. This can be done with this command:
>Update-Module -Name PowerShellGet
>Exit
PowerShell Gallery Overview
PowerShellGet cmdlets may be used for installing packages from within PowerShell Gallery. The Gallery may be used without first signing in to obtain PowerShell content. The Gallery may also be used to download packages directly; however, this method is not suggested.
Discovering Packages
Gallery packages can be found by making use of the search facility in the Gallery home page or by exploring scripts and modules in the packages page. Packages may also be found using the Find-Script, Find-DSCResource, and Find-Module cmdlets with the Repository parameter value PSGallery.
Gallery results may be filtered by using the following parameters:
     AllVersions
     Filter
     Name
     MinimumVersion
     RequiredVersion
     Tag
     Includes
     RoleCapability
     DscResource
     Command
The Find-DscResource cmdlet may be used for discovering particular DSC resources within the Gallery. The cmdlet displays information about DSC resources housed within the Gallery. DSC resources are delivered using modules. It is necessary to use the Install-Module cmdlet to install DSC resources.
Learning about PowerShell Gallery Packages
Once a package is identified, it may be necessary to learn about it more. This may be done by examining the package page within the Gallery. The page may be used to display metadata on the package. The metadata is supplied by the package author.
The other method is by using the Find-Script or Find-Module cmdlets to display the PSGetModuleInfo object. To illustrate, this command displays Gallery data about the module PSReadLine:
>Find-Module -Name PSReadLine -Repository | Get-Member
Downloading PowerShell Gallery packages
This is the suggested manner with which to download packages contained in the Gallery:
     Inspect:
The Save-Script or Save-Module cmdlets may be used for downloading a Gallery package for examination. This method allows for the package to be saved locally without installation. Once saved locally, its contents may be reviewed.
     Install:
  1. Install-Module
The default installation path for Install-Module is:
$env:ProgramFiles\WindowsPowerShell\Modules.
Administrator access is required to install modules in this path. If the Scope parameter in the Install-Module cmdlet is set to value CurrentUser, the module installation path will be:
$env:USERPROFILE\Documents\WindowsPowerShell\Modules
  1. Install-Scripts
The default installation path for Install-Script is:
$env:ProgramFiles\WindowsPowerShell\Scripts
Administrator access is needed to install scripts to this path.
If the Scope parameter in the Install-Script cmdlet is set
to value CurrentUser, the script installation path will be:
$env:USERPROFILE\Documents\WindowsPowerShell\Scripts
Install-Script and Install-Module, by default, install the latest version of a specified package.The RequiredVersion parameter value can be used to install a specific version.
Package Update
Packages installed from the gallery may be updated by running either the [Update-Script][] or [Update-Module][] cmdlets. When the [Update-Script][] cmdlet is invoked without parameters, it will try updating all scripts that were installed by invoking the Install-Script cmdlet. The Name parameter may be used to update specific scripts. Analogously for modules, [Update-Module][] will try updating all modules that were installed using the Install-module cmdlet. The Name parameter may also be used to update specific modules.
List Packages installed from PowerShell Gallery
The Get-InstalledModule cmdlet may be used to determine the modules that were installed from the Gallery. This command displays all modules in the system obtained directly from the Gallery. Analogously for scripts, the Get-InstalledScripts cmdlet may be used to ascertain the scripts that were installed from the Gallery. This command, analogously to modules, will display all scripts in the system obtained directly from the Gallery.
Exercises
8.1. What cmdlet is used to start an interactive session on a computer?
8.2. How many cmdlets have a ComputerName parameter in PowerShell 7.0?
8.3. How is remote command run using the Invoke-Command cmdlet?
8.4. What cmdlet is used to create a persistent connection to a remote computer?
8.5. How are commands run in persistent connection?
8.6. How are local scripts run on remote computers?
8.7. What module is used to acquire PowerShell content from the PowerShell Gallery?
8.8. What type of content is contained in PowerShell Gallery packages?
8.9. What command is used to install the latest NuGet provider in PowerShell?
8.10. What command is used to install the latest version of PowerShellGet in PowerShell?
8.11. What cmdlet is used to update modules installed from the PowerShell Gallery?
8.12. What cmdlet is used to update scripts installed from the PowerShell Gallery?
8.13. What cmdlet is used to obtain a list of all modules installed from the PowerShell Gallery?
8.14. What cmdlet is used to obtain a list of all scripts installed from the PowerShell Gallery?
Chapter Summary
The key points of the chapter were:
       PowerShell commands can be run remotely on a single computer or collection of computers.
        The easiest way to run remote commands is through an interactive session.
        A more efficient way to run remote commands is through a persistent connection.
        Remoting allows for PowerShell scripts to be run remotely.
        PowerShell Gallery is a central repository for PowerShell content.
       The PowerShellGet module contains cmdlets that are used for discovering, installing, and publishing PowerShell packages.