-
Notifications
You must be signed in to change notification settings - Fork 1
/
UninstallDeployablePackage.ps1
104 lines (81 loc) · 2.81 KB
/
UninstallDeployablePackage.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
param([switch]$Elevated)
Function Check-Admin()
{
$currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
$currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
if ((Check-Admin) -eq $false)
{
if ($elevated)
{
#could not elevate, quit
}
else
{
Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
}
exit
}
$moduleToRemove = "A_Entities`r`nA_EntityTrigger"
$moduleToRemove|Set-Content 'C:\DeployablePackage\UninstallDeployablePackage\AOSService\Scripts\ModuleToRemove.txt' -Force
$uninstallFolder = 'C:\DeployablePackage\UninstallDeployablePackage\'
$runbookId = [Guid]::NewGuid()
$runbookFile = Join-Path $uninstallFolder "Runbooks\$runbookId.xml"
$topologyFile = Join-Path $uninstallFolder 'DefaultTopologyData.xml'
$updateInstaller = Join-Path $uninstallFolder 'AXUpdateInstaller.exe'
Function ExtractFiles
{
Unblock-File $file
Expand-Archive -LiteralPath $file -Destination $extracted
}
Function SetTopologyData
{
[xml]$xml = Get-Content $topologyFile
$machine = $xml.TopologyData.MachineList.Machine
# Set computer name
$machine.Name = $env:computername
#Set service models
$serviceModelList = $machine.ServiceModelList
$serviceModelList.RemoveAll()
$instalInfoDll = Join-Path $uninstallFolder 'Microsoft.Dynamics.AX.AXInstallationInfo.dll'
[void][System.Reflection.Assembly]::LoadFile($instalInfoDll)
$models = [Microsoft.Dynamics.AX.AXInstallationInfo.AXInstallationInfo]::GetInstalledServiceModel()
foreach ($name in $models.Name)
{
$element = $xml.CreateElement('string')
$element.InnerText = $name
$serviceModelList.AppendChild($element)
}
$xml.Save($topologyFile)
}
Function GenerateRunbook
{
$serviceModelFile = Join-Path $uninstallFolder 'DevInstallServiceModelData.xml'
& $updateInstaller generate "-runbookId=$runbookId" "-topologyFile=$topologyFile" "-serviceModelFile=$serviceModelFile" "-runbookFile=$runbookFile"
}
Function ImportRunbook
{
& $updateInstaller import "-runbookfile=$runbookFile"
}
Function ExecuteRunbook
{
& $updateInstaller execute "-runbookId=$runbookId"
}
Function RerunRunbook([int] $step)
{
& $updateInstaller execute "-runbookId=$runbookId" "-rerunstep=$step"
}
Function SetStepComplete([int] $step)
{
& $updateInstaller execute "-runbookId=$runbookId" "-setstepcomplete=$step"
}
Function ExportRunbook
{
& $updateInstaller export "-runbookId=$runbookId" "-runbookfile=$runbookFile"
}
Set-MpPreference -DisableRealtimeMonitoring $true
SetTopologyData
GenerateRunbook
ImportRunbook
ExecuteRunbook
Set-MpPreference -DisableRealtimeMonitoring $false