繁体   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