Avatar of Can
Can

asked on 

Run a powershell script asynchronously

Hi All,

I got a powershell script to export all local admin accounts from ad computers to a CSV:

$servers= Get-Content -Path 'C:\Localdata\ADcomputers.csv'
$output = 'C:\localdata\PRODadmin.csv' 

$servers | ForEach-Object {
	$server = $_
	Write-Host "Processing $($server)"
	$group = [ADSI]"WinNT://$($server)/Administrators" 
	$group.psbase.Invoke('Members') | ForEach-Object {
		New-Object PSObject -Property ([ordered]@{
			Server = $server
			Admin = $_.GetType().InvokeMember('Name', 'GetProperty', $null, $_, $null)
		})
	}
} |
	Where-Object {$_.Admin -notmatch '^(L RG.*|Locbeh|Domain Admins|Enterprise Admins|Administrator|G RB.*|SA_.*)$'} |
	Export-csv $Output -NoTypeInformation

Open in new window


Yet this is taking a very long time since its running the script synchronously. Is there a way to edit the script so it runs asynchronous?

Thanks in advance.
Powershell

Avatar of undefined
Last Comment
oBdA

8/22/2022 - Mon