Powershell Newline Character无效我试过`n"`n" '`ñ'

时间:2017-09-27 18:54:07

标签: powershell newline powershell-v2.0 powershell-v3.0 cpu-usage

**我试过n " n" '`ñ'

  

块引用

**

GC D:\code\ServerList.txt | % {
$Comp = $_
#write-output "server Information"
If (Test-Connection $Comp -Quiet){
$Luser = (Get-WmiObject -class win32_process -Filter 
"Name='Explorer.exe'" -ComputerName $Comp | % {$_.GetOwner().User} | 
Sort-Object -Unique) -join ","
$Mem = GWMI -Class win32_operatingsystem -computername $COMP
  New-Object PSObject -Property @{
            "ServerInfo" = ""
            Server = $Comp
            "CPU usage" = "$((GWMI -ComputerName $COMP win32_processor 
| Measure-Object -property LoadPercentage -Average).Average) %"
            "Memory usage" = "$("{0:N2}" -f 
((($Mem.TotalVisibleMemorySize - $Mem.FreePhysicalMemory)*100)/ 
$Mem.TotalVisibleMemorySize)) %"
            "Total FreeSpace" = "$("{0:N2}" -f ((Get-WmiObject -Class 
win32_logicaldisk -ComputerName $COMP -Filter "DriveType = '3'" |
              Measure-Object -property FreeSpace -Sum).Sum /1GB)) GB"
            "DiskSpace" = "$("{0:N2}" -f ((Get-WmiObject -Class 
win32_logicaldisk -ComputerName $COMP -Filter "DriveType = '3'" |
              Measure-Object -property Size -Sum).Sum /1GB)) GB"
        "Comment" =  ""
  "logged Users" = $Luser
  }
}
Else{
"" | Select @{N="Server";E={$Comp}},"CPU usage","Memory usage","Total 
FreeSpace","logged Users","DiskSpace"
}
}| Select "ServerInfo",Server,"logged Users","CPU usage","Memory 
usage","Total FreeSpace" ,"DiskSpace", "Comment" |
Export-Csv "D:\code\Diskncpu.csv" -nti –Append

输出
enter image description here

期望的输出
enter image description here

4 个答案:

答案 0 :(得分:2)

"`r`n"

我相信需要双引号。

答案 1 :(得分:1)

使用[System.Environment]::NewLine在您需要的任何地方添加新行。

说过为了清晰起见我格式化了代码并针对数组执行了

@("MECDEVAPP01","MECDEVAPP01")| % {
    $Comp = $_
    #write-output "server Information"
    If (Test-Connection $Comp -Quiet){
        $Luser = (Get-WmiObject -class win32_process -Filter "Name='Explorer.exe'" -ComputerName $Comp | % {$_.GetOwner().User} | Sort-Object -Unique) -join ","
        $Mem = GWMI -Class win32_operatingsystem -computername $COMP
        New-Object PSObject -Property @{
            "ServerInfo" = ""
            Server = $Comp
            "CPU usage" = "$((GWMI -ComputerName $COMP win32_processor | Measure-Object -property LoadPercentage -Average).Average) %"
            "Memory usage" = "$("{0:N2}" -f ((($Mem.TotalVisibleMemorySize - $Mem.FreePhysicalMemory)*100)/ $Mem.TotalVisibleMemorySize)) %"
            "Total FreeSpace" = "$("{0:N2}" -f ((Get-WmiObject -Class win32_logicaldisk -ComputerName $COMP -Filter "DriveType = '3'" | Measure-Object -property FreeSpace -Sum).Sum /1GB)) GB"
            "DiskSpace" = "$("{0:N2}" -f ((Get-WmiObject -Class win32_logicaldisk -ComputerName $COMP -Filter "DriveType = '3'" | Measure-Object -property Size -Sum).Sum /1GB)) GB"
            "Comment" =  ""
        "logged Users" = $Luser
      }
    }
    Else{
        "" | Select @{N="Server";E={$Comp}},"CPU usage","Memory usage","Total FreeSpace","logged Users","DiskSpace"
    }
}| Select "ServerInfo",Server,"logged Users","CPU usage","Memory usage","Total FreeSpace" ,"DiskSpace", "Comment"|
Export-Csv "C:\Users\asarafian\Downloads\Diskncpu.csv" -nti –Append

csv文件就像这样

"ServerInfo","Server","logged Users","CPU usage","Memory usage","Total FreeSpace","DiskSpace","Comment"
"","MECDEVAPP01","","7 %","70,24 %","203,97 GB","278,36 GB",""
"","MECDEVAPP01","","0 %","70,25 %","203,97 GB","278,36 GB",""

这是我对将记录集(即您使用所有这些管道构建的内容)转换为csv所期望的内容。

我想生成格式化文本然后你不能使用csv,或者你需要组合它的元素。

答案 2 :(得分:0)

尝试使用`r`n换行。它需要新线和回车才能工作。

答案 3 :(得分:0)

如果我理解正确的话,您希望一行显示一个换行符,但您得到的是 `r`n 字面字符或任何您试图抛出的字符。

我能想出的最小测试用例来重现这个问题:

> function paste ($separator = '`r`n') {$($input) -join $separator}
> & { echo foo; echo bar; echo baz; } | paste
foo`r`nbar`r`nbaz

预期的结果是

foo
bar
baz

你如何获得实际的换行符作为输出而不是文字`r`n?超级简单,使用建议的答案即可!

> function paste ($separator = "`r`n") {$($input) -join $separator}
> & { echo foo; echo bar; echo baz; } | paste
foo
bar
baz

或者,如果你不喜欢魔法字符串

function paste ($separator = [System.Environment]::NewLine) {$($input) -join $separator}
PS D:\Temp\specs\ProRail.TrackChanges.Specs.Features> & { echo foo; echo bar; echo baz; } | paste
foo
bar
baz