Powershell Remoting - Get-Process on Remote Servers
Powershell Code:
$Computers = "DB01", "DB02", "DB03" $s = New-PSSession -computername $Computers Invoke-Command -Session $s -ScriptBlock {Get-Process}
If need to get a count: Invoke-Command -Session $s -ScriptBlock {$p = Get-Process} Invoke-Command -Session $s -ScriptBlock {$p.count}
Remove-PSSession $s
Resources:
Get-Help Get-Process
Get-Help New-PSSession The New-PSSession cmdlet creates a Windows PowerShell session (PSSession) on a local or remote computer. When you create a PSSession, Windows PowerShell establishes a persistent connection to the remote computer. Use a PSSession to run multiple commands that share data, such as a function or the value of a variable. To run commands in a PSSession, use the Invoke-Command cmdlet. To use the PSSession to interact directly with a remote computer, use the Enter-PSSession cmdlet. For more information, see about_PSSessions (http://go.microsoft.com/fwlink/?LinkID=135181). You can run commands on a remote computer without creating a PSSession by using the ComputerName parameters of Enter-PSSession or Invoke-Command. When you use the ComputerName parameter, Windows PowerShell creates a temporary connection that is used for the command and is then closed
Get-Help Remove-PSSession The Remove-PSSession cmdlet closes Windows PowerShell sessions (PSSessions) in the current session. It stops any commands that are running in the PSSessions, ends the PSSession, and releases the resources that the PSSession was using. If the PSSession is connected to a remote computer, Remove-PSSession also closes the connection between the local and remote computers. To remove a PSSession, enter the Name, ComputerName, ID, or InstanceID of the session. If you have saved the PSSession in a variable, the session object remains in the variable, but the state of the PSSession is "Closed."
Get-Command | where { $_.Parameters.Keys -contains "ComputerName" -and $_.Parameters.Keys -NotContains "Session"}
Get-Command -Name *Session*
Get-Command -Module *Core* cls
|
|
|
|
|