Repository navigation
Expand file tree
/
Copy pathCommonModule.ps1
More file actions
104 lines (84 loc) · 3.55 KB
/
Copy pathCommonModule.ps1
File metadata and controls
104 lines (84 loc) · 3.55 KB
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
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
New-Module -name CommonModule -ScriptBlock {
function EnableLogging() {
Param (
[Parameter(Mandatory=$true)]
[string]$path
)
$logPath = $path + "/logs/" + ((Get-Date).ToUniversalTime()).ToString("yyyyMMddTHHmmssfffffffZ") + "_log.txt"
$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
Start-Transcript -path $logPath -append
}
function ToSkip() {
Param (
[string]$Message,
[bool]$DebugMode
)
if ($DebugMode){
Write-Host ($Message)
$response = "y"
while ($true) {
$response = Read-Host ("Enter 'y' for yes (default), 'n' for no, 'a' to abort script")
if (!$response) {$response = "y"}
switch -Regex ($response) {
"(?i)^y(es)?$" {return $false}
"(?i)^n(o)?$" {return $true}
"(?i)^a(bort)?$" {exit}
}
}
}
}
function GetStorageName() {
Param (
[Parameter(Mandatory=$true)]
[string] $ResourceGroupName,
[Parameter(Mandatory=$true)]
[string] $Location,
[bool]$ChooseStorage
)
$storageName = $null
if($ChooseStorage) {
Write-Host("Enter name for storage account for resource group '$ResourceGroupName' in location '$Location'")
if (ToSkip "Use auto name generation?" $ChooseStorage) {
Write-Host("Name should contain only lowercase alphanumeric characters, at least 6 characters and maximum 24")
$storageName = Read-Host ("Enter storage account name")
return $storageName
}
}
$storageName = $null
$charsOnly = $ResourceGroupName -replace '[^a-zA-Z0-9]', ''
$substring = $charsOnly.Substring(0, (($charsOnly.Length, 14) | measure -Minimum).Minimum)
$storageName = $substring.ToLower() + "diag" + (Get-Random -Maximum 999999).ToString("000000");
return $storageName
}
function GetDiagnosticsConfigPath() {
Param (
[Parameter(Mandatory=$true)]
[string]$Path,
[Parameter(Mandatory=$true)]
[string]$VmId,
[Parameter(Mandatory=$true)]
[string]$OsType
)
$xmlConfigPath = $null
switch -regex ($OsType) {
"[Ww]indows" {$xmlConfigPath = $Path + "/winDiagConfig.xml"}
"[Ll]inux" {$xmlConfigPath = $Path + "/linuxDiagConfig.xml"}
default {throw [System.InvalidOperationException] "$OsType is not supported. Allowed values are 'Windows' or 'Linux' "}
}
$xmlConfig = [xml](Get-Content $xmlConfigPath)
$xmlConfig.WadCfg.DiagnosticMonitorConfiguration.Metrics.SetAttribute("resourceId", $VmId)
$tmpPath = [System.IO.Path]::GetTempFileName()
$xmlConfig.Save($tmpPath)
return $tmpPath
}
Export-ModuleMember -Function EnableLogging
Export-ModuleMember -Function ToSkip
Export-ModuleMember -Function GetStorageName
Export-ModuleMember -Function GetDiagnosticsConfigPath
} | Import-Module