check if pssnapin could be load

Hello all,
I have a multi form project. The mainform has two buttons. In the formload event 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:

	if (-not (Get-PSSnapin -Registered | Where-Object{$_.Name -eq "Microsoft.Exchange.Management.PowerShell.SnapIn"}))
		{
		$buttonExchange.Visible = $false
		}
	else
		{
		$buttonExchange.Visible = $true
		}
	}

 

When you say it is not working, is like the button is visible even if the snapin is not available ? If feasible, can you share the whole code ? you can replace secret values with dummy values.

Have you exposed the $buttonExchange variable to a scope where your if statement will have access?

$[Global]buttonExchange = …

or

$[Script]buttonExchange = …

Hi,

thanks for your replies. If i set both to $true, it is displayed. The snapin will be loaded with another form. I only want to prevent, when the script runs on an other system without the exchange snapin is installed that it is not possible to click it. I’m using PowerShell Studio from SAPIEN. Modified the code since my last post but also no luck. I don’t post the other two functions “CheckExport” and “Checkxml” because this is working.

$MainForm_Load={
}

#region function Check-Exchange-Modul
function CheckExchangeModul
{
$buttonExchange.Visible = [bool](Get-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn -ea 0)

# if (-not (Get-PSSnapin -Registered | Where-Object{$_.Name -eq “Microsoft.Exchange.Management.PowerShell.SnapIn”}))
# {
# $buttonExchange.Visible = $false
# }
# else
# {
# $buttonExchange.Visible = $true
# }
}

#endregion function Check-Exchange-Modul

#region SplashScreen_Load
$formSplashScreen_Load = {
<#
Use the -PassTru parameter to update the splash screen text:
.EXAMPLE
$splashForm = Show-SplashScreen … -PassThru
#Update the splash screen text
$splashForm.Text = ‘Loading Modules…’
#>

$paramShowSplashScreen = @{
Image = $pictureboxSplashScreenHidden.Image
PassThru = $false
Timeout = 2
}

Show-SplashScreen @paramShowSplashScreen
CheckExport
Checkxml
CheckExchangeModul

}
#endregion SplashScreen_Load

#Append the event to the form
$MainForm.add_Load($formSplashScreen_Load)

#region Splash Screen Helper Function
function Show-SplashScreen
{
<#
.SYNOPSIS
Displays a splash screen using the specified image.

.PARAMETER Image
Mandatory Image object that is displayed in the splash screen.

.PARAMETER Title
(Optional) Sets a title for the splash screen window.

.PARAMETER Timeout
The amount of seconds before the splash screen is closed.
Set to 0 to leave the splash screen open indefinitely.
Default: 2

.PARAMETER ImageLocation
The file path or url to the image.

.PARAMETER PassThru
Returns the splash screen form control. Use to manually close the form.

.PARAMETER Modal
The splash screen will hold up the pipeline until it closes.

.EXAMPLE
PS C:\> Show-SplashScreen -Image $Image -Title ‘Loading…’ -Timeout 3

.EXAMPLE
PS C:\> Show-SplashScreen -ImageLocation ‘C:\Image\MyImage.png’ -Title ‘Loading…’ -Timeout 3

.EXAMPLE
PS C:\> $splashScreen = Show-SplashScreen -Image $Image -Title ‘Loading…’ -PassThru
#close the splash screen
$splashScreen.Close()
.OUTPUTS
System.Windows.Forms.Form

.NOTES
Created by SAPIEN Technologies, Inc.

The size of the splash screen is dependent on the image.
The required assemblies to use this function outside of a WinForms script:
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
#>
[OutputType([System.Windows.Forms.Form])]
param
(
[Parameter(ParameterSetName = ‘Image’,
Mandatory = $true,
Position = 1)]
[ValidateNotNull()]
[System.Drawing.Image]$Image,
[Parameter(Mandatory = $false)]
[string]$Title,
[int]$Timeout = 2,
[Parameter(ParameterSetName = ‘ImageLocation’,
Mandatory = $true,
Position = 1)]
[ValidateNotNullOrEmpty()]
[string]$ImageLocation,
[switch]$PassThru,
[switch]$Modal
)

#Create a splash screen form to display the image.
$splashForm = New-Object System.Windows.Forms.Form

#Create a picture box for the image
$pict = New-Object System.Windows.Forms.PictureBox

if ($Image)
{
$pict.Image = $Image;
}
else
{
$pict.Load($ImageLocation)
}

$pict.AutoSize = $true
$pict.Dock = ‘Fill’
$splashForm.Controls.Add($pict)

#Display a title if defined.
if ($Title)
{
$splashForm.Text = $Title
$splashForm.FormBorderStyle = ‘FixedDialog’
}
else
{
$splashForm.FormBorderStyle = ‘None’
}

#Set a timer
if ($Timeout -gt 0)
{
$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = $Timeout * 1000
$timer.Tag = $splashForm
$timer.add_Tick({
$this.Tag.Close();
$this.Stop()
})
$timer.Start()
}

#Show the form
$splashForm.AutoSize = $true
$splashForm.AutoSizeMode = ‘GrowAndShrink’
$splashForm.ControlBox = $false
$splashForm.StartPosition = ‘CenterScreen’
$splashForm.BackColor = ‘AliceBlue’
$splashForm.TopMost = $true

if ($Modal) { $splashForm.ShowDialog() }
else { $splashForm.Show() }

if ($PassThru)
{
return $splashForm
}
}
#endregion

 

Although it wont hide the button, setting the Enabled property to $False should ghost/disable it.

i found the mistake. the correct code should be:

$buttonExchange.Visible = [bool](Get-PSSnapin -registered Microsoft.Exchange.Management.PowerShell.SnapIn -ea 0)