好的,我的脚本有一些问题,它们可能非常明显,但这是脚本。非常感谢对脚本的审查以及一些反馈,说明它们为什么会被破坏。
## 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
当前问题: - 当成功等于总数时,实际上不会停止。 - 不检查结果vs成功,所以永远重做所有comps。 - Attempts选项卡只是在最后一次或其他时间添加1,所以它会产生一些奇怪的连续“1111”事物。 可能更多的是还没有注意到它们。
答案 0 :(得分: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
}
}