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
ChristopheIT CoordinatorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
Zephyr ICTCloud ArchitectCommented:
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.
0
 
ChristopheIT CoordinatorAuthor Commented:
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
0
 
ChristopheIT CoordinatorAuthor Commented:
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.
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
Zephyr ICTCloud ArchitectCommented:
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

0
 
oBdACommented:
Your inital code is overly complicated.
$visio = Get-WmiObject -Class Win32_Product | Where {$_.Name -match "Visio"} | Select -ExpandProperty Name
If (!$Visio) {$Visio = "Visio not found"}

Open in new window

0

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
 
ChristopheIT CoordinatorAuthor Commented:
Thank you! it works perfectly. You are right, it was complicated.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.