none
PowerShell if ($variable -eq $null) {write-host..}

    Question

  • hi there I'm having trouble getting the correct syntax for my if ($variable -eq $null) to append what I'm  trying to echo or write-host to a file.

    Essentially what I'm trying to do is; if my $syslog or $applog variables do not see that there are errors in the event log, then append "no errors exist for the last 7 days" to my log file. i've tried the -eq $null and -ne $null -eq $true and -eq $false but I cannot get the "no errors.." to the log file even when truly there are not any errors in the event log over the 7 day period.

    below is my script (feel free to butcher it as needed :^) :

    $now = Get-Date
    $app_log_path = "c:\temp\app_event_log.txt"
    $sys_log_path =  "c:\temp\sys_event_log.txt"
    $hostname = (gwmi win32_computersystem name).name
    Echo "System name: $hostname","Report date: $now" | Out-File $app_log_path
    Echo "System name: $hostname","Report date: $now" | Out-File $sys_log_path
    $applog = Get-EventLog application -After $now.adddays(-7) | Where-Object {$_.EntryType -eq "error"} | fl entrytype, eventid, timewritten, source, message| out-file -Append $app_log_path
    $syslog = Get-EventLog system -After $now.adddays(-7) | Where-Object {$_.EntryType -eq "error"} | fl entrytype, eventid, timewritten, source, message| out-file -Append $sys_log_path

    if ($applog -eq $null) {write-host "No Errors exist over the last 7 days" | out-file -Append $app_log_path}
    else
    {
    $applog
    }
    if ($syslog -eq $null) {Write-Host "No Errors exist over the last 7 days" | out-file -Append $sys_log_path}
    else
    {
    $syslog
    }

    any help would be appreciated!

    Thanks

    -JB

    Friday, January 04, 2013 2:52 AM

Answers

  • Write-Host writes text to the PowerShell host display, not the pipeline. So you see "No errors..." on the screen, but nothing goes into the pipeline. if you want to tee the output to the screen and the file, do something like this:

    $CleanMessage = "No Errors exist over the last 7 days" 
    if ($applog -eq $null) {$CleanMessage | Tee-Object $app_log_path -Append}
    

    This doesn't explicitly write to the host, but since it writes to the file and sends it on down the pipe as well (where it is not consumed by anything) it has the same effect.

    Just remember that the name Write-Host really means what it says. ;)

    • Marked as answer by jbailey78 Friday, January 04, 2013 9:52 PM
    Friday, January 04, 2013 3:28 AM

All replies

  • Write-Host writes text to the PowerShell host display, not the pipeline. So you see "No errors..." on the screen, but nothing goes into the pipeline. if you want to tee the output to the screen and the file, do something like this:

    $CleanMessage = "No Errors exist over the last 7 days" 
    if ($applog -eq $null) {$CleanMessage | Tee-Object $app_log_path -Append}
    

    This doesn't explicitly write to the host, but since it writes to the file and sends it on down the pipe as well (where it is not consumed by anything) it has the same effect.

    Just remember that the name Write-Host really means what it says. ;)

    • Marked as answer by jbailey78 Friday, January 04, 2013 9:52 PM
    Friday, January 04, 2013 3:28 AM
  • Not tested, but see if this works better:

    $now = Get-Date
    $app_log_path = "c:\temp\app_event_log.txt"
    $sys_log_path =  "c:\temp\sys_event_log.txt"
    $hostname = (gwmi win32_computersystem name).name
    Echo "System name: $hostname","Report date: $now" | Out-File $app_log_path
    Echo "System name: $hostname","Report date: $now" | Out-File $sys_log_path
    $applog = Get-EventLog application -After $now.adddays(-7) | Where-Object {$_.EntryType -eq "error"} | fl entrytype, eventid, timewritten, source, message-file 
    $syslog = Get-EventLog system -After $now.adddays(-7) | Where-Object {$_.EntryType -eq "error"} | fl entrytype, eventid, timewritten, source, message 
    if ($applog -eq $null) {write-host "No Errors exist over the last 7 days" | out-file -Append $app_log_path}
    else
    {
    $applog
    $applog | outfile -Append $app_log_path
    }
    if ($syslog -eq $null) {Write-Host "No Errors exist over the last 7 days" | out-file -Append $sys_log_path}
    else
    {
    $syslog
    $syslog | out-file -Append $sys_log_path
    }


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


    Friday, January 04, 2013 3:35 AM
    Moderator
  • To test for null do the following:

    if($myvar){
        Write-Host 'variable is not null'
    }else{
         Write-Host 'Variable is null'
    }

    All variable test false in null or empty.  This is more readable and reliable and is how PowerShell was purposely designed to work.


    Happy New Year ¯\_(ツ)_/¯

    • Proposed as answer by Barry J. Lane Thursday, May 29, 2014 3:41 PM
    Friday, January 04, 2013 5:50 AM
  • Hi Alex,

    Tee-object does not have an -append parameter, but I achieved the result I wanted by using your $cleanmessage variable and replaced tee-object with out-file instead.

    thank you for your help!

    -JB

    Friday, January 04, 2013 9:52 PM