user11146726
user11146726

Reputation:

Remove blank lines in Out-File .txt generated with powershell

I'm working with a .ps1-Powershell-Script and I'm stuck.

I have following code:

$a=0
$b=0
$c=0
$freeRam=0
$strComputer = "localhost"
$date = get-date
$a=Get-WmiObject Win32_OperatingSystem -ComputerName $strComputer | fl *freePhysical* | Out-String
$b=Get-WmiObject Win32_OperatingSystem -ComputerName $strComputer | fl *totalvisiblememory* | Out-String
$c=Get-WmiObject win32_processor -ComputerName $strComputer | fl *LoadPercentage* | Out-String
$a = $a -replace '\D+(\d+)','$1'
$b = $b -replace '\D+(\d+)','$1'
$c = $c -replace '\D+(\d+)','$1'
$c = $c.Insert(1,",")
$freeRam=[math]::Round($a/$b*10000)/100 
$seperator="Es ist der " + $date + "; der verfuegbare Arbeitsspeicher betraegt: " + $freeRam + " % " + "und die Auslastung der CPU in % betraegt aktuell " + $c
$seperator | Out-File -append "D:\Logging\FreeRAM.txt"

This code generates a .txt-File with Powershell querying WmiObjects for free physical memory and the actual load percentage for the cpu.

It's working like expected but generates for the win32_processor LoadPercentage-Query blank linkes.

I've tried using .Trim() for Out-File like this $c=Get-WmiObject win32_processor -ComputerName $strComputer | fl *LoadPercentage* | Out-String.Trim() but that's not working. I've tried using -NoNewLine for Out-File like this $seperator | Out-File -append "D:\Logging\FreeRAM.txt" -NoNewLine but it's not working.

My Output looks like this:

Es ist der 03/04/2019 08:13:31; der verfuegbare Arbeitsspeicher betraegt: 71.86 % und die Auslastung der CPU in % betraegt aktuell 1,2




Es ist der 03/04/2019 08:13:35; der verfuegbare Arbeitsspeicher betraegt: 71.86 % und die Auslastung der CPU in % betraegt aktuell 1,0




Es ist der 03/04/2019 08:13:39; der verfuegbare Arbeitsspeicher betraegt: 71.86 % und die Auslastung der CPU in % betraegt aktuell 1,1




Es ist der 03/04/2019 08:14:00; der verfuegbare Arbeitsspeicher betraegt: 71.84 % und die Auslastung der CPU in % betraegt aktuell 1,0

And i want it like that:

Es ist der 03/04/2019 08:13:31; der verfuegbare Arbeitsspeicher betraegt: 71.86 % und die Auslastung der CPU in % betraegt aktuell 1,2
Es ist der 03/04/2019 08:13:35; der verfuegbare Arbeitsspeicher betraegt: 71.86 % und die Auslastung der CPU in % betraegt aktuell 1,0
Es ist der 03/04/2019 08:13:39; der verfuegbare Arbeitsspeicher betraegt: 71.86 % und die Auslastung der CPU in % betraegt aktuell 1,1
Es ist der 03/04/2019 08:14:00; der verfuegbare Arbeitsspeicher betraegt: 71.84 % und die Auslastung der CPU in % betraegt aktuell 1,0

Upvotes: 0

Views: 3389

Answers (3)

Lee_Dailey
Lee_Dailey

Reputation: 7479

[edit - modified processor load percentage calc to take into account the number of processors.]

i see that you have your answer ... but the code you used is somewhat more roundabout than seems needed. so i wrote a rather more direct version. [grin]

what it does ...

  • uses the CIM cmdlets instead of the WMI ones
    CIM is slightly faster than the deprecated WMI calls.
  • only calls the *_OperatingSystem class once
  • gets the .LoadPercentage directly
  • gets the .NumberOfProcessors from CIM_ComputerSystem
  • calculates the actual load % by summing the [possible] array of .LoadPercentage items & dividing by the .NumberOfProcesors
  • avoids the Format-List calls that were inserting the unwanted extra lines
  • uses the -f string format operator to build the output string

i used english for this since i was uncertain of the wording in your language. i presume you can translate back to yours far better than i can ... [grin]

$ComputerName = $env:COMPUTERNAME
$TimeStamp = Get-Date -Format 'yyyy-MM-dd HH:mm:ss'

$CIM_OS = Get-CimInstance -ClassName CIM_OperatingSystem -ComputerName $ComputerName
$CIM_CS_ProcessorCount = (Get-CimInstance -ClassName CIM_ComputerSystem).NumberOfProcessors

$FreeRAM_Pct = [math]::Round($CIM_OS.FreePhysicalMemory / $CIM_OS.TotalVisibleMemorySize * 100, 2)
$ProcLoad_Pct = [math]::Round(((Get-CimInstance -ClassName CIM_Processor -ComputerName $ComputerName).
    LoadPercentage |
    Measure-Object -Sum).Sum / $CIM_CS_ProcessorCount, 2)

'Time stamp = {0}; Free RAM % = {1}; Processor load % = {2}' -f $TimeStamp, $FreeRAM_Pct, $ProcLoad_Pct

output ...

Time stamp = 2019-03-06 06:18:04; Free RAM % = 43.61; Processor load % = 5

Upvotes: 3

Gert Jan Kraaijeveld
Gert Jan Kraaijeveld

Reputation: 1177

Try this construction:

$a=(Get-WmiObject Win32_OperatingSystem -ComputerName $strComputer | fl *freePhysical* | Out-String).Trim()

Or make the last line:

$seperator.Trim() | Out-File -append "D:\Logging\FreeRAM.txt"

Upvotes: 0

R.Amersfoort
R.Amersfoort

Reputation: 85

It seems that you variable $c contains empty new lines. For a quick fix, you can add the trim function as follows:

  $c = $c.Insert(1,",").trim()

Upvotes: 1

Related Questions