Hello!
In my new position at work, I’ve been learning powershell to automate various things. I’ve been tweaking this one script that I created, but I am running into issues.
The Purpose: To scan a last of multiple computers from Active Directory using dumpsec.exe. Each machine is pinged first prior to scanning. The Problem: The scan takes too long, and appears to freeze at various points. The Supposed Solution: Run a workflow to ping each machine first, then output successfully pingable computers to another list, then have dumpsec scan that list itself.
I was able to get this far:
workflow TestCon { param ( [string[]]$Computers, $SiteCode, $HostsFile ) $Computers = Get-Content .$SiteCode$HostsFile foreach -parallel ($computer in $computers){ if (Test-Connection -ComputerName $computer -Count 1 -ErrorAction SilentlyContinue -Quiet){ Out-File .$SiteCode$($SiteCode + "Scan.txt") -Append } else{ Out-File .$SiteCode$($SiteCode + "NoPing.txt") -Append } } } function dumpsec($SiteCode, $ScanFile, $FileName, $NoPing){ $hosts = Get-Content .$SiteCode$ScanFile foreach ($name in $hosts){ CMD /c dumpsec.exe /computer="\$name" /rpt=policy /saveas=csv /outfile=.$name.txt "&" copy /a .$SiteCode$FileName + .$name.txt .$SiteCode$FileName "&" del .$name.txt } }
I see that because the workflow is accessing “Scan.txt” concurrently, it is going to have access issues. What is the best way to alleviate this problem?
submitted by /u/Punkrulz
[link] [comments]
The post Learning PS, Workflow, Output to File appeared first on How to Code .NET.