Script the password for -Credential?

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 5 years, 11 months ago.

  • Author
    Posts
  • #5696

    by AevnsGrandpa at 2013-03-20 11:44:24

    Hope you all are not getting tired of my posts and yes I have done some searching to see if I can find a thread about what I need so......

    I am remoting into a number of servers using the New-PSSession cmdlet and pulling the server names from a text file. Is there anyway to script the password like I am doing with the account name? I have 14 servers in the list and gets a bit tiresome putting in the password 14 times.

    Thanks for the help!

    Jeff

    by coderaven at 2013-03-20 12:06:45

    Look in to the Get-Credential command and saving it to a variable

    $Creds = Get-Credential
    $ServerX = New-PSSession -ComputerName ServerX -Credential $Creds
    $ServerY = New-PSSession -ComputerName ServerY -Credential $Creds

    or
    $Creds = Get-Credential
    Invoke-Command -ComputerName ServerX, ServerY -Credentials $Creds -ScriptBlock { ... }

    If you search for Get-Credential and saving to file, there is a way to pull the password from file, just make sure you can do this in a secure way.

    by poshoholic at 2013-03-20 12:07:47

    Sure, that's easy.
    $username = 'foo'
    $password = 'bar'
    $credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username,(ConvertTo-SecureString -String $password -AsPlainText -Force)

    by poshoholic at 2013-03-20 12:09:44

    FWIW if you are running interactively, I prefer Allan's approach, and that's what I use in practice. But if you want to run unattended, you can put in a password without getting prompted for anything, and like Allan said, just make sure you're pulling the password from a secure location.

    by AevnsGrandpa at 2013-03-20 13:05:51

    Thanks and I want to run unattended, I am going from my domain joined laptop and wanting to run some scripts on a number of firedwalled workgrouped servers. I think Kirk's way will do it.

    Jeff

You must be logged in to reply to this topic.