search
Categories
Sponsors
VirtualMetric Hyper-V Monitoring, Hyper-V Reporting
Archive
Blogroll

Badges
MCSE
Community

Cozumpark Bilisim Portali
PowerShell Function: Test-PSSnapin
Posted in Windows Powershell | No Comment | 2,032 views | 15/07/2012 07:03

Function to test PS Snapins.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
function Test-PSSnapin {
 
<#
    .SYNOPSIS
 
        Function to test PS Snapins
 
    .EXAMPLE
 
        Test-PSSnapin -Name "Microsoft.SystemCenter.VirtualMachineManager"
 
#>
 
[CmdletBinding(SupportsShouldProcess = $true)]
param (
    [Parameter(
        Mandatory = $true,
        HelpMessage = 'PowerShell Snapin Name. Example: Microsoft.SystemCenter.VirtualMachineManager')]
    [string]$Name,
 
	[Parameter(
        Mandatory = $false,
        HelpMessage = 'XML output')]
    [switch]$OutXML = $false
)
	$ErrorActionPreference = "silentlycontinue"
 
	$CheckSnapin = Get-PSSnapin -Name $Name -EA SilentlyContinue
	if (!$CheckSnapin)
	{
		$AddSnapin = Add-PSSnapin -Name $Name -EA SilentlyContinue
		$CheckSnapin = Get-PSSnapin -Name $Name -EA SilentlyContinue
		if (!$CheckSnapin)
		{
			$ResultCode = "-1"
			$ResultMessage = "$Name Snapin is not available."
		}
		else
		{
			$ResultCode = "1"
			$ResultMessage = "$Name Snapin is added."
		}
	}
	else
	{
		$ResultCode = "1"
		$ResultMessage = "$Name Snapin is already loaded."
	}
 
	if ($OutXML)
	{
		New-XML -ResultCode $ResultCode -ResultMessage $ResultMessage
	}
	else
	{
		$Properties = New-Object Psobject
		$Properties | Add-Member Noteproperty ResultCode $ResultCode
		$Properties | Add-Member Noteproperty ResultMessage $ResultMessage
		Write-Output $Properties
	}
}

Example Usage: Test-PSSnapin -Name “Microsoft.SystemCenter.VirtualMachineManager”



Leave a Reply