Link to home
Create AccountLog in
Avatar of E=mc2
E=mc2Flag for Canada

asked on

Change PowerShell script to encrypt the password for the site

I would like to make the script below secure so that the Password, and SSH Host Key Fingerprint are encrypted and cannot be seen by anyone. 

At the ery least the Password for the site I would like to be encrypted. 

Is this possible?

If yes what change would I make in order to enable this?

Thanks in advance.



param (
$localPath = "C:\Users\Users1\Folder1",
    $remotePath = "/folder_1/"
)
try
{
# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
      Protocol = [WinSCP.Protocol]::Sftp
  HostName = "****"
  UserName = "*****"
  Password = "*****"
  SshHostKeyFingerprint = "*****"
  PortNumber = "*****"
 }
    $session = New-Object WinSCP.Session
 try
 {
 # Connect
$session.Open($sessionOptions)
 # Synchronize files to local directory, collect results
 $synchronizationResult = $session.SynchronizeDirectories(
[WinSCP.SynchronizationMode]::Local, $localPath, $remotePath, $False)
# Deliberately not calling $synchronizationResult.Check
# as that would abort our script on any error.
    # Iterate over every download
foreach ($download in $synchronizationResult.Downloads)
{
 # Success or error?
 if ($download.Error -eq $Null)
 {
Write-Host "Download of $($download.FileName) succeeded, removing from
 source"
# Download succeeded, remove file from source
$filename = [WinSCP.RemotePath]::EscapeFileMask($download.FileName)
$removalResult = $session.RemoveFiles($filename)
if ($removalResult.IsSuccess)
 {
Write-Host "Removing of file $($download.FileName) succeeded"
}
else
{
 Write-Host "Removing of file $($download.FileName) failed"
}
else
{
Write-Host (
"Download of $($download.FileName) failed: $($download.Error.Message)")
 }
finally
{
# Disconnect, clean up
$session.Dispose()
 }
    exit 0
}
catch
{
 Write-Host "Error: $($_.Exception.Message)"
   exit 1
}

Open in new window


ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account