|
1 | 1 | --- |
2 | 2 | title: Debug PowerShell scripts run by Custom Script Extension or Run Command |
3 | 3 | description: Troubleshoot PowerShell script failures when you run them remotely on a virtual machine by using the Custom Script Extension or Run Command feature. |
4 | | -ms.date: 03/15/2022 |
5 | | -ms.reviewer: clandis, v-leedennis |
| 4 | +ms.date: 10/23/2024 |
| 5 | +ms.reviewer: clandis, kegregoi, v-leedennis, v-weizhu |
6 | 6 | editor: v-jsitser |
7 | 7 | ms.service: azure-virtual-machines |
8 | 8 | ms.custom: sap:VM Admin - Windows (Guest OS), devx-track-azurepowershell |
@@ -121,76 +121,31 @@ wevtutil set-log "Microsoft-Windows-PowerShell/Operational" /ms:104857600 |
121 | 121 |
|
122 | 122 | ### Turn on process creation auditing |
123 | 123 |
|
124 | | -Use the [auditpol set](/windows-server/administration/windows-commands/auditpol-set) command, [New-Item](/powershell/module/microsoft.powershell.management/new-item) cmdlet, and [New-ItemProperty](/powershell/module/microsoft.powershell.management/new-itemproperty) cmdlet to turn on process creation auditing: |
| 124 | +Use the following command to turn on process creation auditing: |
125 | 125 |
|
126 | | -```powershell |
127 | | -auditpol /set /category:"detailed tracking" /success:enable /subcategory:"Process Creation" |
128 | | -
|
129 | | -$registryPath = @{ |
130 | | - Path = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System\Audit' |
131 | | -} |
132 | | -New-Item @registryPath |
133 | | -
|
134 | | -$forceDwordOne = @{ |
135 | | - PropertyType = 'DWord' |
136 | | - Value = 1 |
137 | | - Force = $True |
138 | | -} |
139 | | -New-ItemProperty @registryPath -Name 'ProcessCreationIncludeCmdLine_Enabled' @forceDwordOne |
| 126 | +```console |
| 127 | +reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System\Audit" /v "ProcessCreationIncludeCmdLine_Enabled" /t REG_DWORD /d 1 /f |
140 | 128 | ``` |
141 | 129 |
|
142 | 130 | ### Turn on PowerShell transcription |
143 | 131 |
|
144 | | -```powershell |
145 | | -$registryPath = @{ |
146 | | - Path = 'HKLM:\Software\Policies\Microsoft\Windows\PowerShell\Transcription' |
147 | | - Force = $True |
148 | | -} |
149 | | -New-Item @registryPath |
150 | | -
|
151 | | -$dwordOne = @{ |
152 | | - PropertyType = 'DWord' |
153 | | - Value = 1 |
154 | | -} |
155 | | -New-ItemProperty @registryPath -Name 'EnableTranscripting' @dwordOne |
156 | | -New-ItemProperty @registryPath -Name 'EnableInvocationHeader' @dwordOne |
157 | | -New-ItemProperty @registryPath -Name 'OutputDirectory' -PropertyType 'String' -Value 'C:\Transcripts' |
| 132 | +```console |
| 133 | +reg add "HKEY_LOCAL_MACHINE\Software\Policies\Microsoft\Windows\PowerShell\Transcription" /v "EnableTranscripting" /t REG_DWORD /d 1 /f |
| 134 | +reg add "HKEY_LOCAL_MACHINE\Software\Policies\Microsoft\Windows\PowerShell\Transcription" /v "EnableInvocationHeader" /t REG_DWORD /d 1 /f |
| 135 | +reg add "HKEY_LOCAL_MACHINE\Software\Policies\Microsoft\Windows\PowerShell\Transcription" /v "OutputDirectory" /t REG_SZ /d C:\Transcripts /f |
158 | 136 | ``` |
159 | 137 |
|
160 | 138 | ### Turn on PowerShell module logging |
161 | 139 |
|
162 | | -```powershell |
163 | | -$moduleLoggingPath = @{ |
164 | | - Path = 'HKLM:\Software\Policies\Microsoft\Windows\PowerShell\ModuleLogging' |
165 | | -} |
166 | | -New-Item @moduleLoggingPath |
167 | | -
|
168 | | -$forceDwordOne = @{ |
169 | | - PropertyType = 'DWord' |
170 | | - Value = 1 |
171 | | - Force = $True |
172 | | -} |
173 | | -New-ItemProperty @moduleLoggingPath -Name 'EnableModuleLogging' @forceDwordOne |
174 | | -
|
175 | | -$moduleNamesPath = 'HKLM:\SOFTWARE\Policies\Microsoft\Windows\PowerShell\ModuleLogging\ModuleNames' |
176 | | -New-Item -Path $moduleNamesPath |
177 | | -New-ItemProperty -Path $moduleNamesPath -Name '*' -PropertyType String -Value '*' |
| 140 | +```console |
| 141 | +reg add "HKEY_LOCAL_MACHINE\Software\Policies\Microsoft\Windows\PowerShell\ModuleLogging" /v "EnableModuleLogging" /t REG_DWORD /d 1 /f |
| 142 | +reg add "HKEY_LOCAL_MACHINE\Software\Policies\Microsoft\Windows\PowerShell\ModuleLogging\ModuleNames" /v "*" /t REG_SZ /d * |
178 | 143 | ``` |
179 | 144 |
|
180 | 145 | ### Turn on PowerShell script block logging |
181 | 146 |
|
182 | | -```powershell |
183 | | -$registryPath = @{ |
184 | | - Path = 'HKLM:\Software\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging' |
185 | | -} |
186 | | -New-Item @registryPath |
187 | | -
|
188 | | -$forceDwordOne = @{ |
189 | | - PropertyType = 'DWord' |
190 | | - Value = 1 |
191 | | - Force = $True |
192 | | -} |
193 | | -New-ItemProperty @registryPath -Name 'EnableScriptBlockLogging' @forceDwordOne |
| 147 | +```console |
| 148 | +reg add "HKEY_LOCAL_MACHINE\Software\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging" /v "EnableScriptBlockLogging" /t REG_DWORD /d 1 /f |
194 | 149 | ``` |
195 | 150 |
|
196 | 151 | ## Understand the output |
@@ -294,65 +249,29 @@ To undo the changes that you made to enable the logging of PowerShell scripting |
294 | 249 |
|
295 | 250 | 1. Turn off process creation auditing: |
296 | 251 |
|
297 | | - ```powershell |
298 | | - auditpol /set /category:"detailed tracking" /success:disable /subcategory:"Process Creation" |
299 | | - $commandSettings = @{ |
300 | | - Path = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System\Audit' |
301 | | - Name = 'ProcessCreationIncludeCmdLine_Enabled' |
302 | | - PropertyType = 'DWord' |
303 | | - Value = 0 |
304 | | - Force = $True |
305 | | - } |
306 | | - New-ItemProperty @commandSettings |
| 252 | + ```console |
| 253 | + reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System\Audit" /v "ProcessCreationIncludeCmdLine_Enabled" /t REG_DWORD /d 0 /f |
307 | 254 | ``` |
308 | 255 |
|
309 | 256 | 1. Turn off transcription: |
310 | 257 |
|
311 | | - ```powershell |
312 | | - $registryPath = @{ |
313 | | - Path = 'HKLM:\SOFTWARE\Policies\Microsoft\Windows\PowerShell\Transcription' |
314 | | - } |
315 | | - $forceDwordZero = @{ |
316 | | - PropertyType = 'DWord' |
317 | | - Value = 0 |
318 | | - Force = $True |
319 | | - } |
320 | | - New-ItemProperty @registryPath -Name 'EnableTranscripting' @forceDwordZero |
321 | | - New-ItemProperty @registryPath -Name 'EnableInvocationHeader' @forceDwordZero |
322 | | - Remove-ItemProperty @registryPath -Name 'OutputDirectory' |
| 258 | + ```console |
| 259 | + reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\PowerShell\Transcription" /v "EnableTranscripting" /t REG_DWORD /d 0 /f |
| 260 | + reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\PowerShell\Transcription" /v "EnableInvocationHeader" /t REG_DWORD /d 0 /f |
| 261 | + reg delete "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\PowerShell\Transcription" /v "OutputDirectory" |
323 | 262 | ``` |
324 | 263 |
|
325 | 264 | 1. Turn off module logging: |
326 | 265 |
|
327 | | - ```powershell |
328 | | - $moduleLoggingPath = @{ |
329 | | - Path = 'HKLM:\Software\Policies\Microsoft\Windows\PowerShell\ModuleLogging' |
330 | | - } |
331 | | - $forceDwordZero = @{ |
332 | | - PropertyType = 'DWord' |
333 | | - Value = 0 |
334 | | - Force = $True |
335 | | - } |
336 | | - New-ItemProperty @moduleLoggingPath -Name 'EnableModuleLogging' @forceDwordZero |
337 | | -
|
338 | | - $moduleNamesPath = @{ |
339 | | - Path = 'HKLM:\SOFTWARE\Policies\Microsoft\Windows\PowerShell\ModuleLogging\ModuleNames' |
340 | | - } |
341 | | - Remove-ItemProperty @moduleNamesPath -Name '*' |
| 266 | + ```console |
| 267 | + reg add "HKEY_LOCAL_MACHINE\Software\Policies\Microsoft\Windows\PowerShell\ModuleLogging" /v "EnableModuleLogging" /t REG_DWORD /d 0 /f |
| 268 | + reg delete "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\PowerShell\ModuleLogging\ModuleNames" /v "*" |
342 | 269 | ``` |
343 | 270 |
|
344 | 271 | 1. Turn off script block logging: |
345 | 272 |
|
346 | | - ```powershell |
347 | | - $registryPath = @{ |
348 | | - Path = 'HKLM:\Software\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging' |
349 | | - } |
350 | | - $forceDwordZero = @{ |
351 | | - PropertyType = 'DWord' |
352 | | - Value = 0 |
353 | | - Force = $True |
354 | | - } |
355 | | - New-ItemProperty @registryPath -Name 'EnableScriptBlockLogging' @forceDwordZero |
| 273 | + ```console |
| 274 | + reg add "HKEY_LOCAL_MACHINE\Software\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging" /v "EnableScriptBlockLogging" /t REG_DWORD /d 0 /f |
356 | 275 | ``` |
357 | 276 |
|
358 | 277 | 1. Remove the transcription folder: |
|
0 commit comments