Powershell 基础知识 - 递增地增加一个变量来管理一个循环
Powershell basics - incrementally increase a variable to manage a loop
我是 PowerShell 的初学者,我正在努力让我的代码跳出 For 循环。更具体地说,我的 $parseCount
变量在调用 $ParseCount++
时总是被重置为“1”,即使一些预先存在的条件意味着它的值是原始的“2”。
因此,我一直陷入无限循环。
在下面的示例中,脚本在第一遍中正确推断出应该完成 "work" 的哪个级别。但随后它将始终将 $ParseCount 变量设置为 1,而不是 $ParseCount + 1。
我相信这很简单。在此先感谢您的帮助!
# all possible Scenarios
If ($Scenario -ieq "Outcome1") {
$ParseCount=0
}
If ($Scenario -ieq "Outcome2") {
$ParseCount=1
}
If ($Scenario -ieq "Outcome3") {
$ParseCount=2
}
# Start the loop
For ($ParseCount -lt 3){
# determine what work to do
If ($ParseCount=0){
write-host "I'm doing some prerequisite stuff"
}
If ($ParseCount -gt 0){
write-host "I'm doing all of the work, beacause prerequisite is done"
}
# Return to the top of the loop
write-host "ParseCount variable is:", $ParseCount
$ParseCount++
write-host "ParseCount was changed, is now set to:", $ParseCount
}
示例输出:
ParseCount 变量为:2
ParseCount 已更改,现在设置为 1
你应该改变
If ($ParseCount=0){
write-host "I'm doing some prerequisite stuff"
}
If ($ParseCount -gt 0){
write-host "I'm doing all of the work, beacause prerequisite is done"
}
(这会将 $ParseCount
设置回 0)
进入
If ($ParseCount -eq 0){
write-host "I'm doing some prerequisite stuff"
}
If ($ParseCount -gt 0){
write-host "I'm doing all of the work, beacause prerequisite is done"
}
我是 PowerShell 的初学者,我正在努力让我的代码跳出 For 循环。更具体地说,我的 $parseCount
变量在调用 $ParseCount++
时总是被重置为“1”,即使一些预先存在的条件意味着它的值是原始的“2”。
因此,我一直陷入无限循环。
在下面的示例中,脚本在第一遍中正确推断出应该完成 "work" 的哪个级别。但随后它将始终将 $ParseCount 变量设置为 1,而不是 $ParseCount + 1。
我相信这很简单。在此先感谢您的帮助!
# all possible Scenarios
If ($Scenario -ieq "Outcome1") {
$ParseCount=0
}
If ($Scenario -ieq "Outcome2") {
$ParseCount=1
}
If ($Scenario -ieq "Outcome3") {
$ParseCount=2
}
# Start the loop
For ($ParseCount -lt 3){
# determine what work to do
If ($ParseCount=0){
write-host "I'm doing some prerequisite stuff"
}
If ($ParseCount -gt 0){
write-host "I'm doing all of the work, beacause prerequisite is done"
}
# Return to the top of the loop
write-host "ParseCount variable is:", $ParseCount
$ParseCount++
write-host "ParseCount was changed, is now set to:", $ParseCount
}
示例输出:
ParseCount 变量为:2 ParseCount 已更改,现在设置为 1
你应该改变
If ($ParseCount=0){
write-host "I'm doing some prerequisite stuff"
}
If ($ParseCount -gt 0){
write-host "I'm doing all of the work, beacause prerequisite is done"
}
(这会将 $ParseCount
设置回 0)
进入
If ($ParseCount -eq 0){
write-host "I'm doing some prerequisite stuff"
}
If ($ParseCount -gt 0){
write-host "I'm doing all of the work, beacause prerequisite is done"
}