Avatar of Christophe
Christophe
Flag for France asked on

Powershell condition

Hello,

I do not know how to do that. I'd like to add condition to this code below. I'd like the value "$visio" writes "none" if the software is not found. For the moment, if the software is not found, result is empty.

$visio = Get-WmiObject -Class Win32_Product | sort-object Name | select Name | where { $_.Name -match “visio”} |
    Format-table -HideTableHeaders |
  Out-String

Open in new window


Thank you in advance for your help

Best,

Christophe
Powershell

Avatar of undefined
Last Comment
Christophe

8/22/2022 - Mon
Zephyr ICT

Do you mean something like this?

If ($visio = Get-WmiObject -Class Win32_Product | sort-object Name | select Name | where { $_.Name -match “visio”} | Format-table -HideTableHeaders | Out-String) {write-host "Visio Files found"}
Else {write-host "No Visio Files found"}

Open in new window


The write-host can be replaced by other things of course.
Christophe

ASKER
Thank you for your help. Yes it is like this but I'd like to write the result of value $visio in email body. I do not know how to use your code to do that. I will try
Christophe

ASKER
Because for the moment, I get result value in email. For exemple I receive "Microsoft Visio Viewer 2010"

If software with visio in name is not found, I'd like to receive "none".  But I need to keep the result of the code. ie visio version found.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Zephyr ICT

Something like this? It will send mail on each occasion, I could probably simplify it more but I'm limited in time right now...

IF ($visio = Get-WmiObject -Class Win32_Product | sort-object Name | select Name | where { $_.Name -match “visio”} | Format-table -HideTableHeaders | Out-String) {Send-MailMessage -To <email> -From <email> -Subject "Visio Files" -body "Visio Files found" -SmtpServer <your-smtp-server>}
Else {Send-MailMessage -To <email> -From <email> -Subject "Visio Files" -body "No Visio Files found" -SmtpServer <your-smtp-server>}

Open in new window

ASKER CERTIFIED SOLUTION
oBdA

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Christophe

ASKER
Thank you! it works perfectly. You are right, it was complicated.