0

当我在不使用凭据的情况下连接到共享的“\\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
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)
4

3 回答 3

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
}
于 2016-11-14T16:29:17.710 回答
0

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

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

应该

New-PSDrive -Name "test" -PSProvider FileSystem -Root "\\server.ru"
于 2021-11-03T16:22:58.340 回答
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")
于 2016-11-14T14:20:20.340 回答