Quantcast
Channel: Windows PowerShell Forum
Viewing all articles
Browse latest Browse all 2562

Powershell v2 | My First Script, Need Guiding Hand for Start-Job Function. (Advanced?)

$
0
0

Alright everything thank you for taking a look at the post as I seem to have hit a brick wall... I am trying to "Multi-Thread" aka add the start-job function to my script. I spent about 4-5 days busting my but getting what I have now, but want to speed it up. From all the tutorials/documentation I can find i don't have any idea how to execute a block of script via start-job. And if i do that, will it effect the numbers that are written to the log? Any help would be immensely appreciated thank you very much in advance.

My Script:

#Script Startup
Set-PSDebug -strict
$consoleObject = (Get-Host).UI.RawUI

#Loads Computer Information
$Computers = Get-Content .\Computers.txt


#Sets Default Values
$Run = 0
$Total = $Computers.length

# Load or Create Log File As Needed.
IF ( test-path .\Log.txt ) {
	$Log = @(Import-Csv .\log.txt)

	$Successful = ($Log | Where-Object {$_.Result -eq "Successful"} | Select-String -inputobject {$_."Computer Name"} -pattern $Computers | Measure-Object).Count

} ELSE {
	add-content .\Log.txt "Computer Name,Is On,Attempts,Result,Time,Date"
}

while ( "$Successful" -le "$Total" ) {
	$Run += 1

	ForEach ($Computer in $Computers) {
		$Time = Get-Date
		$Successful_Percent = $Successful / $Total * 100
		$Successful_Percent = $('{0:N0}' -f $Successful_Percent);
		$consoleObject.WindowTitle = “Admin Check - $Successful Out Of $Total ($Successful_Percent%) Successful `| Run`: $Run”
		
		IF (!($Log | Select-String -pattern "$Computer" -SimpleMatch)) {
			$Log += New-Object PSObject -Property @{ "Computer Name" = $Computer; "Is On" = "Not Checked"; Attempts = 0; Result = "Not Checked Yet"; Time = "Not Checked Yet"; Date = "Not Checked Yet" }
		}
		
		$Computer = $Log | Where-Object {$_."Computer Name" -eq "$Computer"}
		
		IF ($Computer.Result -eq "Successful") {
			write-output "$($Computer.'Computer Name') Already Completed"
		} ELSE {
			IF ( test-connection $Computer."Computer Name" -quiet ) {
				$Computer."Is On" = "True"
                # Command Starts Here
				$CheckComputer = [ADSI]("WinNT://" + $($Computer.'Computer Name') + ",computer")
				$Group = $CheckComputer.psbase.children.find("Administrators")
				$members= $Group.psbase.invoke("Members") | %{$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}
				ForEach($user in $members) {
					$Result = $($Computer.'Computer Name') + "," + $user.ToString()
					$Result
				}
				# Command Ends Here
                $Successful += 1
                $Computer.Result = "Successful"
			} ELSE {
				$IsOn = "False"
			}
			$Computer.Attempts = [int] $Computer.Attempts + 1
			( Get-Content .\log.txt ) | Foreach-Object {$_ -replace "$($Computer.'Computer Name'),.*", ($($Computer.'Computer Name') + "," + $($Computer.'Is On') + "," + $Computer.Attempts + "," + $Computer.Result + "," + $Time.ToShortTimeString() + "," + $Time.ToShortDateString())} | Set-Content .\log.txt
		}
	}
}
Write-Output "Script Completed"


Viewing all articles
Browse latest Browse all 2562

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>