如何使用 powershell 导出 csv 文件的每一行

How can I export each line of a csv file with powershell

我目前正在从我们拥有的脚本转移到 运行 仅在 Powershell 中查询 Exchange Server (2007-2013) 中的消息队列 wmi 对象。

下面是我的代码,虽然不是最好的(我对此仍然是新手),我已经在 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 服务器上 运行ning 它,这是我认为您正在尝试做的事情的写法:

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 文档了解它们的数据类型和参数。