This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#region Connect to Remote PowerShell WinRM HTTPS port 5986 with untrusted certificate - single connection | |
### Variables | |
$ServerName = "ServerName / or / IP Address" | |
$adminname = 'UserName' | |
$adminpassword = 'Password' | |
$password = ConvertTo-SecureString $adminpassword -AsPlainText -Force | |
$cred= New-Object System.Management.Automation.PSCredential (".\$adminname", $password) | |
### Enter session | |
$SessionOptions = New-PSSessionOption –SkipCACheck –SkipCNCheck –SkipRevocationCheck | |
Enter-PSSession -ComputerName $ServerName -Port 5986 -UseSSL -Credential $cred -SessionOption $SessionOptions | |
########################################################################################## | |
#################################### Do Stuff ##################################### | |
########################################################################################## | |
### Get Service | |
Get-Service | ? {$_.Status -match 'runn'} | |
### Create Directory | |
New-Item -Path c:\ -Name MyDirectory -Type Directory -Force | |
### Check Direcroty Listing | |
Get-ChildItem -Path c:\ | ? {$_.Attributes -match 'Directory'} | |
########################################################################################## | |
Exit-PSSession | |
#endregion | |
#region Connect to Remote PowerShell WinRM HTTPS port 5986 with untrusted certificate - multiple connections | |
$adminname = 'UserName' | |
$adminpassword = 'password' | |
$password = ConvertTo-SecureString $adminpassword -AsPlainText -Force | |
$cred= New-Object System.Management.Automation.PSCredential (".\$adminname", $password) | |
$SessionOptions = New-PSSessionOption –SkipCACheck –SkipCNCheck –SkipRevocationCheck | |
$Computers = "123.221.252.23", "123.221.58.233", "ComputerName03" | |
$Params = @{ | |
ComputerName = $Computers | |
Port = 5986 | |
Credential = $cred | |
} | |
New-PSSession @params -UseSSL -OutVariable ps -SessionOption $SessionOptions | |
Invoke-Command -Session $ps -ScriptBlock { | |
# Do Stuff on all computers at the same time | |
$process = "cmd.exe" | |
$arguments = '/bla /bla etc' | |
start-process $process -ArgumentList $arguments -Wait | |
} | |
#endregion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment