1

I'm trying to create a powershell script to allow my kids to reboot my Raspberry Pi from their Windows computer if need be. I've tested everything and have gotten it to work, but the only hitch is that it's prompting for a username and password. I realize the line that's doing it is:

New-SSHSession -ComputerName "myPi" -Credential (Get-Credential)

I've done some searching, but I can't seem to figure out if it's possible to replace the "(Get-Credential)" section to automatically enter the username/password.

And yes, I'm aware of the security risks. They could do much more damage to the Windows machine than they could ever do on the Pi, and the settings on the Pi are very easily restored, so no worries from my end.

3 Answers 3

5

Something like this should work:

$user = "someuser"
$pass = ConvertTo-SecureString -String "somepassword" -AsPlainText -Force
$creds = new-object -typename System.Management.Automation.PSCredential -argumentlist $user,$pass
New-SSHSession -ComputerName "myPi" -Credential $creds
Sign up to request clarification or add additional context in comments.

Comments

2

You could also call a file that has the password encrypted in it. Note this can only be decrypted by the account it was generated on on the computer it was generated on.

$pass = "Password"
$Username = "Username"
$outfile = "c:\filelocation.xml"


$secureStringPwd = $pass | ConvertTo-SecureString -AsPlainText -Force
$credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList ($Username,$secureStringPwd)
$credential | Export-CliXml -Path $OutFile

Addressing Bill.

Correct, hard coding the password in the script is bad practice. Below is how I would change the first portion.

The above came from a custom script that's purpose was to create many cred accounts off a input json is why I wrote it that way.

 $outfile = "c:\filelocation.xml"
 Get-Credential | export-clixml -path $OutFile

You then can call the file in your script like so but this has to be done on the same user and computer that the creds file was generated on.

 $Creds = Import-Clixml -Path "c:\file.xml"
 New-SSHSession -ComputerName "myPi" -Credential $creds

Good point Edited -argumentlist.

2 Comments

+1; quibble: please don't use method syntax in your New-Object call, because that only works accidentally: New-Object -TypeName System.Management.Automation.PSCredential($Username,$secureStringPwd) is really New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList ($Username, $secureStringPwd) or, in it shortest argument-mode form: New-Object -TypeName System.Management.Automation.PSCredential $Username, $secureStringPwd
It's a terrible idea to hard-code a password in plain-text in a script file.
0

Another option could be to do a 1 time setup with get-credential then convert the password to plaintext using convertfrom-securestring and then in the file you can take your password plaintext secure string and so something similar to the other answers:

$user = "someuser"
$pass = "YOUR LONG PASSWORD GUID FROM ABOVE" | convertTO-securestring
$creds = new-object -typename System.Management.Automation.PSCredential -argumentlist $user,$pass
New-SSHSession -ComputerName "myPi" -Credential $creds

This lets you do a one time setup, but avoids having multiple files or having your password appear in a readable way in the script.

If you go this way you need to do the setup FROM the account that will run the script ON the machine that will run the script, because it uses those for the encryption as far as I know.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.