SMLets.psm1

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#requires -version 2.0
# handy global variables
$GLOBAL:SMADLL   = ([appdomain]::CurrentDomain.getassemblies()|?{$_.location -match "System.Management.Automation.dll"}).location
#$GLOBAL:SMDIR = (Get-ItemProperty 'hklm:/software/microsoft/System Center/2010/Service Manager/Setup').InstallDirectory
#$GLOBAL:SMSDKDIR = "${SMDIR}\SDK Binaries"
$GLOBAL:SMDLL    = "Microsoft.EnterpriseManagement.Core"
$GLOBAL:EMGTYPE  = "Microsoft.EnterpriseManagement.EnterpriseManagementGroup"
$GLOBAL:DATAGENDIR = "$psScriptRoot\DataGen"
$GLOBAL:SCSMLOGNAME = "Operations Manager"
$GLOBAL:SCSMLOGSOURCENAME = "SMLets"
#$GLOBAL:smdefaultcomputer = "SCSM12"


<#
.SYNOPSIS
    Load an assembly into the current process space
.DESCRIPTION
    Provides a quick way of loading an assembly into the current process.
.PARAMETER Dll
    The assemply to load. This can be either a file name or full path
.EXAMPLE
    Import-Assembly "C:\Program Files\Microsoft System Center\Service Manager 2010\SDK Binaries\Microsoft.EnterpriseManagement.Core.dll"
.OUTPUTS
    No output
#>

function import-Assembly
{
    [CmdletBinding(SupportsShouldProcess=$true)]

    param ( 
        [parameter(Mandatory=$true,Position=0)]$dll ,
        [switch]$partial
        )
    end
    {
        
        if ( $partial )
        {
            [reflection.assembly]::LoadWithPartialName($dll)
        }
        else
        {
            [reflection.assembly]::LoadFile($dllpath)
        }
    }
}
# load the Service Manager Core assembly
set-alias -scope global load import-assembly
load $SMDLL -Partial | out-null

<#
.SYNOPSIS
    Create a new EnterpriseManagementGroup object
.DESCRIPTION
    This cmdlet creates a connection to a Service Manager 2010 Data Access
    Service and returns the resultant EnterpriseManagementGroup object
.PARAMETER ComputerName
    The ComputerName to use when creating a connection to the
    Service Manager 2010 Data Access Service
.EXAMPLE
    $emg = new-ManagementGroup
.OUTPUTS
a Microsoft.EnterpriseManagement.EnterpriseManagementGroup object
#>

function New-ManagementGroup
{
    [CmdletBinding(SupportsShouldProcess=$true)]
    param ( 
        [parameter(Position=0)][String]$ComputerName = "localhost" 
        ) 
    end
    {
        if($PSCmdlet.ShouldProcess($ComputerName))
        {
            new-object $EMGTYPE $ComputerName
        }
    }
}
set-alias -scope global new-mg New-ManagementGroup
<#
.SYNOPSIS
    Retrieve Service Manager 2010 class which have a specified property
.DESCRIPTION
    This cmdlet retrieves classes which have a property that equals the name
    provided by the parameter 'name'
.PARAMETER Name
    The parameter name to retrieve from any class
    in the Service Manager 2010 system
.EXAMPLE
    get-property Domain
    retrieves ManagementPackClasses which contain the property name 'Domain'
.OUTPUTS
    A ManagementPackClass object which includes a property name that
    equals the name provided
#>

function get-SCSMproperty 
{
    [CmdletBinding()]
    param ( 
        [parameter(Position=0)][string] $name = ".*"
        ) 
    end
    {
        get-scsmclass|?{$_.getproperties()|?{$_.name -eq $name}}
    }
}

function New-SCSMLogMessage($message, $eventId = 1)
{
if($message.GetType().FullName -eq "System.Management.Automation.ErrorRecord")
{
if(![System.Diagnostics.EventLog]::SourceExists($SCSMLOGSOURCENAME) )
{
[System.Diagnostics.EventLog]::CreateEventSource($SCSMLOGSOURCENAME, $SCSMLOGNAME)
}

$eventMessage = [string]::Format("{2}{0}{0}{1}{0}{0}{3}", [Environment]::NewLine, $message, $message.InvocationInfo.PositionMessage, $message.Exception.StackTrace) 

Write-EventLog -LogName $SCSMLOGNAME -Source $SCSMLOGSOURCENAME -EntryType Error -EventID $eventId -Message $eventMessage #-category 1 -rawdata 10,20
}
else
{
Write-EventLog -LogName $SCSMLOGNAME -Source $SCSMLOGSOURCENAME -EntryType Information -EventID $eventId -Message $message
}
}


#function Get-SCSMClassProperty
#{
# [CmdletBinding()]
# param ( [string]$classname )
# $class = get-scsmclass $classname
# if ( $class -is [array] )
# {
# $class |%{$_.name}| write-host -for red
# Throw "Too many classes, try again"
# }
# if ( ! $class )
# {
# throw "$classname not found, try again"
# }
# $EMOT = "Microsoft.EnterpriseManagement.Common.CreatableEnterpriseManagementObject"
# try
# {
# if ( $Class.Abstract )
# {
# $Class.GetProperties("recursive")
# }
# else
# {
# $emo = new-object $EMOT $class.ManagementGroup,$class
# $emo.getproperties("recursive")
# }
# }
# catch
# {
# throw $error[0]
# }
#}
set-alias get-property get-property
function get-SCSMCommand
{
    [CmdletBinding()]
    param ( )
    end
    {
        get-command -module SMLets | sort-object CommandType,Noun
    }
}
# now add the PSScriptRoot to the path, this will ensure that any scripts
# in the module directory are accessible
$env:path += ";$psscriptroot;$psscriptroot\Scripts"

set-alias -scope global Export-SCSMManagementPack Export-SCManagementPack
set-alias -scope global Get-SCSMManagementPack Get-SCManagementPack
set-alias -scope global Get-SCSMManagementPackElement Get-SCManagementPackElement
set-alias -scope global Import-SCSMManagementPack Import-SCManagementPack
set-alias -scope global New-SCSMManagementPack New-SCManagementPack
set-alias -scope global Remove-SCSMManagementPack Remove-SCManagementPack

#$SMLetsTypesFile = join-path $PSScriptRoot SMLets.Types.ps1xml
#update-typedata $SMLetsTypesFile -ErrorAction SilentlyContinue
#$SMLetsFormatFile = join-path $PSScriptRoot SMLets.Format.ps1xml
#update-formatdata $SMLetsFormatFile -ErrorAction SilentlyContinue