Skip to content

Instantly share code, notes, and snippets.

@whosgonna
Created May 21, 2020 19:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whosgonna/4cbbf9bfc0ca413cd5f440de3706d52c to your computer and use it in GitHub Desktop.
Save whosgonna/4cbbf9bfc0ca413cd5f440de3706d52c to your computer and use it in GitHub Desktop.
How to execute tasks in bulk via powershell remoting
## 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