Sometimes I need to re-use old PowerShell snippets, but I had difficulties to find it out again, so it might be a good idea to put all of this here.

 

Connecting to remote hosts without dropping passwords clear text inside scripts

First thing, I encrypted the password in a text file for later use, by

read-host -prompt "Enter password to be encrypted in mypassword.txt" -assecurestring | convertfrom-securestring | out-file '.\securestring.txt'

This way, I can connect to remote systems without having to specify clear passwords, like this:

$pass = cat '.\securestring.txt' | convertto-securestring
$mycred = new-object -typename System.Management.Automation.PSCredential -argumentlist "Admin@domain",$pass

 

Copy files using BITS

Copy big files between systems without affecting too much to network performance or consuming bandwidth it’s a very important thing, and I had good results with BITS

Import-Module BitsTransfer
Start-BitsTransfer -Source $Source -Destination $Destination -DisplayName "Transferring file"

 

You can even wait until finishing the copy

Import-Module BitsTransfer
# Start the BITS transfer. Added a loop to wait until complete
$job = Start-BitsTransfer -Source $Source -Destination $Destination -Asynchronous -DisplayName "Transferring file"
While ($job | Where-Object { $job.JobState -eq "Transferring"}) {
    Sleep -Seconds 1
}


Leave a Reply

%d bloggers like this: