$false 时执行 $true 语句
Executing $true statement when $false
我在使用 PowerShell 脚本时遇到了一些问题。这样做的目的是爬取网络并查找存在于任何 PC 上的 files/folders。
这是原始来源:
#FiToFin Script#
$Fltr = "how_recover*.*"
$Online = "C:\Users\<username>\Scripts\Logs\Online.log"
$CSV = "C:\Users\<username>\Scripts\Devices.csv"
#$Tstpath = test-path "\$computer\c$"
$Offline = "C:\Users\<username>\Scripts\Logs\Offline.log"
##################################################
$devices = Get-Content "$CSV"
foreach ($computer in $devices) {
Test-Path "\$computer\c$" > $Tstpath
if ($Tstpath -eq $True) {
ls -Path "\$computer\c$\users" -Filter $Fltr -Recurse |
Out-File -Append $Online
} else {
Write-Host "$computer is NOT Online" | Out-File -Append $Offline
}
}
##################################################
Write-Host "_____________________"
Write-Host "Online file = $Online"
Write-Host "Offile file = $Offline"
Write-Host "_____________________"
我已将 if
语句更改为 if($Tstpath -eq "True")
、if ($lastexitcode -eq $true)
和 if($Tstpath -eq $false)
,无论如何它们都只解析第一个 {Do command}
。他们从不掉进 else
。甚至尝试将 Tstpath = Test-Path \$computer\c$
作为变量,而只是 运行。
当它解析第一个 {Do Command}
时,return 是
ls : Cannot find path '\<computerName>\c$\u' because it does not exist.
At C:\Users\<username>\Scripts\FiToFin.ps1:19 char:3
+ ls -Path "\$computer\c$\users" -Filter $Fltr -Recurse | Out-File -Append $On ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (\<computername>\c$\u:String) [Get-ChildItem], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
什么有效:
如果我的测试机器在运行,我可以 ls -Path "\$computer\c$\users" -Filter $Fltr -Recurse | Out-File -Append $Online
就好了。
我从 Test-Path \$computer\c$
得到 True
或 False
,甚至 > $var
和 Write-Host
结果也很好。
我不知道这是为什么,很想知道。
这也有效:
###################################################################
$computer = "TestPC"
$Tstpath = Test-Path \$computer\c$
####################################################################
$Tstpath > $null
if($Tstpath -eq $True) {
Write-Host "$computer is Online"
} else {
Write-Host "$computer is NOT Online"
}
但是当您添加命令 ls
或 Get-ChildItem
时,它会崩溃。
所以,问题是:为什么它从不执行 else
部分?
我发现有两个问题可能会导致您遇到问题。如何初始化和更新变量 $Tstpath
# Presumably Initialize
$Tstpath = test-path "\$computer\c$"
# Updating in loop
test-path "\$computer\c$" > $Tstpath
我假设您正在 PowerShell ISE 中进行测试,并且 $Tstpath
在某个时候有一个 $true 值。
问题是您从未更新变量。查看 about_redirection 的 TechNet,您会看到:
Operator Description Example
-------- ---------------------- ------------------------------
'>' Sends output to the Get-Process > Process.txt
specified file.
您的命令试图将其输出到 "file"。你应该有一个关于无法找到文件或系统上某处的文件的错误,其中包含一个布尔值(因为它不是追加重定向器)。
为了保持逻辑,您应该做的是通过赋值保存结果。
$Tstpath = Test-path "\$computer\c$"
那你可以测试一下。
但是它是多余的,因为您不再需要该值。将它直接放在 if 语句中会更容易。
if(test-path "\$computer\c$"){"Do something"}else{"Fail Trumpet"}
我还建议使用 Export-CSV -Append
,因为您正在处理对象。会产生良好的结构化输出。
Get-ChildItem -path "\$computer\c$\users\" -Filter $Fltr -Recurse | Export-CSV -Append $Online -NoTypeInformation
我在使用 PowerShell 脚本时遇到了一些问题。这样做的目的是爬取网络并查找存在于任何 PC 上的 files/folders。
这是原始来源:
#FiToFin Script#
$Fltr = "how_recover*.*"
$Online = "C:\Users\<username>\Scripts\Logs\Online.log"
$CSV = "C:\Users\<username>\Scripts\Devices.csv"
#$Tstpath = test-path "\$computer\c$"
$Offline = "C:\Users\<username>\Scripts\Logs\Offline.log"
##################################################
$devices = Get-Content "$CSV"
foreach ($computer in $devices) {
Test-Path "\$computer\c$" > $Tstpath
if ($Tstpath -eq $True) {
ls -Path "\$computer\c$\users" -Filter $Fltr -Recurse |
Out-File -Append $Online
} else {
Write-Host "$computer is NOT Online" | Out-File -Append $Offline
}
}
##################################################
Write-Host "_____________________"
Write-Host "Online file = $Online"
Write-Host "Offile file = $Offline"
Write-Host "_____________________"
我已将 if
语句更改为 if($Tstpath -eq "True")
、if ($lastexitcode -eq $true)
和 if($Tstpath -eq $false)
,无论如何它们都只解析第一个 {Do command}
。他们从不掉进 else
。甚至尝试将 Tstpath = Test-Path \$computer\c$
作为变量,而只是 运行。
当它解析第一个 {Do Command}
时,return 是
ls : Cannot find path '\<computerName>\c$\u' because it does not exist.
At C:\Users\<username>\Scripts\FiToFin.ps1:19 char:3
+ ls -Path "\$computer\c$\users" -Filter $Fltr -Recurse | Out-File -Append $On ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (\<computername>\c$\u:String) [Get-ChildItem], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
什么有效:
如果我的测试机器在运行,我可以 ls -Path "\$computer\c$\users" -Filter $Fltr -Recurse | Out-File -Append $Online
就好了。
我从 Test-Path \$computer\c$
得到 True
或 False
,甚至 > $var
和 Write-Host
结果也很好。
我不知道这是为什么,很想知道。
这也有效:
###################################################################
$computer = "TestPC"
$Tstpath = Test-Path \$computer\c$
####################################################################
$Tstpath > $null
if($Tstpath -eq $True) {
Write-Host "$computer is Online"
} else {
Write-Host "$computer is NOT Online"
}
但是当您添加命令 ls
或 Get-ChildItem
时,它会崩溃。
所以,问题是:为什么它从不执行 else
部分?
我发现有两个问题可能会导致您遇到问题。如何初始化和更新变量 $Tstpath
# Presumably Initialize
$Tstpath = test-path "\$computer\c$"
# Updating in loop
test-path "\$computer\c$" > $Tstpath
我假设您正在 PowerShell ISE 中进行测试,并且 $Tstpath
在某个时候有一个 $true 值。
问题是您从未更新变量。查看 about_redirection 的 TechNet,您会看到:
Operator Description Example -------- ---------------------- ------------------------------ '>' Sends output to the Get-Process > Process.txt specified file.
您的命令试图将其输出到 "file"。你应该有一个关于无法找到文件或系统上某处的文件的错误,其中包含一个布尔值(因为它不是追加重定向器)。
为了保持逻辑,您应该做的是通过赋值保存结果。
$Tstpath = Test-path "\$computer\c$"
那你可以测试一下。
但是它是多余的,因为您不再需要该值。将它直接放在 if 语句中会更容易。
if(test-path "\$computer\c$"){"Do something"}else{"Fail Trumpet"}
我还建议使用 Export-CSV -Append
,因为您正在处理对象。会产生良好的结构化输出。
Get-ChildItem -path "\$computer\c$\users\" -Filter $Fltr -Recurse | Export-CSV -Append $Online -NoTypeInformation