-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutoPackager.ps1
113 lines (102 loc) · 3.26 KB
/
AutoPackager.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
Function Write-EphingLog {
Param (
[Parameter(Mandatory=$false)]
$Message,
[Parameter(Mandatory=$false)]
$ErrorMessage,
[Parameter(Mandatory=$false)]
$Component,
[Parameter(Mandatory=$false)]
[int]$Type,
[Parameter(Mandatory=$false)]
$LogFile
)
<#
Type: 1 = Normal, 2 = Warning (yellow), 3 = Error (red)
#>
$Time = Get-Date -Format "HH:mm:ss.ffffff"
$Date = Get-Date -Format "MM-dd-yyyy"
if ($ErrorMessage -ne $null) {$Type = 3}
if ($Component -eq $null) {$Component = " "}
if ($Type -eq $null) {$Type = 1}
$LogMessage = "<![LOG[$Message $ErrorMessage" + "]LOG]!><time=`"$Time`" date=`"$Date`" component=`"$Component`" context=`"`" type=`"$Type`" thread=`"`" file=`"`">"
#$LogMessage | Out-File -Append -Encoding UTF8 -FilePath $LogFile
if ($ErrorMessage -ne $null) {
$Message = "$Message - $ErrorMessage"
}
Write-Host $Message
}
Function Handle-EphingError {
Param (
$ErrorObject,
$Message
)
Write-EphingLog -Message $Message -ErrorMessage $ErrorObject.Exception.Message
}
Function Install-EphingApplication {
Param(
$ComputerName,
$ApplicationName
)
try {
$ApplicationObject = Get-EphingClientApplication -ComputerName $ComputerName -ApplicationName $ApplicationName
if($ApplicationObject -ne $null) {
$WMIPath = "\\$ComputerName\root\ccm\clientsdk:CCM_Application"
$WMIClass = [WMIClass] $WMIPath
$ApplicationRevision = $ApplicationObject.Revision
$IsMachineTarget = $ApplicationObject.IsMachineTarget
$EnforcePreference = $ApplicationObject.EnforcePreference
$ApplicationID = $ApplicationObject.ID
$null = $WMIClass.Install($ApplicationID, $ApplicationRevision, $IsMachineTarget, "", "1", $false)
return 'Installed'
}
else {
return 'NotFound'
}
}
catch {
Handle-EphingError -ErrorObject $_ -Message 'Error installing application'
return 'ErrorInstalling'
}
}
Function Get-EphingClientApplication {
Param(
$ComputerName,
$ApplicationName
)
try {
$ApplicationObjects = Get-WmiObject -Namespace 'root\ccm\clientsdk' -ComputerName $ComputerName -Class 'CCM_Application'
Foreach ( $ApplicationObject in $ApplicationObjects ) {
if ($ApplicationObject.FullName -eq $ApplicationName) {
return $ApplicationObject
}
}
}
catch {
Handle-EphingError -ErrorObject $_ -Message 'Error retrieving application object'
}
}
Function Get-EphingOptions {
if ( Test-Path "$PSScriptRoot\Settings.ini" ) {
try {
$Settings = Get-Content "$PSScriptRoot\Settings.ini"
$SettingsHash = @{}
Foreach($line in $Settings) {
if ($line.contains('=')) {
$SplitLine = $line.Split('=')
$SettingsHash[$SplitLine[0].Trim()] = $SplitLine[1].Trim()
}
}
return $SettingsHash
}
catch {
Write-EphingLog -Message 'Can not load settings'
Exit
}
}
else {
Write-EphingLog -Message 'Can not load settings'
Exit
}
}
$Settings = Get-EphingOptions