How to create PSDrive using credentials?

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)
powershell
share
remote-access
drive-mapping
asked on Stack Overflow Nov 14, 2016 by Pixar • edited Nov 14, 2016 by Pixar

2 Answers

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")
answered on Stack Overflow Nov 14, 2016 by 4c74356b41 • edited Nov 14, 2016 by 4c74356b41
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
}
answered on Stack Overflow Nov 14, 2016 by trebleCode • edited Oct 24, 2017 by Pang

User contributions licensed under CC BY-SA 3.0