Thursday, September 5, 2019

Basics of PowerShell Session


#Create a new Powershell Session to the computer
New-PSSession -ComputerName client7-1

Id Name            ComputerName    State         ConfigurationName     Availability
 -- ----            ------------    -----         -----------------     ------------
 12 Session12       client7-1       Opened        Microsoft.PowerShell     Available

#It gets the PS Session details
# Like Id, Name, ComputerName, State, ConfigurationName, Availability
Get-PSSession

                                                                       
#enter the powershell Session
Enter-PSSession -ID 12

PS C:\Windows\system32> Enter-PSSession -ID 12                                           
[client7-1]: PS C:\Users\Administrator\Documents> echo $env:ComputerName              

#To know which computer we are logged into
echo $env:ComputerName
CLIENT7-1

# Exit the powershell session
Exit-PSSession

echo $env:ComputerName
DCNUGGET
#To see the list of PS Session
Get-PSSession
Id Name            ComputerName    State         ConfigurationName     Availability
 -- ----            ------------    -----         -----------------     ------------
 12 Session12       client7-1       Opened        Microsoft.PowerShell     Available

#To remove the PSSesion, then we will not be able to connect back
Remove-PSSession -id 12

Get-PSSession
                         

No comments:

Post a Comment

How to suspend an script activity Temporarily ?

There are situations where the script execution needs to be suspended or paused for few milliseconds or seconds. PowerShell Syntax St...