【问题标题】:How to create PSDrive using credentials?如何使用凭据创建 PSDrive?
【发布时间】:2017-03-28 05:10:30
【问题描述】:

当我在不使用凭据的情况下连接到共享的“\\ip_address\”文件夹时,我连接成功:

$driveInfo = New-PSDrive `
  -Name $driveName `
  -PSProvider "FileSystem" `
  -Root $targetDirectory `
  -Scope "Script"

当我为此命令指定凭据时:

$driveInfo = New-PSDrive `
  -Name $driveName `
  -PSProvider "FileSystem" `
  -Root $targetDirectory `
  -Credential (Get-Credential) ` # Now ask credentials from user.
  -Scope "Script"

我得到错误:

System.ComponentModel.Win32Exception (0x80004005):网络路径 没找到

这个错误是什么意思?
如何向用户询问凭据并使用它们来映射远程共享文件夹?


操作系统:Windows 10
PS版本:5.0.10586.672
构建版本:10.0.10586.672
CLRVersion:4.0.30319.42000
WSManStack 版本:3.0
PSRemotingProtocol版本:2.3
序列化版本:1.1.0.1


下面的命令很好:

New-PSDrive -Name "test" -PSProvider FileSystem -Root "\\server.ru\"

以下命令是错误的:

New-PSDrive -Name "test" -PSProvider FileSystem -Root "\\server.ru\" -Credential (Get-Credential)

【问题讨论】:

  • 你可以尝试硬编码网络路径来测试它吗?所以网络路径中没有变量,只是一个字符串?或范围为global
  • @4c74356b41:以下命令效果很好:"New-PSDrive -Name "test" -PSProvider FileSystem -Root "\\server.ru\builds\...\"。以下命令结果出现错误:“New-PSDrive -Name "test" -PSProvider FileSystem -Root "\\xz.avp.ru\ftp\builds2\VIISLA\" -Credential (Get-Credential)"。New-PSDrive :网络路径没有找到
  • 好吧,那条路径解决了吗?
  • 是的,该路径已解析。更重要的是 - 没有“-Credential”参数的命令有效:)

标签: powershell share remote-access drive-mapping


【解决方案1】:

我知道这是超级旧的,但问题是 -root 路径中的斜杠

New-PSDrive -Name "test" -PSProvider FileSystem -Root "\\server.ru\"

应该是

New-PSDrive -Name "test" -PSProvider FileSystem -Root "\\server.ru"

【讨论】:

    【解决方案2】:

    我希望我的功能符合您的需求。

    Function ConnectTo-Domain()
        {
            $Domain = "my.domain.com"
            $DomainName = "MYDOMAIN"
            $userCred = $ENV:USERNAME
    
        do
        {
            # Get Domain Controllers of target domain
    
            $targetdomaindc = Get-ADDomainController -DomainName $Domain -Discover
            $targetdcname = $($targetdomaindc.hostname)
    
            # Authenticate with user providing password credential
    
            Write-Host "`nEnter $Domain credentials to get available Domain Controllers" -ForegroundColor Yellow
            $DCs = Get-ADDomainController -Filter * -Server $targetdcname -Credential (Get-Credential $Domain\$userCred)
        }
        until ($DCs -ne $NULL)
    
        $i = 0
    
        do
        {
            # Check that the target Domain Controller is available
    
            $testConnection = Test-Connection -ComputerName $DCs.Name[$i] -Count 2
            $currentDC = $DCs.Name[$i]
            $i++
        }
        until($testConnection -ne $NULL)
    
        # Check if an existing PSDrive exists and create if not
    
        $checkDrives = Get-PSDrive
        if ($checkDrives.Name -notcontains "$DomainName")
        {
            Write-Host "Enter $Domain credentials to connect to an available Domain Controller" -ForegroundColor Yellow
            New-PSDrive -Name $DomainName -PSProvider ActiveDirectory -Server $currentDC -Credential (Get-Credential $Domain\$userCred) -Root "//RootDSE/" -Scope Global
        }
    
        $DomainDriveName = $DomainName + ":\"
        cd $DomainDriveName
    }
    

    【讨论】:

      【解决方案3】:

      尝试传入一个 PSCredentials 对象:

      $username = "domain01\admin01"
      $password = cat C:\securestring.txt | convertto-securestring
      $cred = new-object -typename System.Management.Automation.PSCredential `
               -argumentlist $username, $password
      

      所以-Credential $cred
      或使用:

      $net = new-object -ComObject WScript.Network
      $net.MapNetworkDrive("u:", "\\server\share", $false, "domain\user", "password")
      

      【讨论】:

      • 我得到同样的错误:“找不到网络路径”。
      • 更新了我的答案,我不确定您使用的是哪个版本的 PowerShell,但 PowerShell 3+ 上的 New-PSDrive 支持凭据
      • 抱歉,忘记包含该信息。 PS版本:5.0.10586.672
      • 你不能回退到comobject的东西吗?
      猜你喜欢
      • 2013-06-06
      • 1970-01-01
      • 2019-06-09
      • 1970-01-01
      • 1970-01-01
      • 2021-09-17
      • 1970-01-01
      • 2019-03-30
      • 1970-01-01
      相关资源
      最近更新 更多