2013-08-05 70 views
0

我正在使用一個PowerShell腳本,它將使用Export-Csv命令將一個列表od系統管理員輸出到CSV文件。該獲取數據的腳本的部分是:更改導出CSV輸出輸出格式

Foreach ($Computer in $Computers){ 
    $Online = Test-Connection -ComputerName $Computer -Quiet 
    if ($Online -eq "True"){ 
    $GroupName = Get-WmiObject win32_group -ComputerName $Computer | ? {$_.SID -eq 'S-1-5-32-544'} | Select-Object name -ExpandProperty name 
    $LocalGroup =[ADSI]"WinNT://$Computer/$GroupName" 
    $GroupMembers = @($LocalGroup.psbase.Invoke("Members")) 
    $Members = $GroupMembers | foreach {$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)} 
    foreach ($Member in $Members){ 
     $obj = New-Object System.Object 
     $obj | Add-Member -MemberType NoteProperty -Name "Computer" -Value $Computer 
     $obj | Add-Member -MemberType NoteProperty -Name "AdminGroupMembers" -Value $Member 
     $obj 
    } 
    } 
} 

} 

Get-Admins | Export-Csv -NoTypeInformation c:\scripts\adm.csv -Encoding UTF8 

電流輸出的格式如下所示:

"Computer1", "Admin1" 
"Computer1", "Admin2" 
"Computer1", "Admin3" 
"Computer1", "Admin4" 
"Computer2", "Admin1" 
"Computer2", "Admin2" 
"Computer3", "Admin1" 

我試圖讓輸出看起來像這樣:

"Computer1", "Admin1" , "Admin2" , "Admin3" , "Admin4" 
"Computer2", "Admin1" , "Admin2" 
"Computer3", "Admin1" , "Admin2" , "Admin3" 

任何想法?

回答

1

您的輸出格式不是CSV,因此Export-Csv不適合您。試試這個:

Get-Admins | group { $_.Computer } | % { 
    '{0},{1}' -f @($_.Group.Computer)[0], ($_.Group.AdminGroupMembers -join ',') 
} | Out-File 'output.csv' 

對於PowerShell的v2的你需要手動展開組屬性:

Get-Admins | group { $_.Computer } | % { 
    $computer = @($_.Group | select -Expand Computer)[0] 
    $admins = ($_.Group | select -Expand AdminGroupMembers) -join ',' 
    '{0},{1}' -f $computer, $admins 
} | Out-File 'output.csv' 
+0

我很欣賞你的建議,但該文件不輸出到CSV文件,只是沒有格式我會喜歡的。您上面的建議運行,但不提供CSV輸出,這是一個要求。 – user2654059

+0

@ user2654059現在確實如此。 –

+0

輸出文件是9行,每行有一個逗號,沒有其他數據。 #導出數據並提供反饋 #-------------------------------------- -------------------------------------------------- ----------------- Get-Admins |組{$ _。Computer} | %{ '{0},{1}'-f @($ _。Group.Computer)[0],($ _。Group.AdminGroupMembers -join',') } Out-File'c​​:\ scripts \ adm.csv' Write-Host「'n爲在線系統完成的腳本」 – user2654059