2017-05-31 3 views
0

私はちょうどCSVに次のものをエクスポートしようとしているのですが、Export-CSVForeach-Objectループ内に入れてみました。Powershell Export-CSV

第1列にサーバー名$serverを、第2列に説明$descriptionを入れたいとします。

以前のバージョンでは、Out-File -append .\outa.txt -InputObject $_.Name,$descriptionを使用してテキストファイルを作成できましたが、フォーマットがうまく機能しませんでした。

$server = Get-ADComputer -Filter {OperatingSystem -Like "*Server*" -and Name -Notlike "*DOM*"} 
$server | ForEach-Object { 
    $Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine',$_.Name) 
    $RegKey = $Reg.OpenSubKey("SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters") 
    $description = $RegKey.GetValue("srvcomment") 
} | Export-Csv .\out1.csv 

答えて

1

ForEachとしてForEach-Objectに反対する:

$server = Get-ADComputer -Filter {OperatingSystem -Like "*Server*" -and Name -Notlike "*DOM*"} 

foreach($s in $server){ 

    # replaced $_ with $s 
    $Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine',$s.Name) 
    $RegKey= $Reg.OpenSubKey("SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters") 
    $description = $RegKey.GetValue("srvcomment") 

    # casting as array is required to use += 
    # disclaimer: the array is destroyed and rebuilt on each run. 
    [array]$myData += New-Object psobject -Property @{ 
     Server  = $s.Name 
     Description = $description 
    } 
} 

# If you want a CSV without the top info line, use the notypeinfo switch 
# Select-Object gives you the column order you want. 
$myData | Select-Object Server,Description | Export-Csv .\out1.csv -NoTypeInformation 

編集 - コメントの答え
は、配列を作成せずに、ループ内からそれを行うことが可能であるが、オブジェクトが必要ですby Export-Csv AFAIK。

ForEach-Object

$server = Get-ADComputer -Filter {OperatingSystem -Like "*Server*" -and Name -Notlike "*DOM*"} 
$server | ForEach-Object { 
    $Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine',$_.Name) 
    $RegKey = $Reg.OpenSubKey("SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters") 
    New-object psobject -Property @{Server=$_.Name;Description=$RegKey.GetValue("srvcomment")} | Export-Csv .\out1.csv -NoTypeInformation 
} 

ForEach

$server = Get-ADComputer -Filter {OperatingSystem -Like "*Server*" -and Name -Notlike "*DOM*"} 
foreach($s in $server){ 
    $Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine',$s.Name) 
    $RegKey= $Reg.OpenSubKey("SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters") 
    New-object psobject -Property @{Server=$s.Name;Description=$RegKey.GetValue("srvcomment")} | Export-Csv .\out1.csv -NoTypeInformation 
} 

あなたが値に問題がある場合は、ブラケット/部分式は助けることができる:

New-object psobject -Property @{Server=($_.Name);Description=($RegKey.GetValue("srvcomment"))} | Export-Csv .\out1.csv -NoTypeInformation 
New-object psobject -Property @{Server=$($_.Name);Description=$($RegKey.GetValue("srvcomment"))} | Export-Csv .\out1.csv -NoTypeInformation 
+0

これは私にとってうまくいきます。それは配列を作成せずにそれを行うことは可能ですか?余分なステップのように思えます - ループ中に出力を置く方法がなければなりません。 – yoscar

+0

@yoscar編集を参照してください。最後に 'ForEach-Object'も使いこなしました – gms0ulman

0

あなたのForEach-Objectはステートメントのみが含まれています(変数の割り当て)。それは何も戻っていない。何かを返す必要があります。それ以外の場合は、空のパイプラインをExport-CSVに渡しています。

私はあなたが実際に何をしたいのか、代わりに$Regと呼ばれる変数、$RegKey、および$descriptionを設定する(そしてそれらの値を使用したことがない)の、ということを推測しているCSVの列を作成しているがRegRegKey、およびdescriptionと呼ばれます。その場合、ループのたびに[PSCustomObject](これらのプロパティが追加されている)を生成する必要があります。

$server = Get-ADComputer -Filter {OperatingSystem -Like "*Server*" -and Name -Notlike "*DOM*"} 
$server | ForEach-Object { 
    [PSCustomObject] @{ 
     Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine',$_.Name) 
     RegKey = $Reg.OpenSubKey("SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters") 
     description = $RegKey.GetValue("srvcomment") 
    } 
} | Export-Csv .\out1.csv