簡體   English   中英

Powershell For Loop 用於多台服務器

[英]Powershell For Loop for multiple servers

$SM_Module_username = "admin"
$SM_Module_password = "xxxxxx"
$user = $SM_Module_username
$pass= $SM_Module_password
$secpasswd = ConvertTo-SecureString $pass -AsPlainText -Force
$global:credential = New-Object System.Management.Automation.PSCredential($user, $secpasswd)
$expand_query ='?$expand=*($levels=1)'
$smmip = "172.128.35.56"

$uri = "https://$smmip/redfish/v1/Systems/System.Embedded.1"
$result = Invoke-WebRequest -Uri $uri -Credential $credential -Method Get -UseBasicParsing -ErrorVariable RespErr -Headers @{"Accept"="application/json"}
$get_host = $result.Content | ConvertFrom-Json
$get_host.HostName

$uri = "https://$smmip/redfish/v1/UpdateService/FirmwareInventory$expand_query"
$result = Invoke-WebRequest -Uri $uri -Credential $credential -Method Get -UseBasicParsing -ErrorVariable RespErr -Headers @{"Accept"="application/json"}
$get_fw_inventory = $result.Content | ConvertFrom-Json
$get_fw_inventory.Members | Where-Object {($_.Id -like "Installed*") -and ($_.Name -eq "Integrated Remote Access Controller")} | select Name, Version

盡管上面的代碼有效,但我希望使用 Powershell PSVersion 5.1.14409.1018對多個服務器運行。 有人可以幫我用foreach循環和hashtable修改腳本嗎? 擁有以下 output 會很有幫助。

Host        Name                                Version
----        ----                                -------
Host1       Integrated Remote Access Controller 2.70.70.70
Host2       Integrated Remote Access Controller 2.60.60.60
Host3       Integrated Remote Access Controller 2.60.60.60

由於您需要為每個服務器使用不同的 API,因此我將使用 IP/服務器作為鍵創建一個哈希表,並使用一個簡單的 IP 數組來檢索值並指定您的目標

$ApiTable = @{"10.10.10.10" = "api/for/server1"; "20.20.20.20" = "api/for/server2"; "30.30.30.30" = "api/for/server3"}

$Servers = ("10.10.10.10","20.20.20.20","30.30.30.30")

foreach($Server in $Servers)
{
    $uri = "https://$Server/$ApiTable.$Server"
}

要組合 2 個 API 調用的結果,您可以像這樣迭代服務器列表:

# create an array op server IPs
$servers = "172.128.35.56","172.128.35.57","172.128.35.58"

$SM_Module_username = "admin"
$SM_Module_password = "xxxxxx"
$user = $SM_Module_username
$pass= $SM_Module_password
$secpasswd = ConvertTo-SecureString $pass -AsPlainText -Force
$global:credential = New-Object System.Management.Automation.PSCredential($user, $secpasswd)
$expand_query ='?$expand=*($levels=1)'

foreach ($smmip in $servers) {
    $uri = "https://$smmip/redfish/v1/Systems/System.Embedded.1"
    $result = Invoke-WebRequest -Uri $uri -Credential $credential -Method Get -UseBasicParsing -ErrorVariable RespErr -Headers @{"Accept"="application/json"}
    $get_host = $result.Content | ConvertFrom-Json
    # remember this for constructing the PSObject later
    $hostname = $get_host.HostName

    $uri = "https://$smmip/redfish/v1/UpdateService/FirmwareInventory$expand_query"
    $result = Invoke-WebRequest -Uri $uri -Credential $credential -Method Get -UseBasicParsing -ErrorVariable RespErr -Headers @{"Accept"="application/json"}
    $get_fw_inventory = $result.Content | ConvertFrom-Json
    $get_fw_inventory.Members | Where-Object {($_.Id -like "Installed*") -and ($_.Name -eq "Integrated Remote Access Controller")} | ForEach-Object {
        [PsCustomObject]@{
            Host    = $hostname
            Name    = $_.Name
            Version = $_.version
        }
    }
}

希望這可以幫助

您可以執行以下操作:

# You will need to provide your IPs or FQDNs for your hosts below
$RespErr = $null
$smmips = '172.128.35.56','172.128.35.57','172.128.35.58','172.128.35.59'
$SM_Module_username = "admin"
$SM_Module_password = "xxxxxx"
$secpasswd = ConvertTo-SecureString $SM_Module_password -AsPlainText -Force
$global:credential = [pscredential]::new($SM_Module_username, $secpasswd)
$expand_query ='?$expand=*($levels=1)'

foreach ($smmip in $smmips) {
    $hosturi = "https://$smmip/redfish/v1/Systems/System.Embedded.1"
    $result = Invoke-WebRequest -Uri $hosturi -Credential $credential -Method Get -UseBasicParsing -ErrorVariable +RespErr -Headers @{"Accept"="application/json"}
    $get_host = $result.Content | ConvertFrom-Json
    
    $inventoryuri = "https://$smmip/redfish/v1/UpdateService/FirmwareInventory$expand_query"
    $result = Invoke-WebRequest -Uri $inventoryuri -Credential $credential -Method Get -UseBasicParsing -ErrorVariable +RespErr -Headers @{"Accept"="application/json"}
    $get_fw_inventory = $result.Content | ConvertFrom-Json
    $get_fw_inventory.Members |
        Where-Object {$_.Id -like "Installed*" -and $_.Name -eq "Integrated Remote Access Controller"} |
            Select-Object @{n='Host';e={$get_host.HostName}},Name, Version
}

解釋:

這里唯一不同的輸入是服務器的 IP。 因此,我們只需要遍歷 IP。 循環體僅包含每個 IP 不同的計算表達式。

使用foreach循環,我們遍歷集合$smmips ,每個當前項目為$smmip

由於您只需要一個 output 每次迭代具有三個屬性,因此我們只需要 output 數據和最終的Select-Object 為了合並前面表達式中的Host屬性,我們使用了一個名為Host的計算屬性。

我將您的錯誤變量$RespErr添加到腳本的開頭以將其初始化為$null 使用語法-ErrorVariable +RespErr將使$RespErr成為可能來自任何主機響應的錯誤集合(如果有的話)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM