从文本列表中测试 UNC 路径,并在每一行中写入其结果
Test UNC paths from a text list and write each line with its result
我有一个包含多个 DFS 共享的文本文件,如下所示:
\\computer1\dfsshare1
\\computer2\dfsshare2
\\computer3\dfsshare3
\\computer4\dfsshare4
\\computer5\dfsshare5
我想使用 Foreach-Object
函数对它们中的每一个进行 Test-Path
,然后列出每一行 + =
+ 它的结果(真或假)并添加一个 @
在行尾,全部显示在屏幕上。
我测试了以下内容:
$DFSList = "G:\DFS-MONITOR\DFS-List.txt"
Get-Content $DFSList | Foreach-Object {Write-host $_'=' Test-Path $_ '@'}
但无法让 Test-Path
在 Write-Host
之后工作(只是没有它)。
假设第 4 个 DFS 共享不可用,预期结果应该是这样的:
\computer1\dfsshare1=True@
\computer2\dfsshare2=True@
\computer3\dfsshare3=True@
\computer4\dfsshare4=False@
\computer5\dfsshare5=True@
在 PowerShell 中,如果要将 command/script/cmdlet 的 result/output 传递给另一个 command/script/cmdlet,您需要包装命令在括号中提供结果:
Write-Host $_ '=' (Test-Path $_) '@'
我有一个包含多个 DFS 共享的文本文件,如下所示:
\\computer1\dfsshare1
\\computer2\dfsshare2
\\computer3\dfsshare3
\\computer4\dfsshare4
\\computer5\dfsshare5
我想使用 Foreach-Object
函数对它们中的每一个进行 Test-Path
,然后列出每一行 + =
+ 它的结果(真或假)并添加一个 @
在行尾,全部显示在屏幕上。
我测试了以下内容:
$DFSList = "G:\DFS-MONITOR\DFS-List.txt"
Get-Content $DFSList | Foreach-Object {Write-host $_'=' Test-Path $_ '@'}
但无法让 Test-Path
在 Write-Host
之后工作(只是没有它)。
假设第 4 个 DFS 共享不可用,预期结果应该是这样的:
\computer1\dfsshare1=True@
\computer2\dfsshare2=True@
\computer3\dfsshare3=True@
\computer4\dfsshare4=False@
\computer5\dfsshare5=True@
在 PowerShell 中,如果要将 command/script/cmdlet 的 result/output 传递给另一个 command/script/cmdlet,您需要包装命令在括号中提供结果:
Write-Host $_ '=' (Test-Path $_) '@'