Created
May 21, 2020 19:59
-
-
Save whosgonna/4cbbf9bfc0ca413cd5f440de3706d52c to your computer and use it in GitHub Desktop.
How to execute tasks in bulk via powershell remoting
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
## General concept for running remote tasks in powershell on multiple domain computers | |
# | |
## First create a list of computer(s) on which to execute the job. This can be a single | |
## computer by hostname, like this: | |
$computers = 'COMPUTER1' ## A single Computer | |
## OR ## | |
$computers = @( 'COMPUTER1', 'COMPUTER2'); | |
## OR ## | |
$computers = $(Get-ADComputer -Filter * -Searchbase 'OU=Computers,DC=DOMAIN,DC=COM') | ForEach-Object {$_.Name} | |
$ses = New-PsSession -ComputerName $computers | |
$script = "C:\Path\To\PS\Script.ps1"; | |
$invoke_session = Invoke-Command -Session $ses -FilePath $script -AsJob | |
## Get feedback from the script: | |
$invoke_session | Receive-Job -wait | |
## Destroy the sessions. | |
$ses | Remove-PSSession | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment