1

好的,我的脚本有一些问题,它们可能很明显,但这是脚本。非常感谢您对脚本进行审查,并提供一些反馈,说明它们被破坏的原因。

## 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

当前问题: - 当成功等于总数时实际上不会停止。- 不检查结果与成功,所以永远重做所有的比赛。- Attempts 选项卡只是不断地在最后一次或某事上加 1,所以它会产生一些奇怪的连续“1111”的东西。可能更多只是还没有注意到它们。

4

1 回答 1

2

这是工作代码:

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
}
}
于 2012-10-29T09:16:54.597 回答