【问题标题】:New-PSDrive "The network path was not found"New-PSDrive“找不到网络路径”
【发布时间】: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
}

Problem

这是整个脚本:

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 名称。

标签: powershell new-psdrive


【解决方案1】:

正如评论,$cred 中的用户可能有权访问服务器路径中的文件,但您似乎没有。

尝试使用Invoke-Command,您可以使用与您自己不同的凭据执行脚本块:

$cred = Get-Credential -Credential domain\name

$serverlist = Get-Content -Path "C:\file\serverlist.txt"
# loop through the list of servers and have these perform the action in the scriptblock
$result = foreach ($s in $serverlist) {
    Invoke-Command -ComputerName $s -Credential $cred -ScriptBlock {
        # you're running this on the server itself, so now use the LOCAL path
        $msg = if ((Get-Content 'E:\Updates\file\errors.txt' -Raw) -match '0') { 'ok' } else { 'nok' }
        # output 'ok' or 'nok'
        '{0} : {1}' -f $env:COMPUTERNAME, $msg
    }
}

# write to the results.txt file
# change 'Add-Content' in the next line to 'Set-Content' if you want to create a new, blank file
Get-Date -Format "dddd MM/dd/yyyy HH:mm K" | Add-Content -Path 'C:\file\results.txt'
$result | Add-Content -Path 'C:\file\results.txt'

实际上,您甚至不需要 foreach 循环,因为参数-ComputerName 可以接收服务器名称数组:

$result = Invoke-Command -ComputerName $serverlist -Credential $cred -ScriptBlock {
    # you're running this on the server itself, so now use the LOCAL path
    $msg = if ((Get-Content 'E:\Updates\file\errors.txt' -Raw) -match '0') { 'ok' } else { 'nok' }
    # output 'ok' or 'nok'
    '{0} : {1}' -f $env:COMPUTERNAME, $msg
}

【讨论】:

  • 非常感谢你的帮助队友!我明天会试一试(我不能 rn)并向您发送反馈。非常感谢您的帮助!
  • 你好@Theo。我用良好的路径和凭证尝试了你的脚本,但我仍然遇到问题:[****] Connecting to remote server **** failed with the following error message : The user name or password is incorrect. For more information, see the about_Remote_Troubleshooting Help topic. + CategoryInfo : OpenError: (*****:String) [], PSRemotingTransportException + FullyQualifiedErrorId : LogonFailure,PSSessionStateBroken。我确定这是正确的凭据,并且用户是计算机的管理员。非常感谢
  • @su7 请尝试$cred = Get-Credential -Message 'Please enter your admin credentials'。您现在提供的凭据被明确拒绝..
猜你喜欢
  • 1970-01-01
  • 2014-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-18
  • 2015-03-04
  • 2017-07-04
相关资源
最近更新 更多