Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows PowerShell give password in command Enter-PSSession

I am writing a project where I run a PowerShell script and create a new PSSession as follows:

PowerShell.exe -Command enter-pssession myUser -credential userName

When I run this, it opens a dialog to prompt the user for a password. However, I would prefer for the user to be able to enter the password along with the rest of the above line instead of having to be bothered with the prompt. I'm very new to PowerShell, and everything I've found in docs only gives ways to bring the prompt up for the password. Is this something that is possible to accomplish in PowerShell? Thanks!

like image 938
thnkwthprtls Avatar asked Mar 31 '14 14:03

thnkwthprtls


People also ask

How do you use PSSession in PowerShell?

Use a PSSession to run multiple commands that share data, such as a function or the value of a variable. To run commands in a PSSession, use the Invoke-Command cmdlet. To use the PSSession to interact directly with a remote computer, use the Enter-PSSession cmdlet. For more information, see about_PSSessions.

How do I enter-PSSession?

The Enter-PSSession cmdlet starts an interactive session with a single remote computer. During the session, the commands that you type run on the remote computer, just as if you were typing directly on the remote computer. You can have only one interactive session at a time.

How do I give credentials in PowerShell?

The first way to create a credential object is to use the PowerShell cmdlet Get-Credential . When you run without parameters, it prompts you for a username and password. Or you can call the cmdlet with some optional parameters.

How do I prompt for UserName and password in PowerShell?

You can use the credential object in security operations. The Get-Credential cmdlet prompts the user for a password or a user name and password. You can use the Message parameter to specify a customized message in the command line prompt.


1 Answers

Rahul gave you an answer that lets you avoid the password prompt altogether but at the risk of entering the user's password in clear text. Another option would be to allow the user to fill in their password in the popup box once but then save it so they never need to re-enter it.

To popup a prompt for username and password and save the result to a file:

 Get-Credential | Export-Clixml "mycredentials.xml"

Then you can either read that into a variable:

$cred = Import-Clixml "mycredentials.xml"
new-pssession -computername <computer> -credential $cred

or combine the two commands:

new-pssession -computername <computer> -credential (Import-Clixml "mycredentials.xml")

When you save the credentials to a file this way the password is securely encrypted using the currently logged-on user's login credentials.

like image 63
Duncan Avatar answered Sep 28 '22 06:09

Duncan