测试 for/foreach 循环中是否存在远程 UNC 路径时的不同结果
Different results when testing if remote UNC path exists in a for/foreach loop
当试图遍历一组约 3k 台机器以检查文件夹路径是否存在时,Test-Path (PowerShell) 和 'IF EXIST' (Batch) return 都是虚假结果,说当 实际上存在时,远程路径并不存在。
我 运行 PowerShell 会话和 ISE(和命令提示符)'As An Administrator' 具有与我的用户登录帐户不同的权威域凭据。我已将 -Credential 参数提供给 Test-Path,结果没有任何变化。
我是 运行 Win10 v1709 (10.0.16299.547) 和 PowerShell v5.1.16299.547。
当 运行 命令本身在一次性机器名称上时,它们起作用:
Powershell:
Test-Path "\machineName\c$\Program Files (x86)\Common Files\Folder Name"
批次:
IF EXIST "\machineName\c$\Program Files (x86)\Common Files\Folder Name" (echo True)
以上两个例子return 'True' 符合预期。
然而,当在 for/foreach 循环中使用这些命令时,我只得到 'False' 结果 :(
PowerShell:
$Computers = Get-Content c:\logs\computers.txt
Write-Output "Checking $($Computers.count) Machines ..."
foreach ($Computer in $Computers)
{
if (Test-Path "\$Computer\c$\Program Files (x86)\Common Files\Folder Name")
{
Write-Output "$($Computer): Folder Exists"
}
}
批次:
@echo off
for /f %%i in (C:\logs\computers.txt) do (
echo | SET /P nonewline=Checking %%i ...
IF EXIST "\%%i\c$\Program Files (x86)\Common Files\Folder Name" (
echo Found
echo %%i >> c:\logs\folder_exists.txt
) ELSE (echo .)
)
pause
这两个例子 return 没有。
我从哪里开始寻找可能导致这种不良行为的原因?
是否有从我的域应用的可能导致此结果的 GPO?
如果循环不起作用,请添加一些调试逻辑。目前,脚本在两种情况下静默失败:1) $Computers
为空 2) test-path
出现意外问题。
当一个人在控制台上工作时,初始化一些变量并且从不在脚本版本中分配它们是一个常见的错误。要捕获这种情况,请将 set-strictmode
添加到脚本中,以便在使用未初始化的变量时它会抱怨。
set-strictmode -Version 2.0
# Prohibits references to uninitialized variables
# and references to non-existent properties of an object.
# This eliminates common typos
if($Computers.Length -le 0) { # Is there data to process?
write-host '$Computers collection was empty!'
}
if($Computers.GetType().name -ne "Object[]") { # Is the collection an array anyway?
write-host '$Computers wasnt an array but: ' $Computers.GetType()
}
foreach ($Computer in $Computers)
{
if (Test-Path "\$Computer\c$\Program Files (x86)\Common Files\Folder Name")
{
Write-Output "$($Computer): Folder Exists"
} else { # test-path fails. Write a warning and the path tried
write-host -nonewline "Fail: "
write-host "test-path \$Computer\c$\Program Files (x86)\Common Files\Folder Name"
}
}
批量分辨率:
在 Notepad++ 中打开文本文件,不知何故文件的编码设置为 'UCS-2 LE BOM' 而不是预期的 'UTF-8'。将编码设置为 UTF-8 并保存文件可解决批处理文件问题。
很多年前我遇到过同样的问题,所以我有点尴尬,因为我在发布之前没有想到这一点。
PowerShell 解析:
我最近开始使用 SCCM 模块,但没有注意到我的脚本将我的位置设置为 "PS $SCCMsiteCode:>" PSDrive。
只需在 ISE 控制台窗格中键入 'c:' 并按回车键,脚本就可以 return 预期的结果。
我认为当前位置不会阻止 Write-Output 写入控制台窗格。
当试图遍历一组约 3k 台机器以检查文件夹路径是否存在时,Test-Path (PowerShell) 和 'IF EXIST' (Batch) return 都是虚假结果,说当 实际上存在时,远程路径并不存在。 我 运行 PowerShell 会话和 ISE(和命令提示符)'As An Administrator' 具有与我的用户登录帐户不同的权威域凭据。我已将 -Credential 参数提供给 Test-Path,结果没有任何变化。
我是 运行 Win10 v1709 (10.0.16299.547) 和 PowerShell v5.1.16299.547。
当 运行 命令本身在一次性机器名称上时,它们起作用:
Powershell:
Test-Path "\machineName\c$\Program Files (x86)\Common Files\Folder Name"
批次:
IF EXIST "\machineName\c$\Program Files (x86)\Common Files\Folder Name" (echo True)
以上两个例子return 'True' 符合预期。
然而,当在 for/foreach 循环中使用这些命令时,我只得到 'False' 结果 :(
PowerShell:
$Computers = Get-Content c:\logs\computers.txt
Write-Output "Checking $($Computers.count) Machines ..."
foreach ($Computer in $Computers)
{
if (Test-Path "\$Computer\c$\Program Files (x86)\Common Files\Folder Name")
{
Write-Output "$($Computer): Folder Exists"
}
}
批次:
@echo off
for /f %%i in (C:\logs\computers.txt) do (
echo | SET /P nonewline=Checking %%i ...
IF EXIST "\%%i\c$\Program Files (x86)\Common Files\Folder Name" (
echo Found
echo %%i >> c:\logs\folder_exists.txt
) ELSE (echo .)
)
pause
这两个例子 return 没有。
我从哪里开始寻找可能导致这种不良行为的原因?
是否有从我的域应用的可能导致此结果的 GPO?
如果循环不起作用,请添加一些调试逻辑。目前,脚本在两种情况下静默失败:1) $Computers
为空 2) test-path
出现意外问题。
当一个人在控制台上工作时,初始化一些变量并且从不在脚本版本中分配它们是一个常见的错误。要捕获这种情况,请将 set-strictmode
添加到脚本中,以便在使用未初始化的变量时它会抱怨。
set-strictmode -Version 2.0
# Prohibits references to uninitialized variables
# and references to non-existent properties of an object.
# This eliminates common typos
if($Computers.Length -le 0) { # Is there data to process?
write-host '$Computers collection was empty!'
}
if($Computers.GetType().name -ne "Object[]") { # Is the collection an array anyway?
write-host '$Computers wasnt an array but: ' $Computers.GetType()
}
foreach ($Computer in $Computers)
{
if (Test-Path "\$Computer\c$\Program Files (x86)\Common Files\Folder Name")
{
Write-Output "$($Computer): Folder Exists"
} else { # test-path fails. Write a warning and the path tried
write-host -nonewline "Fail: "
write-host "test-path \$Computer\c$\Program Files (x86)\Common Files\Folder Name"
}
}
批量分辨率:
在 Notepad++ 中打开文本文件,不知何故文件的编码设置为 'UCS-2 LE BOM' 而不是预期的 'UTF-8'。将编码设置为 UTF-8 并保存文件可解决批处理文件问题。
很多年前我遇到过同样的问题,所以我有点尴尬,因为我在发布之前没有想到这一点。
PowerShell 解析:
我最近开始使用 SCCM 模块,但没有注意到我的脚本将我的位置设置为 "PS $SCCMsiteCode:>" PSDrive。
只需在 ISE 控制台窗格中键入 'c:' 并按回车键,脚本就可以 return 预期的结果。
我认为当前位置不会阻止 Write-Output 写入控制台窗格。