check if pssnapin could be load

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 3 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
paddy75
Posts: 27
Last visit: Sat Mar 16, 2024 11:11 pm

check if pssnapin could be load

Post by paddy75 »

Hello all,
I have a multi form project. The mainform has two buttons. In the formload i like to check when the exchange snapin is installed on the machine show the exchange button, when not hide it. I tried diffrent code, but all what i tried was not successfull. I tested it on the exchange server directly. Hope someone has an idea.
Here's the code I have at the moment:
  1.     if (-not (Get-PSSnapin -Registered | Where-Object{$_.Name -eq "Microsoft.Exchange.Management.PowerShell.SnapIn"}))
  2.         {
  3.         $buttonExchange.Visible = $false
  4.         }
  5.     else
  6.         {
  7.         $buttonExchange.Visible = $true
  8.         }
  9.     }
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: check if pssnapin could be load

Post by jvierra »

This is how to do this:

$buttonExchange.Visible = [bool](Get-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 -ea 0)
paddy75
Posts: 27
Last visit: Sat Mar 16, 2024 11:11 pm

Re: check if pssnapin could be load

Post by paddy75 »

Thanks for your reply, but this is also not working. Button is not visible but snapin is installed.
We have Exchange 2013, so i replaced the name. I tried this now:
  1. $buttonExchange.Visible = [bool](Get-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn -ea 0)
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: check if pssnapin could be load

Post by jvierra »

You have to use the exact name of the snapin.

At a prompt get the snapin and check the name. THe name you are using is not the3 name of the snapin.
paddy75
Posts: 27
Last visit: Sat Mar 16, 2024 11:11 pm

Re: check if pssnapin could be load

Post by paddy75 »

Name was correct. I found the mistake. the correct code is:

Code: Select all

 $buttonExchange.Visible = [bool](Get-PSSnapin -registered Microsoft.Exchange.Management.PowerShell.SnapIn -ea 0)
This topic is 3 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked