Using credentials with script RRS feed

  • Question

  • Hi

    We are using a script and has to pass different credentials for each server, we use below method to collect the credentials 

    $PWD1=Read-Host -assecure 
    $PWD1 | ConvertFrom-SecureString

    With a CSV file we use header Server,Username,Password and then mention each server name, user name and Credential from $PWD1 for each accounts. With the script below are the main section that uses these credentials


    $Computers=Import-csv "D:\PSS\Systems.csv"

    ForEach ($system in $computers) 
    $server = $computer.server
    $username = $computer.username$passwd = $system.password | ConvertTo-SecureString
    $cred = New-Object System.Management.Automation.PSCredential -argumentlist $username,$passwd
    $NICs = GWMI -comp $server Win32_networkadapterconfiguration -Credential $cred -ErrorAction Stop | Where-Object {$_.IPEnabled -eq $TRUE -and $_.DHCPEnabled -eq $False}

    Here we want to know whether this is the secured way of passing credentials or there a better option available for doing this?

    Thanks in advance


    • Moved by Bill_Stewart Monday, January 7, 2019 8:15 PM Unanswerable drive-by question
    Tuesday, November 13, 2018 12:43 PM

All replies

  • Please fix your post.  Code is broken and wrapped onto many lines.  Post code with the code posting tool provided.

    Yes.  This is the preferred method of passing credentials.

    ErrorAction Stop is pointless without a Try/Catch

    Here is the correct way to call and filter with WMI:

    $NICs = Get-WmiObject Win32_networkadapterconfiguration -Filter 'IPEnabled=True and DHCPEnabled=False' -comp $server  -Credential $cred  

    In a domain there is no need to use credentials.  Just run script as a domain admin account.


    Tuesday, November 13, 2018 3:37 PM