Showing posts with label .Contains() method in PowerShell. Show all posts
Showing posts with label .Contains() method in PowerShell. Show all posts

Monday, March 26, 2012

PowerShell & SCOM: Use Operations Manager Shell to close multiple Alerts generated by Same Rule


Hi,
When today i login in to my SCOM console I saw approx 134 Active Alerts about Microsoft SQL Job failure. I was about to close these alert but then one thing strike in my mind that lets try to close these alerts using “Operations Manager Shell”. These Alerts are generating from same source and the name of the alert is same to it would not be to hard. So let try.
problem
Our First Step is to find out the Command which can show us Alert in SCOM Shell.
Open “Operations manager shell” and type  Get-Command *alert* , this will search for all SCOM cmdlets which have the word alert (we use wildcard *), and as you know PowerShell cmdlets works on Verb-Noun format  so if we use Get-Alert cmdlets it will shows all alerts.
alert
Let Try Get-Alert
alert-2
Type get-alert in the shell and hit Enter and it shown you all alerts.
Sol-5
Next task is to choose which Alert to Close .. if you look at active Alert in SCOM CONSOLE name of My Alert is “"A SQL job failed to complete successfully"
 problem-1
Now our next step is to find the properties and methods supported by Get-Alertcommand. To know these we need to use another command Get-Member
member
Yes.. it has name property…Gr8
Sol-2
Now we need to see all alerts whose name match “"A SQL job failed to complete successfully", for filter the output from Get-alert command we pipe (|) the output of Get-Alert command to Where-Object cmdlet.
Get-alert | Where-Object { $_.Name -match "A SQL job failed to complete successfully"}
where 
This will show all alerts whose name matches with "A SQL job failed to complete successfully"
So now our next step is to find a cmdlet which can close these alerts. lets find out..  run the same command
Get-Command *alert*
resolve-alert
So this time we have find Resolve-Alert cmdlet. now we need to join and our cmdlets command using piping . so this should be like this.
get-alert | where-object { $_.Name -match "A SQL job failed to complete successfully"} | Resolve-alert
rr
in Get Alert we are searching for an Alerts | then we are filter then using Name with match to “Sql job Failure | and then we are resolving them.  now type the above command and hit enter. It will take sometime to do this.
When you command run successfully, open SCOM Console and search for same SQL JOB error and you will found nothing :)
Solved-7

I hope that it helps someone…
Thanks
Aman Dhally

Tuesday, February 7, 2012

Part-5: Text Manipulation in PowerShell using .Contains() and .Equals() methods

 

Part-1: Text Manipulation in PowerShell using Trim, TrimStart , TrimEnd methods
Part-2: Text Manipulation in PowerShell using .ToLower and .ToUpper method
Part-3: Text Manipulation in PowerShell using .StartsWith() and EndsWith() methods
Part-4: Text Manipulation in PowerShell using .Replace(), and .Remove() methods.

 

Hi,

today we are using .Contains() and .Equals() method on our text. The output of these methods is Boolean which mean either “True” or “False”

Lets start.

first set our $text variables , today we are going to use  3 variables "$text,$text2 and $text3

$text = "The quick brown fox runs over the lazy dog"

$text2 = "Tommy is not a lazy dog"

$text3 = "The quick brown fox runs over the lazy dog"

07-02-2012 13-43-05 

.Contains()

.Contains() method  check for the  word which we provide in brackets in to the original string. Let me show you.

$text.Contains("brown")

I am checking that if our $text variable contains the word “brown” in it  , and yes we have “brown” word in our $text variable so the out is “True”

07-02-2012 13-46-33

lets check the another example.

$text.Contains("red")

Now we are checking that if we have a “red” word in our $text variable. we know that our $text is set to  “"The quick brown fox runs over the lazy dog"” and there is no “red” in it so that’s why output is false.

07-02-2012 13-50-37

.Equals()

Like .Contains(), .Equals() also give output in Boolean. .Equal() method checks that both Strings are equal or not. the syntax is .Equal and provide the string to match in brackets().

Let see the examples.

here are our $text variables

$text = "The quick brown fox runs over the lazy dog"

$text2 = "Tommy is not a lazy dog"

$text3 = "The quick brown fox runs over the lazy dog"

---------

$text.Equals("$text3")

First i am checking $text and $text3 variable. as you seen $text and $text3 are identical so the output of the .Equal() is true.

07-02-2012 14-08-09

Lets check $text variable with $text2 variable.

$text.Equals("$text2")

We know that $text variable and $text2 variables are not same and when we have used the .Equal method to compare both variables the output is “False”

 

07-02-2012 14-12-13

Videos

Use Contains() method

 

Use Equal Method

Thanks for viewing.

Aman Dhally

messenger_freak_adp1 (30)