Skip to content

Commit ba11d63

Browse files
committed
updated logging
1 parent 3960ea3 commit ba11d63

File tree

1 file changed

+79
-53
lines changed

1 file changed

+79
-53
lines changed

workload/scripts/Set-SessionHostConfiguration.ps1

+79-53
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,47 @@ Param(
3636
# $ScreenCaptureProtection
3737
)
3838

39-
##############################################################
40-
# Functions
41-
##############################################################
42-
function Write-Log {
43-
param(
44-
[parameter(Mandatory)]
45-
[string]$Message,
39+
function New-Log {
40+
Param (
41+
[Parameter(Mandatory = $true, Position = 0)]
42+
[string] $Path
43+
)
44+
45+
$date = Get-Date -UFormat "%Y-%m-%d %H-%M-%S"
46+
Set-Variable logFile -Scope Script
47+
$script:logFile = "$Script:Name-$date.log"
48+
49+
if ((Test-Path $path ) -eq $false) {
50+
$null = New-Item -Path $path -type directory
51+
}
52+
53+
$script:Log = Join-Path $path $logfile
54+
55+
Add-Content $script:Log "Date`t`t`tCategory`t`tDetails"
56+
}
4657

47-
[parameter(Mandatory)]
48-
[string]$Type
58+
function Write-Log {
59+
Param (
60+
[Parameter(Mandatory = $false, Position = 0)]
61+
[ValidateSet("Info", "Warning", "Error")]
62+
$Category = 'Info',
63+
[Parameter(Mandatory = $true, Position = 1)]
64+
$Message
4965
)
50-
$Path = 'C:\Windows\Temp\AVDSessionHostConfig.log'
51-
if (!(Test-Path -Path $Path)) {
52-
New-Item -Path 'C:\' -Name 'AVDSessionHostConfig.log' | Out-Null
66+
67+
$Date = get-date
68+
$Content = "[$Date]`t$Category`t`t$Message`n"
69+
Add-Content $Script:Log $content -ErrorAction Stop
70+
If ($Verbose) {
71+
Write-Verbose $Content
72+
}
73+
Else {
74+
Switch ($Category) {
75+
'Info' { Write-Host $content }
76+
'Error' { Write-Error $Content }
77+
'Warning' { Write-Warning $Content }
78+
}
5379
}
54-
$Timestamp = Get-Date -Format 'MM/dd/yyyy HH:mm:ss.ff'
55-
$Entry = '[' + $Timestamp + '] [' + $Type + '] ' + $Message
56-
$Entry | Out-File -FilePath $Path -Append
5780
}
5881

5982
function Get-WebFile {
@@ -78,51 +101,53 @@ function Get-WebFile {
78101
Function Set-RegistryValue {
79102
[CmdletBinding()]
80103
param (
81-
[Parameter()]
82-
[string]
83-
$Name,
84-
[Parameter()]
85-
[string]
86-
$Path,
87-
[Parameter()]
88-
[string]$PropertyType,
89-
[Parameter()]
90-
$Value
104+
[Parameter()]
105+
[string]
106+
$Name,
107+
[Parameter()]
108+
[string]
109+
$Path,
110+
[Parameter()]
111+
[string]$PropertyType,
112+
[Parameter()]
113+
$Value
91114
)
92115
Begin {
93-
Write-Log -message "[Set-RegistryValue]: Setting Registry Value: $Name"
116+
Write-Log -message "[Set-RegistryValue]: Setting Registry Value: $Name"
94117
}
95118
Process {
96-
# Create the registry Key(s) if necessary.
97-
If (!(Test-Path -Path $Path)) {
98-
Write-Log -message "[Set-RegistryValue]: Creating Registry Key: $Path"
99-
New-Item -Path $Path -Force | Out-Null
100-
}
101-
# Check for existing registry setting
102-
$RemoteValue = Get-ItemProperty -Path $Path -Name $Name -ErrorAction SilentlyContinue
103-
If ($RemoteValue) {
104-
# Get current Value
105-
$CurrentValue = Get-ItemPropertyValue -Path $Path -Name $Name
106-
Write-Log -message "[Set-RegistryValue]: Current Value of $($Path)\$($Name) : $CurrentValue"
107-
If ($Value -ne $CurrentValue) {
108-
Write-Log -message "[Set-RegistryValue]: Setting Value of $($Path)\$($Name) : $Value"
109-
Set-ItemProperty -Path $Path -Name $Name -Value $Value -Force | Out-Null
110-
} Else {
111-
Write-Log -message "[Set-RegistryValue]: Value of $($Path)\$($Name) is already set to $Value"
112-
}
113-
}
114-
Else {
115-
Write-Log -message "[Set-RegistryValue]: Setting Value of $($Path)\$($Name) : $Value"
116-
New-ItemProperty -Path $Path -Name $Name -PropertyType $PropertyType -Value $Value -Force | Out-Null
117-
}
118-
Start-Sleep -Milliseconds 500
119+
# Create the registry Key(s) if necessary.
120+
If (!(Test-Path -Path $Path)) {
121+
Write-Log -message "[Set-RegistryValue]: Creating Registry Key: $Path"
122+
New-Item -Path $Path -Force | Out-Null
123+
}
124+
# Check for existing registry setting
125+
$RemoteValue = Get-ItemProperty -Path $Path -Name $Name -ErrorAction SilentlyContinue
126+
If ($RemoteValue) {
127+
# Get current Value
128+
$CurrentValue = Get-ItemPropertyValue -Path $Path -Name $Name
129+
Write-Log -message "[Set-RegistryValue]: Current Value of $($Path)\$($Name) : $CurrentValue"
130+
If ($Value -ne $CurrentValue) {
131+
Write-Log -message "[Set-RegistryValue]: Setting Value of $($Path)\$($Name) : $Value"
132+
Set-ItemProperty -Path $Path -Name $Name -Value $Value -Force | Out-Null
133+
}
134+
Else {
135+
Write-Log -message "[Set-RegistryValue]: Value of $($Path)\$($Name) is already set to $Value"
136+
}
137+
}
138+
Else {
139+
Write-Log -message "[Set-RegistryValue]: Setting Value of $($Path)\$($Name) : $Value"
140+
New-ItemProperty -Path $Path -Name $Name -PropertyType $PropertyType -Value $Value -Force | Out-Null
141+
}
142+
Start-Sleep -Milliseconds 500
119143
}
120144
End {
121145
}
122-
}
146+
}
123147

124148
$ErrorActionPreference = 'Stop'
125-
149+
$Script:Name = 'Set-SessionHostConfiguration'
150+
New-Log -Path (Join-Path -Path $env:SystemRoot -ChildPath 'Logs')
126151
try {
127152

128153
##############################################################
@@ -333,7 +358,8 @@ try {
333358
$CMDKey = Start-Process -FilePath 'cmdkey.exe' -ArgumentList "/add:$FSLogixStorageFQDN /user:localhost\$SAName /pass:$FSLogixStorageAccountKey" -Wait -PassThru
334359
If ($CMDKey.ExitCode -ne 0) {
335360
Write-Log -Message "CMDKey Failed with '$($CMDKey.ExitCode)'. Failed to add Local Storage Account Key for '$FSLogixStorageFQDN' to Credential Manager" -Type 'ERROR'
336-
} Else {
361+
}
362+
Else {
337363
Write-Log -Message "Successfully added Local Storage Account Key for '$FSLogixStorageFQDN' to Credential Manager" -Type 'INFO'
338364
}
339365
$Settings += @(
@@ -384,7 +410,7 @@ try {
384410

385411
# Resize OS Disk
386412
Write-Log -message "Resizing OS Disk"
387-
$driveLetter = $env:SystemDrive.Substring(0,1)
413+
$driveLetter = $env:SystemDrive.Substring(0, 1)
388414
$size = Get-PartitionSupportedSize -DriveLetter $driveLetter
389415
Resize-Partition -DriveLetter $driveLetter -Size $size.SizeMax
390416
Write-Log -message "OS Disk Resized"

0 commit comments

Comments
 (0)