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

Badges
MCSE
Community

Cozumpark Bilisim Portali
Posted in Windows Powershell | No Comment | 1,877 views | 15/07/2012 07:05

Function to test PS Modules

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-PSModule {
 
<#
    .SYNOPSIS
 
        Function to test PS Modules
 
    .EXAMPLE
 
        Test-PSModule -Name "FailoverClusters"
 
#>
 
[CmdletBinding(SupportsShouldProcess = $true)]
param (
    [Parameter(
        Mandatory = $true,
        HelpMessage = 'PowerShell Module Name. Example: FailoverClusters')]
    [string]$Name,
 
	[Parameter(
        Mandatory = $false,
        HelpMessage = 'XML output')]
    [switch]$OutXML = $false
)
	$ErrorActionPreference = "silentlycontinue"
 
	$CheckModule = Get-Module -Name $Name -EA SilentlyContinue
	if (!$CheckModule)
	{
		$ImportModule = Import-Module -Name $Name -EA SilentlyContinue
		$CheckModule = Get-Module -Name $Name -EA SilentlyContinue
		if (!$CheckModule)
		{
			$ResultCode = "-1"
			$ResultMessage = "$Name Module is not available."
		}
		else
		{
			$ResultCode = "1"
			$ResultMessage = "$Name Module is imported."
		}
	}
	else
	{
		$ResultCode = "1"
		$ResultMessage = "$Name Module is already imported."
	}
 
	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-PSModule -Name “FailoverClusters”