Link to home
Create AccountLog in
Avatar of Christophe
ChristopheFlag 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
Avatar of Zephyr ICT
Zephyr ICT
Flag of Belgium image

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.
Avatar of 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
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.
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
Avatar of oBdA
oBdA

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Thank you! it works perfectly. You are right, it was complicated.