In PowerShell, I have this file:
# Load environment variables from .env file
$envFile = ".env"
if (Test-Path $envFile) {
$envValues = Get-Content $envFile | ForEach-Object {
if ($_ -match '\s*([^=]+)\s*=\s*(.+)') {
$matches[1].Trim(), $matches[2].Trim()
}
}
$envHash = @{}
for ($i = 0; $i -lt $envValues.Length; $i += 2) {
$envHash[$envValues[$i]] = $envValues[$i + 1]
}
}
else {
Write-Error "Error: .env file not found"
Exit 1
}
# Load WinSCP .NET assembly
Add-Type -Path "C:\Users\vicen\AppData\Local\Programs\WinSCP\WinSCPnet.dll" # Replace with actual path to WinSCPnet.dll
#try {
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = $envHash["HOSTNAME"]
UserName = $envHash["USERNAME"]
PortNumber = [int] $envHash["PORT"]
#SshHostKeyFingerprint = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILH4qdAEbpQSMeOtgvaKE5MZHujoO5xyN8V+PdbWfWAz"
}
if ($envHash["PASSWORD"]) {
$sessionOptions.Password = ConvertTo-SecureString $envHash["PASSWORD"] -AsPlainText -Force
}
elseif ($envHash["PRIVATE_KEY_PATH"]) {
$sessionOptions.SshPrivateKeyPath = $envHash["PRIVATE_KEY_PATH"]
}
else {
Write-Error "Error: Password or private key path not provided in .env file"
Exit 1
}
$session = New-Object WinSCP.Session
try {
# Connect
$session.Open($sessionOptions)
# Upload files
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
$transferResult = $session.PutFiles($envHash["LOCAL_FILE"], $envHash["REMOTE_DIR"], $false, $transferOptions)
# Throw on any error
$transferResult.Check()
# Print results
foreach ($transfer in $transferResult.Transfers) {
Write-Host ("Upload of {0} succeeded" -f $transfer.FileName)
}
}
finally {
# Disconnect, clean up
$session.Dispose()
}
#}
#catch {
# Write-Host ("Error: {0}" -f $_.Exception.Message)
# Exit 1
#}
With the line that sets
SshHostKeyFingerprint
commented out, I get
SessionOptions.Protocol is Protocol.Sftp or Protocol.Scp, but SessionOptions.SshHostKeyFingerprint is not set.
However, if I set it, it says
The value supplied is not valid, or the property is read-only. Change the value, and then try again.
along with the previous error.
I cannot figure out what I'm doing wrong, tbh, I don't use PowerShell scripts that much, but anyway.
I patiently await your response, bye.