We help IT Professionals succeed at work.
Get Started

Run a powershell script asynchronously

Can
Can asked
on
54 Views
Last Modified: 2019-12-22
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.
Comment
Watch Question
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE