【发布时间】:2021-11-26 09:40:04
【问题描述】:
我在使用“New-PSDrive -Root”时遇到问题
当我尝试使用数组将 New-PSDrive -Root $patch 与 $path 映射时,cmd 不会映射驱动器但给我一个错误:“找不到网络路径”。
如果我在我的 Windows 中使用资源管理器,则该路径有效。
我该如何解决这个问题?
非常感谢
示例:
foreach ($s in $serverlist)
{
$path = "\\$s\e$\Updates\file\
New-PSDrive -Name "S" -Root $path -Persist -PSProvider "FileSystem" -Credential $cred
}
这是整个脚本:
Get-Date -Format "dddd MM/dd/yyyy HH:mm K">> "C:\file\results.txt"
$cred = Get-Credential -Credential domain\name
$serverlist = @(get-content -Path "C:\file\serverlist.txt")
foreach ($s in $serverlist)
{
$path = "\\$s\e$\Updates\file\"
New-PSDrive -Name "S" -Root $path -Persist -PSProvider "FileSystem" -Credential $cred
$path2 = "\\$s\e$\Updates\file\errors.txt"
$file = Get-Content $path2
$containsWord = $file | %{$_ -match "0"}
if ($containsWord -contains $true) {
Out-File -FilePath "C:\file\results.txt" -Append -InputObject "$s : ok"
} else {
Out-File -FilePath "C:\file\results.txt" -Append -InputObject "$s : nok"
}
Remove-PSDrive -Name "S"
}
编辑 1:如果我尝试通过具有相同凭据的 Windows 资源管理器直接访问该文件,然后我运行脚本,它就可以工作
【问题讨论】:
-
$path本身返回什么? -
[1] 向我们展示您的一些 serverlist.txt 文件,因为现在我们不知道
"\\$s\e$\Updates\file\"读取的内容。 [2] 你为什么要在其余代码中不使用映射时尝试进行映射? [3] 用户domain\name是否允许访问驱动e$? -
@AbrahamZinala $path 返回正确的路径:\\server1\e$\update\file\
-
@Theo 用户 $cred 有权访问驱动 e$。映射驱动器让我可以访问 Get-Content "\\$s\e$\Updates\file\errors.txt" 中的日志。
-
@su7
"\\$s\e$\Updates\file\errors.txt"是 UNC 名称。同样,您没有使用映射S:\errors.txt,因此我会将其完全排除在代码之外并始终使用 UNC 名称。