unexpected line breaks while using out-file

Hi all,

here is my code to export bunch of inventory to a text file.

$ADInfo = Get-ADDomain
$allDCs = $ADInfo.ReplicaDirectoryServers
foreach ($item in $allDCs)
{
$processor1 = (gwmi win32_processor -ComputerName $item).Name
$RAM = (gwmi win32_ComputerSystem -ComputerName $item).TotalPhysicalMemory
$memory = [math]::Round($RAM/1024/1024/1024, 0)
$TimeZone0 = (gwmi Win32_TimeZone -ComputerName $item).caption
$TimeZone1 = $TimeZone0 -replace ":", ""
$TimeZone = $TimeZone1 -replace " ", ""
$IPGateway0 = (gwmi win32_NetworkAdapterConfiguration -ComputerName $item | where {$_.IPEnabled -eq "True"}).DefaultIPGateway
foreach ($ipgw in $IPGateway0)
{
$IPGateway = $ipgw | Out-String				
}
$NICs = Get-ADComputer -Filter * -Properties ipv4Address | where {$_.DNSHostName -like "$item"} | foreach {$_.ipv4address}
foreach ($NICItem in $NICs)
{
$IPAddress2 = $NICItem | Out-String
"$item : {0} : {1} GB : {2} : {3} : {4}" -f $processor1,$memory,$TimeZone,$IPAddress2,$IPGateway >> c:\scripts\hardware.log 
}

}

It exports data to hardware.log file. But for the last variable (IPGateway or anything i put) it breaks line and add something like that:

Intel(R) Core(TM) i5-2520M CPU @ 2.50GHz + 1 + (UTC+0200)Minsk + 10.0.0.2
 + 100.0.8.7

Any idea?

July 2nd, 2013 9:15am

I'm guessing $IPAddress2 has a line break at the end of it. Try something like this to see if this fixes it:

"$item : {0} : {1} GB : {2} : {3} : {4}" -f $processor1,$memory,$TimeZone,($IPAddress2 -replace "`n", ""),$IPGateway >> c:\scripts\hardware.log 

Mike

Free Windows Admin Tool Kit Click here and download it now
July 2nd, 2013 9:20am

yeap, worked.
July 2nd, 2013 9:24am

ups no luck. That solution is applicable if i export to txt. But it still adds additional line in PS console. For instance:

$ADInfo = Get-ADDomain
$allDCs = $ADInfo.ReplicaDirectoryServers
foreach ($item in $allDCs)
{
$NICs = Get-ADComputer -Filter * -Properties ipv4Address | where {$_.DNSHostName -like "$item"} | foreach {$_.ipv4address}
foreach ($NICItem in $NICs)
{
$IPAddress2 = $NICItem | Out-String
$IPAddress3 = $IPAddress2 -replace "`n", ""
$IPAddress3
}
}

IPAddress3 adds one more line at the end of the data.

Don't we have a switch like -noendline with out-string command? 

One more line breaks all my parsing rules.


Free Windows Admin Tool Kit Click here and download it now
July 2nd, 2013 11:48am

If you just want to output the IP addresses separated by a space, use -join:

$ADInfo = Get-ADDomain
$allDCs = $ADInfo.ReplicaDirectoryServers
foreach ($item in $allDCs)
{
$NICs = (Get-ADComputer -Filter * -Properties ipv4Address | where {$_.DNSHostName -like "$item"}).ipv4address -join " "
# Show Results
$NICs
}

Edit - the above works in PS 3.0. For 2.0, you can do this:

$ADInfo = Get-ADDomain
$allDCs = $ADInfo.ReplicaDirectoryServers
foreach ($item in $allDCs)
{
$NICs = Get-ADComputer -Filter * -Properties ipv4Address | where {$_.DNSHostName -like "$item"} | ForEach-Object { $_.ipv4address }
$NICResults = $NICs -join " "
# Show Results
$NICResults
}

Mike




July 2nd, 2013 12:31pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics