2016-11-14 29 views
0

当我连接到共享 “\\ IP_ADDRESS \” 文件夹,而不使用凭据我成功地连接:如何使用凭证创建PSDrive?

$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): The network path was not found

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


操作系统:Windows 10
PSVersion:5.0.10586.672
BuildVersion:10.0.10586.672
CLRVERSION:4.0.30319.42000
WSManStackVersion:3.0
PSRemotingProtocolVersion:2.3
SerializationVersion: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) 
+0

你可以尝试硬编码的网络路径只是为了测试它吗?所以网络路径中没有变量,只是一个字符串?或范围到“全球”? – 4c74356b41

+0

@ 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:未找到网络路径 – Pixar

+0

好,确实那条路径解决了吗? – 4c74356b41

回答

0

尝试传递一个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") 
+0

我得到了同样的错误:“网络路径找不到” – Pixar

+0

更新了我的答案,我没有确定你使用的是什么版本的PowerShell,但PowerShell 3+上的New-PSDrive支持证书。 – 4c74356b41

+0

对不起,忘记了包含这些信息。 PSVersion:5.0.10586.672 – Pixar

0

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

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 
} 
相关问题