All posts by smarc

PowerShell – Testing if a String is NULL or EMPTY

PowerShell – Testing if a String is NULL or EMPTY

Checking if a string is NULL or EMPTY is very common requirement in Powershell script. If we do not checked that we will get some unwanted results when we try to perform some operation on that string variable which is empty or null.

The following method is used to check if a string is NULL or empty.

$mystring ='hello'
if($mystring) {            
    Write-Host "Your string is not EMPTY"            
} else {            
    Write-Host "Your string is EMPTY or NULL"            
}

We can also use the .NET Class “System.String” to check null or empty string. It has a method IsNullOrEmpty() which returns true if the passed string is null or empty. Check the below example.

$mystring ='hello'
IF([string]::IsNullOrEmpty($mystring)) {            
    Write-Host "Your string is EMPTY or NULL"            
} else {            
    Write-Host "Your string is not EMPTY"            
}

We can also use the method IsNullOrWhiteSpace() from “System.String” class to detect a given string is NULL or EMPTY and whether it has WHITESPACE.

$mystring = " "            
IF([string]::IsNullOrWhiteSpace($mystring)) {            
    Write-Host "Your string is NULL or EMPTY or it has WHITESPACE"            
} else {            
    Write-Host "Your string is not EMPTY"            
}     

Note: The method IsNullOrWhiteSpace() is available only from .NET Frmaework 4.0, so, this method do not work in Powershell 2.0 and it will work only from Powershell 3.0.

You will get below error, when you run this method in Powershell 2.0.

IsNullOrWhitespace : Method invocation failed because [System.String] doesn’t contain a method named ‘IsNullOrWhiteSpace’

Source: PowerShell – Testing if a String is NULL or EMPTY

Task Scheduler Event IDs | mnaoumov.NET

Task Scheduler Event IDs

I discovered that some of my task scheduler tasks are failing on the server and wanted to configure email notifications if that happens

I found an article how to send task scheduler notifications

I wanted to configure a trigger for multiple Event IDs and found how to do this here

The only question left if the list of Event IDs and I could not find a list of all possible values so I extracted them from EventLog myself and putting them here

Event ID Task Category
100 Task Started
101 Task Start Failed
102 Task completed
103 Action start failed
106 Task registered
107 Task triggered on scheduler
108 Task triggered on event
110 Task triggered by user
111 Task terminated
118 Task triggered by computer startup
119 Task triggered on logon
129 Created Task Process
135 Launch condition not met, machine not idle
140 Task registration updated
141 Task registration deleted
142 Task disabled
200 Action started
201 Action completed
203 Action failed to start
301 Task engine properly shut down
310 Task Engine started
311 Task Engine failed to start
314 Task Engine idle
317 Task Engine started
318 Task engine properly shut down
319 Task Engine received message to start task
322 Launch request ignored, instance already running
329 Task stopping due to timeout reached
332 Launch condition not met, user not logged-on
400 Service started
411 Service signaled time change
700 Compatibility module started

Going to create an alert for ids 101,103,111,311,329

m from EventLog myself and putting them here Event

Source: Task Scheduler Event IDs | mnaoumov.NET