Powershell脚本问题(新手)



好吧,我的脚本有一些问题,它们可能很明显,但这是脚本。非常感谢您对剧本的评论,并有一些反馈。

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

当前问题: - 当成功的总和时,实际上并不会停止。 - 不需要检查结果与成功,因此可以永远重做所有的补充。 - "尝试"选项卡只能在最后一次添加1个,因此它使一些奇怪的连续" 1111"事情。可能还没有注意到它们。

这是工作代码:

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

最新更新