0

When I connect to shared "\\ip_address\" folder without using credentials I connect successfully:

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

When I specify credentials for this command:

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

I get error:

System.ComponentModel.Win32Exception (0x80004005): The network path was not found

What does this error mean?
How can I ask credentials from user and use them to map the remote shared folder?


OS: 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


The following command is good:

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

The following command is bad:

New-PSDrive -Name "test" -PSProvider FileSystem -Root "\\server.ru\" -Credential (Get-Credential)
Egor Okhterov
  • 478
  • 2
  • 9
  • 34
  • can you try to hardcode the network path just to test it? so no variable in the network path, just a string? or scope to ``global``? – 4c74356b41 Nov 14 '16 at 14:38
  • @4c74356b41: The following command works good: "New-PSDrive -Name "test" -PSProvider FileSystem -Root "\\server.ru\builds\...\". The following command results in a error: "New-PSDrive -Name "test" -PSProvider FileSystem -Root "\\xz.avp.ru\ftp\builds2\VIISLA\" -Credential (Get-Credential)". New-PSDrive : The network path was not found – Egor Okhterov Nov 14 '16 at 14:51
  • well, does that path resolve? – 4c74356b41 Nov 14 '16 at 14:52
  • Yes, that path resolves. Even more - the command without "-Credential" parameter works :) – Egor Okhterov Nov 14 '16 at 14:59

3 Answers3

1

I know this is super old but the issue is the trailing slash in the path for -root

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

should be

New-PSDrive -Name "test" -PSProvider FileSystem -Root "\\server.ru"
Joshua Pape
  • 11
  • 1
  • 3
0

Try passing in a PSCredentials object:

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

so -Credential $cred
or use:

$net = new-object -ComObject WScript.Network
$net.MapNetworkDrive("u:", "\\server\share", $false, "domain\user", "password")
4c74356b41
  • 69,186
  • 6
  • 100
  • 141
0

I hope my function fits your needs.

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
}
Pang
  • 9,564
  • 146
  • 81
  • 122
trebleCode
  • 2,134
  • 19
  • 34