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

Powershell v2 | My First Script, multiple easy questions

$
0
0

Ok, spent nearly 3 full days on google trying to get this script to work but I currently have a few issues, so this will be a pretty long post, and before we begin I wanna say I'm sorry I'm new to powershell and this is my first script.

Full 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"}
	IF (!$Successful) { $Successful = 0 }
} 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.length / $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 "Already Completed"
		} ELSE {
			IF ( test-connection $Computer."Computer Name" -quiet ) {
				$Computer."Is On" = "True"
                # Command Here
                $Successful += 1
                $ComputerResult = "Successful"
			} ELSE {
				$IsOn = "False"
			}
			$Computer.Attempts += 1
		}
	}
}
Write-Output "Script Completed"

Log File

Computer Name,Is On,Attempts,Result,Time,Date
Computer-Name1,False,1,Failed,9:48 AM,10/28/2012
HELLBOMBS-PC,True,1,Successful,9:48 AM,10/28/2012
Computer-Name2,False,1,Failed,9:48 AM,10/28/2012
Computer-Name3,False,1,Successful,9:48 AM,10/28/2012

Issue 1: Success Doesn't work

The "$Successful" variable doesn't work, no matter what i try keeps just defaulting to nothing.

Issue 2: No idea how to expand certain variables

As you can see above one of my variables sub name, er whatever its called is $Computer."Computer Name". How do i call that in something like write-output "!IWantComputerNameHere! Already 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>