简体   繁体   中英

In a powershell pipeline how to remove trailing LF character?

Windows 2012 R2, Hyper-V 2012 R2, SCVMM, PowerShell...

...I'm generating a list of VMs using:

Get-SCVirtualMachine -All `
| Select-Object Name, ComputerName, Status, Memory, CPUCount, Location, VMHost `
| Sort-Object   Name, ComputerName `
| Select        Name, ComputerName, Status, @{e={[math]::Round($_.Memory/1024)}}, CPUCount, Location, VMHost `
| Export-CSV -Path "get-vms.csv" -NoTypeInformation

...but for one of the VMs, the ComputerName text (when it prints "getaddrinfo failed") contains a trailing LF (char 0x10) and I can't work out how to strip it off.

The problem entry in the exported CSV file looks like:

B:\> type get-vms.csv
...
"client02","client02.blah.com","Running","12","4","C:\Storage\CSV02\client02","host02.blah.com"
"client03","getaddrinfo failed
","Running","2","1","C:\Storage\CSV03\client03","host03.blah.com"
...

And to show the evidence of a trailing LineFeed character, I copied get-vms.csv to aa and to bb, and then used notepad to edit bb and delete the hidden invisible character, and saved, and then did a binary file compare, which shows:

B:\>fc /b a.a b.b
Comparing files a.a and B.B
00012DE7: 0A 22
00012DE8: 22 2C
00012DEA: 2C 22
...

I've tried to use this expression in the "| Select" clause, ie just before the Export-CSV:

@{e={[string]::Replace($_.ComputerName,[char]0x0A,"l")}}

...in place of just:

ComputerName

...but then the script fails to properly report the ComputerName of all VMs, ie when I use my attempt to strip off the bad character, my CSV file then looks like:

B:\> type get-vms.csv
...
"client02",,"Running","12","4","C:\Storage\CSV02\client02","host02.blah.com"
"client03",,"Running","2","1","C:\Storage\CSV03\client03","host03.blah.com"
...

...ie notice how the ComputerName item at the second field is now effectively reporting nothing/null.

Needless to say, I'm scratching my head... how do I strip the unwanted LF character yet still display the ComputerName text ?

You can use plain Powershell for this too, instead of .NET specific code. The newline characters is represented by ``n` in PS. So you can simply replace it with an empty string to remove it like this:

ComputerName -replace "`n",""

If you put that in your select it should be something like this:

Select Name, @{ Name = "ComputerName"; Expression = {$_.ComputerName -replace "`n","" }}, ...

A workaround would be to use a for loop (% { ...}) in the pipe and create a custom PS object containing the properties you need, filtering the LF out of the name and using that to export. That might be faster, but I'm not sure. Something like this:

<get,sort objects etc> 
| % { [PSCustomObject] @{ "ComputerName" = $_.ComputerName -replace "`n","" ; "Status" = $_.Status } } 
| <export to csv>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM