簡體   English   中英

從位於多個遠程服務器上的文本文件中選擇所需的內容並寫入以從執行腳本的位置登錄 csv

[英]pick required content from text file located on multiple remote servers and write to log in csv from where script is executed

我正在編寫一個腳本來從多個遠程服務器讀取文本文件,並從文本文件中獲取所需的詳細信息,將這些詳細信息寫入在執行腳本的 pc 上創建的 csv 日志文件。 我想添加 try、catch 和 if 條件以使我的腳本按預期工作。 要求如下:

  1. 從位於路徑下的遠程服務器讀取文件,
  2. 替換不需要的字符(這是通過代碼替換完成的)
  3. 使用 set-content 保存文本文件(已完成)
  4. 從文件中獲取所需的內容,存儲在數組變量中(完成)
  5. 在執行腳本的 PC 上創建的日志文件 (.csv) 中寫入內容。

問題是腳本獲取詳細信息,但是在嘗試寫入時記錄其未寫入並給出錯誤“無法索引到空數組”。

我的代碼如下:

$servers = gc .\servers.txt
$Global:ScriptDir = Split-Path $script:MyInvocation.MyCommand.Path
$date = (get-date).ToString("yyyyMMdd_HHmm")
$ILOContent = "$ScriptDir\ILOConfigData_$date.csv"
Add-Content -Path $ILOContent -Value "ILO_Name,ILO_Domain,Network_Details,ILO-TimeZone,Directory_Users,LDAP_Directory_Authentication,Directory_Groups,SNMP-Settings,Directory_Server_Address"


Foreach($server in $servers){

        Invoke-Command -ComputerName $server -Credential $Credential -ScriptBlock {

                    New-Item -Path "C:\Program Files\Hewlett Packard Enterprise\HPONCFG" -ItemType File -Name Current_ILOConfig.xml -Force| Out-Null
                    Set-Location "C:\Program Files\Hewlett Packard Enterprise\HPONCFG"
                    $ILODataPath = Get-Location
                    $WantFile = "$ILODataPath\Current_ILOConfig.txt"
                    $FileExists = Test-Path $WantFile
                    If ($FileExists -eq $True) {Remove-Item $WantFile }
                    Sleep(2)
                    Write-Host "Gathering current ILO configuration for $ENV:COMPUTERNAME" 
                    & "C:\Program Files\Hewlett Packard Enterprise\HPONCFG\hponcfg.exe" /a /w `
                      "C:\Program Files\Hewlett Packard Enterprise\HPONCFG\Current_ILOConfig.xml" |Out-Null
                    
                    Get-Content .\Current_ILOConfig.xml|Set-Content "Current_ILOConfig.txt"
                    Sleep(3)
                    $ILORAW_DATA = Get-Content .\Current_ILOConfig.txt

                    $ILORAW_DATA|ForEach-Object{
                                         $_ -replace '<!-- ' `
                                            -replace ' -->' `
                                            -replace '<' `
                                            -replace ' />' `
                                            -replace '"' `
                                            }|Set-Content .\Current_ILOConfig.txt

                    $ILO_DATA = Get-Content .\Current_ILOConfig.txt
                    Write-Host "Getting DNS details"
                    $DNS_NAME = $ILO_DATA |Where {$_ |Select-String -Pattern " DNS_NAME VALUE"," DOMAIN_NAME VALUE"}
                    $ILONAME = $DNS_NAME -split "="
                    $ServerILO = $ILONAME[1]
                    $ILONAME[3]

                    Write-Host "Getting Network details"
                    $NT = $ILO_DATA | where {$_ |Select-String -Pattern " IP_ADDRESS VALUE"," SUBNET_MASK"," GATEWAY_IP_ADDRESS"," PRIM_DNS_SERVER VALUE", " SEC_DNS_SERVER VALUE"," TER_DNS_SERVER VALUE" } 
                    $Network = [array]$NT -join "`n"
                    $Network.Trim()

                    Write-Host "Getting ILO TimeZone"
                    $TZ= $ILO_DATA |Where {$_ | Select-String -Pattern " TIMEZONE VALUE"}
                    $TimeZone =$TZ -Split "="
                    $TimeZone[1]

                    #DIRECT
                    Write-Host "Getting Directory User details"
                    
                    $DIR_USER = $ILO_DATA |Where {$_ | Select-String -Pattern " DIR_USER_CONTEXT"}
                    $User =@()
                    
                        foreach($Usr in $DIR_USER)
                        {
                         $User += ($Usr -split "VALUE=")[1]
                          
                         }$User ; $DIR_USERS = [Array]$User -join "`n"

                    
                    Write-Host "getting Global:ScriptDir location"
                    Set-Location $Global:ScriptDir
                    Get-Location
                    $Data = $ILONAME[1]+$ILONAME[3]+$Network,$Model,$TimeZone[1],$DIR_USERS
                    $Data|Select-Object $ILONAME[1],$ILONAME[3],$Network,$TimeZone[1],$DIR_USERS |Export-Csv -Append -Path $ILOContent -notypeinformation                                                        

 }
 }

正如所評論的,我認為使用文本方法解析 XML 文件中的屬性是一個壞主意。
最好讓 PowerShell 為您解析它並選擇您需要的屬性:

$date      = (Get-Date).ToString("yyyyMMdd_HHmm")
$scriptDir = Split-Path $MyInvocation.MyCommand.Path
$outFile   = "$ScriptDir\ILOConfigData_$date.csv"
$servers   = Get-Content -Path .\servers.txt

$result = foreach ($server in $servers) {
    Write-Host "Gathering current ILO configuration for '$server'" 
    Invoke-Command -ComputerName $server -Credential $Credential -ScriptBlock {
        & "C:\Program Files\Hewlett Packard Enterprise\HPONCFG\hponcfg.exe" /a /w `
          "C:\Program Files\Hewlett Packard Enterprise\HPONCFG\Current_ILOConfig.xml" | Out-Null
    
        # load the xml the hponcfg.exe just created (PowerShell will parse it for you)
        $config = New-Object -TypeName System.Xml.XmlDocument
        $config.Load("C:\Program Files\Hewlett Packard Enterprise\HPONCFG\Current_ILOConfig.xml")

        # preselect the nodes for DIR_USER_CONTEXT_1, DIR_USER_CONTEXT_2 etc.
        $userContext = $config.HPONCFG.MOD_DIR_CONFIG.ChildNodes | Where-Object {$_.Name -like 'DIR_USER_CONTEXT*' }

        # output a PSObject to be collected in variable $config
        [PsCustomObject] @{
            Server             = $env:COMPUTERNAME
            DNSName            = $config.HPONCFG.MOD_NETWORK_SETTINGS.DNS_NAME.VALUE
            DomainName         = $config.HPONCFG.MOD_NETWORK_SETTINGS.DOMAIN_NAME.VALUE
            IPAddress          = $config.HPONCFG.MOD_NETWORK_SETTINGS.IP_ADDRESS.VALUE
            SubnetMask         = $config.HPONCFG.MOD_NETWORK_SETTINGS.SUBNET_MASK.VALUE
            GateWay            = $config.HPONCFG.MOD_NETWORK_SETTINGS.GATEWAY_IP_ADDRESS.VALUE
            PrimaryDnsServer   = $config.HPONCFG.MOD_NETWORK_SETTINGS.PRIM_DNS_SERVER.VALUE
            SecondaryDnsServer = $config.HPONCFG.MOD_NETWORK_SETTINGS.SEC_DNS_SERVER.VALUE
            TertiaryDnsServer  = $config.HPONCFG.MOD_NETWORK_SETTINGS.TER_DNS_SERVER.VALUE
            TimeZone           = $config.HPONCFG.MOD_NETWORK_SETTINGS.TIMEZONE.VALUE
            UserContext        = $userContext.Value -join [environment]::NewLine
        } 
    }
}

$result | Export-Csv -Path $outFile -NoTypeInformation

暫無
暫無

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

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