-
Notifications
You must be signed in to change notification settings - Fork 209
/
CloudAPIPowerShellManagement.psm1
175 lines (143 loc) · 4.46 KB
/
CloudAPIPowerShellManagement.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
#region Console functions
# https://stackoverflow.com/questions/40617800/opening-powershell-script-and-hide-command-prompt-but-not-the-gui
Add-Type -Name Window -Namespace Console -MemberDefinition '
[DllImport("Kernel32.dll")]
public static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool SetConsoleIcon(IntPtr hIcon);
[DllImport("user32.dll")]
public static extern int SendMessage(int hWnd, uint wMsg, uint wParam, IntPtr lParam);
'
function Show-Console
{
$consolePtr = [Console.Window]::GetConsoleWindow()
# Hide = 0,
# ShowNormal = 1,
# ShowMinimized = 2,
# ShowMaximized = 3,
# Maximize = 3,
# ShowNormalNoActivate = 4,
# Show = 5,
# Minimize = 6,
# ShowMinNoActivate = 7,
# ShowNoActivate = 8,
# Restore = 9,
# ShowDefault = 10,
# ForceMinimized = 11
[Console.Window]::ShowWindow($consolePtr, 4)
}
function Hide-Console
{
$consolePtr = [Console.Window]::GetConsoleWindow()
#0 hide
[Console.Window]::ShowWindow($consolePtr, 0) | Out-Null
}
#endregion
# Unblock all files
# Not 100% OK but avoid issues with loading blocked files
function Unblock-AllFiles
{
param($folder)
(Get-ChildItem $folder -force | Where-Object {! $_.PSIsContainer}) | Unblock-File
foreach($subFolder in (Get-ChildItem $folder -force | Where-Object {$_.PSIsContainer}))
{
Unblock-AllFiles $subFolder.FullName
}
}
function Initialize-CloudAPIManagement
{
[CmdletBinding(SupportsShouldProcess=$True)]
param(
[string]
$View = "",
[switch]
$ShowConsoleWindow,
[switch]
$JSonSettings,
[string]
$JSonFile,
[switch]
$Silent,
[string]
$SilentBatchFile,
[string]
$tenantId,
[string]
$appId,
[string]
$secret,
[string]
$certificate
)
$PSModuleAutoloadingPreference = "none"
$global:wpfNS = "xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'"
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
Add-Type -AssemblyName PresentationFramework
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$global:hideUI = ($Silent -eq $true)
$global:SilentBatchFile = $SilentBatchFile
if($tenantId)
{
$global:AzureAppId = $appId
$global:ClientSecret = $secret
$global:ClientCert = $certificate
}
if($global:hideUI -ne $true)
{
# Run with UI
try
{
[xml]$xaml = Get-Content ([IO.Path]::GetDirectoryName($PSCommandPath) + "\Xaml\SplashScreen.xaml")
$global:SplashScreen = ([Windows.Markup.XamlReader]::Load((New-Object System.Xml.XmlNodeReader $xaml)))
$global:txtSplashTitle = $global:SplashScreen.FindName("txtSplashTitle")
$global:txtSplashText = $global:SplashScreen.FindName("txtSplashText")
$global:txtSplashTitle.Text = ("Initializing Cloud API PowerShell Management")
$global:SplashScreen.Show() | Out-Null
[System.Windows.Forms.Application]::DoEvents()
}
catch
{
}
}
else
{
# Run silent
if(-not $tenantId)
{
# Core module not loaded yet so can't use log function
Write-Error "Tenant Id is missing. Use -TenantId <Tenant-guid> on the command line to run silent batch jobs"
return
}
}
$global:TenantId = $tenantId
if($ShowConsoleWindow -ne $true)
{
Hide-Console
}
if($JSonSettings -eq $true)
{
$global:UseJSonSettings = $true
$global:JSonSettingFile = $JSonFile
}
else
{
$global:UseJSonSettings = $false
}
if($global:hideUI -ne $true)
{
$global:txtSplashText.Text = "Unblock files"
}
[System.Windows.Forms.Application]::DoEvents()
Unblock-AllFiles $PSScriptRoot
if($global:hideUI -ne $true)
{
$global:txtSplashText.Text = "Load core module"
}
[System.Windows.Forms.Application]::DoEvents()
Import-Module ($PSScriptRoot + "\Core.psm1") -Force -Global
Start-CoreApp $View
}