[英]How can I export each line of a csv file with powershell
我目前正在遠離一個腳本,該腳本查詢Exchange Server(2007-2013)中的Message Queues wmi對象以僅在Powershell中運行。
下面是我的代碼,雖然不是最好的(我仍然是新手),但我已經在2007年和2013年版本的Exchange(在適當的地方進行了修改)上進行了測試,並且效果很好,它從初始導出的csv中提取每一行並設置滿足我尋找的結果:
每行一個CSV文件
但是我犯了一個菜鳥錯誤! 並非我們所有的客戶端服務器都會在它們的交換中容納4個消息隊列,這將在2和一個我不確定的變量之間變化,如果它在具有3個消息隊列的Exchange服務器上運行,那么您猜對了,因為我的dir子句僅配置了4個文件,所以我的結果會偏斜。
是否有任何PS專家可以幫助我了解有關此的代碼,並為我正確地指出成功導出行的正確方向,而不會遇到諸如此類的問題?
Add-PSSnapin Microsoft.Exchange.Management.Powershell.E2010
$output= "C:\Support\ExchangeQueue.csv"
$output1= "C:\Support\ExchangeQueue1.csv"
$output2= "C:\Support\ExchangeQueue2.csv"
$output3= "C:\Support\ExchangeQueue3.csv"
$Hubs= Get-ExchangeServer | Where {$_.ServerRole -like "*Hub*"}
$Hubs | foreach{
Get-Queue | Select-Object -Property Identity, MessageCount, NextHopDomain |
Export-Csv -path $output -NoTypeInformation
Get-Queue | Select-Object -Property Identity, MessageCount, NextHopDomain |
Export-Csv -path $output1 -NoTypeInformation
Get-Queue | Select-Object -Property Identity, MessageCount, NextHopDomain |
Export-Csv -path $output2 -NoTypeInformation
Get-Queue | Select-Object -Property Identity, MessageCount, NextHopDomain |
Export-Csv -path $output3 -NoTypeInformation
}
(Get-Content $output) | Foreach-Object {$_ -replace '"', ''}|Out-File $output
(Get-Content $output1) | Foreach-Object {$_ -replace '"', ''}|Out-File $output1
(Get-Content $output2) | Foreach-Object {$_ -replace '"', ''}|Out-File $output2
(Get-Content $output3) | Foreach-Object {$_ -replace '"', ''}|Out-File $output3
dir $output | %{
$content = gc $_.FullName
$outputfile = @($content | select -First 1 )
$outputfile += $content[1..($content.count -4)]
$outputfile | Set-Content $_.FullName -Force
(Get-Content $output |
Select-Object -Skip 1) | #Delete header
Set-Content $output
}
dir $output1 | %{
$content1 = gc $_.FullName
$outputfile1 = @($content | select -First 1 )
$outputfile1 += $content[2..($content.count -3)]
$outputfile1 | Set-Content $_.FullName -Force
(Get-Content $output1 |
Select-Object -Skip 1) | #Delete header
Set-Content $output1
}
dir $output2 | %{
$content2 = gc $_.FullName
$outputfile2 = @($content | select -First 1 )
$outputfile2 += $content[3..($content.count -2)]
$outputfile2 | Set-Content $_.FullName -Force
(Get-Content $output2 |
Select-Object -Skip 1) | #Delete header
Set-Content $output2
}
dir $output3 | %{
$content3 = gc $_.FullName
$outputfile3 = @($content | select -First 1 )
$outputfile3 += $content[4..($content.count -1)]
$outputfile3 | Set-Content $_.FullName -Force
(Get-Content $output3 |
Select-Object -Skip 1) | #Delete header
Set-Content $output3
}
我認為這里有很多邏輯是不必要的。 假設您在每台Exchange服務器上運行它,這就是我想寫的我想做的事情:
Add-PSSnapin Microsoft.Exchange.Management.Powershell.E2010;
$OutputFilePattern = 'C:\Support\ExchangeQueue{0}.csv';
$ServerRole = (Get-ExchangeServer).ServerRole;
if ($ServerRole -like '*HubTransport*' ) {
$AllQueues = Get-Queue | Select-Object -Property Identity, MessageCount, NextHopDomain;
$OutputFileNum = 0;
foreach ($Queue in $AllQueues) {
$OutputFileNum++;
$OutputFile = $OutputFilePattern -f $OutputFileNum;
#Buffer and Set-Content used here instead of Export-Csv due to quoting
$Buffer = @();
$Buffer += 'Identity,MessageCount,NextHopDomain';
$Buffer += '{0},{1},{2}' -f $Queue.Identity, $Queue.MessageCount, $Queue.NextHopDomain;
Set-Content -Path $OutputFile -Value $Buffer -Force;
}
}
您可能需要在其中的幾個對象上使用ToString()
方法,以使其正常工作。 即(Get-ExchangeServer).ServerRole.ToString()
,然后是$Queue.Identity.ToString(), $Queue.MessageCount.ToString(), $Queue.NextHopDomain.ToString()
。 我無法再運行這些cmdlet,因此我依賴MSDN文檔獲取其數據類型和參數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.