Post a reply

Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)


Topic review


Re: Network drive cannot be mounted with PowerShell

Your code runs net use on the local machine. But you need to run it on the remote machine.
You might try using Session.ExecuteCommand.
Though as that runs the command in a different session, I'm not sure if that would have the desired effect.

Network drive cannot be mounted with PowerShell

I would like to transfer data using WinSCP in a PowerShell script.
The connection can be established and the transfer works.

But now I want the following...
The target server has a network drive mounted. On this drive the data should be transferred.

I try to mount the network drive using net use but I get the message that the drive cannot be mounted and the script stops.

If I establish the connection using the WinSCP GUI and open PuTTY the network drive can be mounted.

Can anyone help me?
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Sftp
    HostName = ""
    UserName = ""
    Password = ""
    SshHostKeyFingerprint = ""
    SshPrivateKeyPath = ""
$session = New-Object WinSCP.Session
try {
    if ($session.Opened) {
        $netUseResult = net use K: \\test\abc
        if ($netUseResult -like "*successful*") {
            $transferOptions = New-Object WinSCP.TransferOptions
            $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
            $transferResult = $session.PutFiles("C:\Temp\Test\*", "K:/abc", $False, $transferOptions)
            foreach ($transfer in $transferResult.Transfers) {
                Write-Host "Upload $($transfer.FileName) successful."
        } else {
            Write-Host "Network drive could not be connected."
    } else {
        Write-Host "Connection to WinSCP could not be established."
catch {
    Write-Host "An error has occurred: $($_.Exception.Message)"
finally {
    if ($netUseResult -like "*successful*") {
        net use K: /delete