stuffatwork190
stuffatwork190

Reputation: 157

Removing Blank Lines From Powershell Output

I have searched for an answer to this and have not found something that has worked for me yet.

I have a Powershell script that is being used in Tripwire to assess the IIS version of a server. The value is returned, but it can't be interpreted by Tripwire because of blank lines, I believe.

Kindly see the code below. Thank you in advance!

%Windir%\system32\WindowsPowerShell\v1.0\powershell.exe    "$ErrorActionPreference='SilentlyContinue'; $iv=(get-itemproperty HKLM:\SOFTWARE\Microsoft\InetStp\  | select setupstring | ft -hidetableheaders); if ($iv.length -gt 0 | where {$_.versionstring -ne ""} ) { echo $iv } else { Write-Host -NoNewline "Not Installed" }"

Upvotes: 0

Views: 904

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174435

The reason for the extra lines is this: |ft -hidetableheaders

Just grab the Setupstring property value directly with Select-Object -ExpandProperty setupstring:

$iv = Get-ItemProperty HKLM:\SOFTWARE\Microsoft\InetStp\ | Select -ExpandProperty setupstring; 

The expression $iv.length -gt 0| where {$_.versionstring -ne ""} doesn't make to much sense, and is exactly equivalent to $iv.length -gt 0.

You can simplify the if statement by just testing $iv:

if ($iv) { 
    Write-Host $iv -NoNewline
} else {
    Write-Host 'Not Installed' -NoNewline 
}

So the line becomes:

%Windir%\system32\WindowsPowerShell\v1.0\powershell.exe "$ErrorActionPreference='SilentlyContinue';$iv=Get-ItemProperty HKLM:\SOFTWARE\Microsoft\InetStp\ | Select -Expand setupstring; if ($iv) { Write-Host $iv -NoNewline} else { Write-Host 'Not Installed' -NoNewline }"

Upvotes: 1

Related Questions