1

Ok, am having some issues with my script, they probably are pretty apparent but here is the script. Would greatly appreciate a review of the script with some feedback as why they are broken.

## Script Startup
$Computers = Get-Content .\computers.txt
$Total = $Computers.length
$consoleObject = (Get-Host).UI.RawUI

## Set Variables
$Run = 0
$Successful = 0

# Checks to see if there is a log file already created. If so check number of successful computer ran against, if not, create the log file.
IF ( test-path .\~temp.txt ) {
    $Log = Get-Content .\~temp.txt
    ForEach ($LogLine in $Log) {
    $LogCheck = $LogLine.ToString().Split(",")[2]
        IF ( "$LogCheck" -eq "Successful" ) {
            $Successful += 1
        }
    }
} ELSE {
    add-content .\~temp.txt "Computer Name,Attempts,Last Attempt,Time,Date"
}



while ( "$Completed" -le "$total" ) {
    $Run += 1
    $Time = Get-Date
    $consoleObject.WindowTitle = “Admin Check - $Successful Out Of $Total Successful `| Run`: $Run”

    ForEach ($Computer in $Computers) {
        IF ( Select-String .\~temp.txt -pattern "$Computer" -quiet ) {
            $LogUpdate = Select-String .\~Temp.txt -pattern $Computer
            $Attempts = $LogUpdate.ToString().Split(",")[1]
            $Result = $LogUpdate.ToString().Split(",")[3]
        } ELSE {
            add-content .\~temp.txt "$Computer,0,Not Checked,Not Run Yet,Not Run Yet"
            $Attempts = ""
            $Result = ""
        }

        IF ( "$Result" -eq "Successful") {
            write-output "$Computer Already Completed"
        } ELSE {
            IF ( test-connection $Computer -quiet ) {
                # Command Here
                $Successful += 1
                $IsOn = "True"
            } ELSE {
                $IsOn = "False"
            }
            $Attempts += 1
        }
        ( Get-Content .\~temp.txt ) | Foreach-Object {$_ -replace "$Computer,.*", ($Computer + "," + $Attempts + "," + $IsOn + "," + $Result + "," + $Time.ToShortTimeString() + "," + $Time.ToShortDateString())} | Set-Content .\~temp.txt
    }
}

~temp.txt

Computer Name,Attempts,Last Attempt Result,Time,Date
52qkkgw-94210jv,11111111111111,False,,8:47 PM,10/27/2012
HELLBOMBS-PC,11111111111111111111111111111111111111111111111111111111111,True,,8:47 PM,10/27/2012
52qkkgw-94210dv,11111111111111111111111111111111111111111111111111111111,False,,8:46 PM,10/27/2012

Current Issue: - Doesn't actually stop when successful equals total. - doesn't check result vs successful so keeps redoing all comps forever. - The Attempts tab just keeps adding 1 to the last time or something, so it makes some weird continuous "1111" thing. Probably more just havn't noticed them yet.

4
  • Where is $Completed defined? I see $Run getting incremented but don't see $Completed anywhere. Also, remove those quotes off of your numeric variables. And set them like this [int32]$Run=0 so that they are cast properly as integers. Commented Oct 27, 2012 at 12:44
  • And I forgot : You have a $Total variable and a $total variable. Fix the casing on that as well since variables are case-sensitive. Commented Oct 27, 2012 at 12:46
  • 4
    variables are most definitely not case sensitive in Powershell. Commented Oct 27, 2012 at 14:39
  • Aren't they? My mistake then. That is just a holdover from all my other languages. Never make assumptions. Commented Oct 28, 2012 at 13:31

1 Answer 1

2

Here's working code:

clear
## Script Startup
$Computers = Get-Content \computers.txt
cd c:\pst
$Total = $Computers.length
$consoleObject = (Get-Host).UI.RawUI

## Set Variables
$Run = 1
$Successful = 0

# Checks to see if there is a log file already created. If so check number of successful         computer ran against, if not, create the log file.
IF ( test-path .\~temp.txt ) {
$Log = Get-Content .\~temp.txt
ForEach ($LogLine in $Log) {
$LogCheck = $LogLine.ToString().Split(",")[2]
    IF ( "$LogCheck" -eq "Successful" ) {
        $Successful += 1
    }
}
} ELSE {
add-content .\~temp.txt "Computer Name,Attempts,Last Attempt,Time,Date"
}

while ( $Run -ne $total ) {
$Run += 1
$Time = Get-Date
$consoleObject.WindowTitle = “Admin Check - $Successful Out Of $Total Successful `| Run`: $Run”

ForEach ($Computer in $Computers) {
    IF ( Select-String .\~temp.txt -pattern "$Computer" -quiet ) {
        $LogUpdate = Select-String .\~Temp.txt -pattern $Computer
        $Attempts = $LogUpdate.ToString().Split(",")[1]
        $Result = $LogUpdate.ToString().Split(",")[3]
    } ELSE {
        add-content .\~temp.txt "$Computer,0,Not Checked,Not Run Yet,Not Run Yet"
        $Attempts = ""
        $Result = ""
    }

    IF ( "$Result" -eq "Successful") {
        write-output "$Computer Already Completed"
    } ELSE {
        IF ( test-connection $Computer -quiet ) {
            # Command Here
            $Successful += 1
            $IsOn = "True"
        } ELSE {
            $IsOn = "False"
        }
        $Attempts += 1
    }
    ( Get-Content .\~temp.txt ) | Foreach-Object {$_ -replace "$Computer,.*", ($Computer + "," + $Attempts + "," + $IsOn + "," + $Result + "," + $Time.ToShortTimeString() + "," + $Time.ToShortDateString())} | Set-Content .\~temp.txt
}
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.