-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddApplicationWithEnhancedDeploymentTypeAndRequirements.ps1
143 lines (115 loc) · 8.4 KB
/
AddApplicationWithEnhancedDeploymentTypeAndRequirements.ps1
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
$SiteServer = "degpwa59.deluxe.com"
$SiteCode = "DLX"
$GetIdentification = [WmiClass]"\\$($SiteServer)\root\SMS\Site_$($SiteCode):SMS_Identification"
$ScopeID = "ScopeId_" + $GetIdentification.GetSiteID().SiteID -replace "{","" -replace "}",""
$ContentSourcePath = "\\dompwv01f\dappuat\Google Talk 1.0.0.104"
$ApplicationTitle = "TestApp"
$ApplicationVersion = 1.0
$ApplicationSoftwareVersion = "1.0"
$ApplicationLanguage = (Get-Culture).Name
$ApplicationDescription = "Test description"
$ApplicationPublisher = "TestCorp"
$DeploymentInstallCommandLine = "setup.exe"
$DeploymentUninstallCommandLine = "setup.exe /uninstall"
# Create unique ID for application and deployment type
$ApplicationID = "APP_" + [GUID]::NewGuid().ToString()
$DeploymentTypeID = "DEP_" + [GUID]::NewGuid().ToString()
$ObjectApplicationID = New-Object Microsoft.ConfigurationManagement.ApplicationManagement.ObjectId($ScopeID,$ApplicationID)
$ObjectDeploymentTypeID = New-Object Microsoft.ConfigurationManagement.ApplicationManagement.ObjectId($ScopeID,$DeploymentTypeID)
$ObjectApplication = New-Object Microsoft.ConfigurationManagement.ApplicationManagement.Application($ObjectApplicationID)
$ObjectDeploymentType = New-Object Microsoft.ConfigurationManagement.ApplicationManagement.DeploymentType($ObjectDeploymentTypeID,"Script")
# Add content to the Application
$ApplicationContent = [Microsoft.ConfigurationManagement.ApplicationManagement.ContentImporter]::CreateContentFromFolder($ContentSourcePath)
$ApplicationContent.OnSlowNetwork = [Microsoft.ConfigurationManagement.ApplicationManagement.ContentHandlingMode]::DoNothing
$ApplicationContent.OnFastNetwork = [Microsoft.ConfigurationManagement.ApplicationManagement.ContentHandlingMode]::Download
# Application information
$ObjectDisplayInfo = New-Object Microsoft.ConfigurationManagement.ApplicationManagement.AppDisplayInfo
$ObjectDisplayInfo.Language = $ApplicationLanguage
$ObjectDisplayInfo.Title = $ApplicationTitle
$ObjectDisplayInfo.Description = $ApplicationDescription
$ObjectApplication.DisplayInfo.Add($ObjectDisplayInfo)
$ObjectApplication.DisplayInfo.DefaultLanguage = $ApplicationLanguage
$ObjectApplication.Title = $ApplicationTitle
$ObjectApplication.Version = $ApplicationVersion
$ObjectApplication.SoftwareVersion = $ApplicationSoftwareVersion
$ObjectApplication.Description = $ApplicationDescription
$ObjectApplication.Publisher = $ApplicationPublisher
# DeploymentType configuration
$ObjectDeploymentType.Title = $ApplicationTitle
$ObjectDeploymentType.Version = $ApplicationVersion
$ObjectDeploymentType.Enabled = $true
$ObjectDeploymentType.Description = $ApplicationDescription
$ObjectDeploymentType.Installer.Contents.Add($ApplicationContent)
$ObjectDeploymentType.Installer.InstallCommandLine = $DeploymentInstallCommandLine
$ObjectDeploymentType.Installer.UninstallCommandLine = $DeploymentUninstallCommandLine
$ObjectDeploymentType.Installer.ProductCode = "{" + [GUID]::NewGuid().ToString() + "}"
$ObjectDeploymentType.Installer.ProductVersion = "1.0"
$ObjectDeploymentType.Installer.ExecutionContext = "System"
$ObjectDeploymentType.Installer.RequiresUserInteraction = $false
#$ObjectDeploymentType.Installer.RequiresElevatedRights = $true
$ObjectDeploymentType.Installer.DetectionMethod = [Microsoft.ConfigurationManagement.ApplicationManagement.DetectionMethod]::Enhanced
#build the enhanced detection method
$EnhancedDetectionMethod = new-object Microsoft.ConfigurationManagement.ApplicationManagement.EnhancedDetectionMethod
#update with correct product GUID
$MSISettingInstance = new-object Microsoft.ConfigurationManagement.DesiredConfigurationManagement.MSISettingInstance -ArgumentList ([GUID]::NewGuid().ToString(),$null)
#delete existing application if present
$preApp = Get-CMApplication -Name $ApplicationTitle
if($preApp){
Remove-CMApplication -Name $ApplicationTitle -force
}
#create settings reference
$refName = "SettingRef_" + [Guid]::NewGuid().ToString() #create a new GUID
$logicalName = $MSISettingInstance.LogicalName #i.e. MSI_74dbdc2c-2331-49ce-8e77-37204365d0e7
$msiVersion = "1.0" #to be passed in
$dataType = [Microsoft.SystemsManagementServer.DesiredConfigurationManagement.Expressions.DataType]::Version #Object Type being compared against.
$dataType2 = [Microsoft.ConfigurationManagement.DesiredConfigurationManagement.ConfigurationItemSettingSourceType]::MSI
$dataTypeVersion = [Microsoft.ConfigurationManagement.DesiredConfigurationManagement.ConfigurationItemSettingMethodType]::Value
$dataTypeIsEquals = [Microsoft.ConfigurationManagement.DesiredConfigurationManagement.ExpressionOperators.ExpressionOperator]::IsEquals
$SettingsReference = new-object Microsoft.SystemsManagementServer.DesiredConfigurationManagement.Expressions.SettingReference -ArgumentList ($scopeID, $refName, 0, $logicalName, $dataType, $dataType2, $true, $dataTypeVersion,"ProductVersion")
$SettingsConstant = new-object Microsoft.SystemsManagementServer.DesiredConfigurationManagement.Expressions.ConstantValue -ArgumentList ($msiVersion, [Microsoft.SystemsManagementServer.DesiredConfigurationManagement.Expressions.DataType]::Version)
#unsure why this works, but it instantiates the correct object type
$operands = new-object "Microsoft.ConfigurationManagement.DesiredConfigurationManagement.CustomCollection``1[[Microsoft.SystemsManagementServer.DesiredConfigurationManagement.Expressions.ExpressionBase]]"
$operands.add($SettingsReference)
$operands.add($SettingsConstant)
$expression = new-object Microsoft.SystemsManagementServer.DesiredConfigurationManagement.Expressions.Expression -ArgumentList ($dataTypeIsEquals), $operands
$anno = new-object Microsoft.SystemsManagementServer.DesiredConfigurationManagement.Rules.Annotation
$anno.DisplayName = new-object Microsoft.SystemsManagementServer.DesiredConfigurationManagement.Rules.LocalizableString -ArgumentList "DisplayName", $logicalName, $null
$rule = new-object "Microsoft.SystemsManagementServer.DesiredConfigurationManagement.Rules.Rule" -ArgumentList ( ("Rule_" + [Guid]::NewGuid().ToString()), [Microsoft.SystemsManagementServer.DesiredConfigurationManagement.Rules.NoncomplianceSeverity]::None, $anno, $expression)
$ObjectDeploymentType.Installer.EnhancedDetectionMethod = new-object Microsoft.ConfigurationManagement.ApplicationManagement.EnhancedDetectionMethod
$ObjectDeploymentType.Installer.EnhancedDetectionMethod.Settings.Add($MSISettingInstance)
$ObjectDeploymentType.Installer.EnhancedDetectionMethod.Rule = $rule
$T1 = "Windows/x64_Windows_7_SP1"
$T4 = "Windows/All_x64_Windows_Server_2008_R2"
$t0 = "Windows/x86_Embedded_Windows_7"
#add each scope as a requirement.
$scopes = $t1, $t4, $t0
foreach($scope in $scopes){
#$rule = Create-SCCMGlobalConditionsRule "Global/OperatingSystem" "OneOF" @("Windows/All_x64_Windows_XP_Professional") .
$Expoperator = [Microsoft.ConfigurationManagement.DesiredConfigurationManagement.ExpressionOperators.ExpressionOperator]::OneOf
$operands = new-object "Microsoft.ConfigurationManagement.DesiredConfigurationManagement.CustomCollection``1[[Microsoft.SystemsManagementServer.DesiredConfigurationManagement.Expressions.RuleExpression]]"
$operands.Add("$scope")
#Creating the expression
$arg = @( $Expoperator ,
$operands
)
$expression = new-object Microsoft.SystemsManagementServer.DesiredConfigurationManagement.Expressions.OperatingSystemExpression -ArgumentList $arg
#Creating the rule
$arg = @(
("Rule_" + [Guid]::NewGuid().ToString()),
[Microsoft.SystemsManagementServer.DesiredConfigurationManagement.Rules.NoncomplianceSeverity]::None,
$anno,
$expression
)
$rule = new-object "Microsoft.SystemsManagementServer.DesiredConfigurationManagement.Rules.Rule" -ArgumentList $arg
# Add DeploymentType to Application
$ObjectDeploymentType.Requirements.Add($rule)
}$ObjectApplication.DeploymentTypes.Add($ObjectDeploymentType)
# Serialize the Application
$ApplicationXML = [Microsoft.ConfigurationManagement.ApplicationManagement.Serialization.SccmSerializer]::SerializeToString($ObjectApplication)
$ApplicationClass = [WmiClass]"\\$($SiteServer)\root\SMS\Site_$($SiteCode):SMS_Application"
$ObjectApplication = $ApplicationClass.CreateInstance()
$ObjectApplication.SDMPackageXML = $ApplicationXML
$Temp = $ObjectApplication.Put()
$ObjectApplication.Get()
#$app = Get-WmiObject -computername "degpwa59.deluxe.com" -namespace "Root\SMS\Site_DLX" -query "select * from SMS_Application"