添加"非标准"迭代ArrayList的值

时间:2018-05-22 07:39:17

标签: powershell

我目前正在编写脚本以从远程服务器列表中检索注册表项并导出为CSV。

我已经得到了这个,这是有效的(我稍后会去清理错误处理),但是输出并不是我想要生成的,因为它没有很好地导出到CSV。

$DataList = New-Object System.Collections.ArrayList
foreach ($Target in $ImportData) {
    Write-Host "Scanning" $Target.ComputerName ":" -NoNewline
    $RegData = Get-RegValue -ComputerName $($Target.ComputerName) -Hive $RegHive -Key $KeyName
    if (!$RegData) {
        Write-Host "No data found for" $Target.ComputerName
        [void]$DataList.Add([PSCustomObject]@{
            ComputerName = $Target.ComputerName
            Hive ="No Data"
            Key = ""
            Value = ""
            Data = ""
            Type = ""
        })
    } else {
        $DataList.Add($RegData)
        Write-Host "Done"
    }
}
$DataList

输出如下:

ComputerName Hive            Key                  Value                     Data
------------ ----            ---                  -----                     ---
SERVER1   LocalMachine    SOFTWARE\CUSTOM\Serv... AssetNumber               987
SERVER1   LocalMachine    SOFTWARE\CUSTOM\Serv... BuildDate                 04/
SERVER1   LocalMachine    SOFTWARE\CUSTOM\Serv... iLODefaultPwd             NA
SERVER1   LocalMachine    SOFTWARE\CUSTOM\Serv... OwnerContactDetails       S G
SERVER1   LocalMachine    SOFTWARE\CUSTOM\Serv... OwnerName                 MS
SERVER1   LocalMachine    SOFTWARE\CUSTOM\Serv... OwnerPhone                643
SERVER1   LocalMachine    SOFTWARE\CUSTOM\Serv... PatchAutoManual           Aut
SERVER1   LocalMachine    SOFTWARE\CUSTOM\Serv... ServerRole                Man
SERVER1   LocalMachine    SOFTWARE\CUSTOM\Serv... ServerType                Vir
SERVER1   LocalMachine    SOFTWARE\CUSTOM\Serv... WarrantyExpires           NA

Value        :
Key          :
Data         :
Hive         : None found
Type         :
ComputerName : SERVER2

SERVER3     LocalMachine    SOFTWARE\CUSTOM\Serv... buildDate                 15/
SERVER3     LocalMachine    SOFTWARE\CUSTOM\Serv... OperatingSystem           Win
SERVER3     LocalMachine    SOFTWARE\CUSTOM\Serv... OwnerContactDetails       M/S
SERVER3     LocalMachine    SOFTWARE\CUSTOM\Serv... OwnerName                 GM
SERVER3     LocalMachine    SOFTWARE\CUSTOM\Serv... OwnerPhone                131
SERVER3     LocalMachine    SOFTWARE\CUSTOM\Serv... PatchAutoManual           -
SERVER3     LocalMachine    SOFTWARE\CUSTOM\Serv... ServerRole                SNB
SERVER3     LocalMachine    SOFTWARE\CUSTOM\Serv... ServerType                Phy
SERVER3     LocalMachine    SOFTWARE\CUSTOM\Serv... WarrantyExpires           -

我怀疑这两种对象类型不同,但我不会编写太多脚本,并且对于如何使我的自定义条目与自动检索的条目相匹配,我有一个完整的心理障碍。

1 个答案:

答案 0 :(得分:1)

Get-RegValue(假设您正在使用RemoteRegistry module中的cmdlet)返回RegistryValue个对象,而PSCustomObject类型加速器显然会生成PSCustomObject对象。您可以使用Get-Member cmdlet检查类型。

您可以通过Select-Object管道列表来避免此问题。我也不会首先使用ArrayList。只需在变量中收集循环输出。

$DataList = foreach ($Target in $ImportData) {
    $RegData = Get-RegValue ...
    if ($RegData) {
        $RegData
    } else {
        [PSCustomObject]@{
            ComputerName = $Target.ComputerName
            Hive         = 'No Data'
            Key          = ''
            Value        = ''
            Data         = ''
            Type         = ''
        }
    }
}

$DataList |
    Select-Object ComputerName, Hive, Key, Value, Data, Type |
    Export-Csv ...

Select-Object步骤将所有输入对象转换为具有所选属性的自定义对象。