Connecting multiple servers with different password.

This topic contains 1 reply, has 2 voices, and was last updated by  Sam Boutros 3 weeks ago.

  • Author
    Posts
  • #100230

    raghavendra
    Participant

    Hello Team

    I am trying to connect multiple UCS servers using UCS powertool. Some servers has different password.How to connecting them in a loop and the script should try both the password's, which I mentioned in the credential file. Please find below the sample code.

    $ucsservers = get-content c:\scripts\servers.txt

    $user = "admin"
    $password = "password" | ConvertTo-SecureString -AsPlainText -Force
    $password1 = "password1234" | ConvertTo-SecureString -AsPlainText -Force
    $cred = New-Object System.Management.Automation.PSCredential($user, $password)
    $cred1 = New-Object System.Management.Automation.PSCredential($user, $password1)

    foreach($ucs in $ucservers){
    If (Connect-ucs -Name $ucs -nodefault -crediantial $cred)

    else

    }}

    Please advise which loop I need to use and how to do catch the connected or disconnected(password wrong) errors..

    Thanks
    Raghav

  • #100233

    Sam Boutros
    Participant
    #Requires -Version 5
    Install-Module POSH-SSH,SB-Tools -Force
    Import-Module POSH-SSH,SB-Tools -DisableNameChecking
    
    $ucsservers = Get-Content c:\scripts\servers.txt
    $CredList = 1..2 | foreach { Get-SBCredential 'admin' }
    
    $myOutput = foreach($ucs in $ucservers){
        Remove-Variable ValidCred -EA 0 
        0..1 | foreach {
            If (Connect-ucs -Name $ucs -nodefault -crediantial $CredList[$PSItem] -EA 0) {
                $ValidCred = $CredList[$PSItem]
            } 
        }
        [PSCustomObject][Ordered]@{
            ComputerName = $ucs
            UserName     = $ValidCred.UserName
            Password     = $ValidCred.GetNetworkCredential().Password
        }
    }
    
    $myOutput | FT -a 
    

You must be logged in to reply to this topic.