0

I'm a newbie at powershell/programming in general and currently attempting to update admin passwords on Chatsworth CPI PDUs. The PDUs are daisy-chained together and accessed through the primary/alternate PDU IPs. Each PDU has a unique identifier that is passed as a header. After validation the API returns a Sessionid that must also be passed with the identifier to other endpoints. The Sessionid is only valid for 10 minutes

I'm looking for a way to loop both the identifiers and Sessionid with the Invoke-RestMethod to update all of the passwords.

What would be the best method to accomplish this?

This is what I am currently running to test retrieving the sessionids

 #Unique Identifier for each PDU
$Identifier = @("80224", "80245", "80181", "76998", "80226", "80949", "80479", "80556", "86619", "86621", "80521", "80925", "80293", "80924", "76893", "80514", "80363", "81280", "80476")

#Loop each identifier to endpoint
$Sessionid = foreach ($identity in $Identifier) {
    <# $$header is the current item #>

    $Newheader = @{
        "PDUSelector" = $identity

    }

    

    Invoke-RestMethod -Uri $Login -Method Post -SkipCertificateCheck -Body $body -ContentType application/json -Headers $Newheader  



    
}
#Sessionids returned
$Sessionid.SessionID

This is an example of the sessionids that are returned: +KZWXna9xd2yvw== yL6FALD8n/5B2g== AZyCuSvqScOXTA== oPvKWulUCqpjzA== 8utMMVZ5o0aIag== VVY7QQcsCPPHCw== BRhvFvCLQGEkiQ== FmFMLZc/3ITQrQ== g+XrlAaImDcGAw== bXYRvNw0yRIDUw== mYTW3HtjT1Mopg== 3Bk9PX4bQXvp+g== sqZJGKO/SnrDdQ== COg4+s84jurHgA== mKL6a35Y3m5gnw== qqTjRHxtLV5sXw== skyV2hB9/dqJ8A== MolN0nt1rqBg/A== VhbAj/43I/yGyw==

1 Answers1

0

Something like this can get you started. If you wanted to multi thread each of the password updates, that would take a little more depending on which approach you want to take.

#Unique Identifier for each PDU
$Identifier = @('80224', '80245', '80181', '76998', '80226', '80949', '80479', '80556', '86619', '86621', '80521', '80925', '80293', '80924', '76893', '80514', '80363', '81280', '80476')

$splat = @{
    Uri = $Login
    Method = 'Post'
    SkipCertificateCheck = $true
    Body = $body
    ContentType = 'application/json'
}

#Loop each identifier to endpoint
$Sessionids = $Identifier | ForEach-Object { Invoke-RestMethod @splat -Headers @{ PDUSelector = $_ } }

# Given sessionIds ordinally match with $Identifier
$splat.Uri = $setPassword
$splat.Method = 'Put' # or whatever method is appropriate
$splat.Body = "Password=$password"

for( $ptr = 0; $ptr -lt $SessionIds.Count) {
    $headers = @{ 
               PDUSelector = $Identifier[$ptr]
               SessionId = $SessionIds[$ptr].SessionID 
               }
    Invoke-RestMethod @splat -Headers $headers
}
Jim
  • 692
  • 7
  • 15