Hello,
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 {
$session.Open($sessionOptions)
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)
$transferResult.Check()
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 {
$session.Dispose()
if ($netUseResult -like "*successful*") {
net use K: /delete
}
}