Skip to content

wmic removal #1721 - #1722

Open
Alexander Gottwald (ago1024) wants to merge 2 commits into
microsoft:mainfrom
ago1024:main
Open

Alexander Gottwald (ago1024) wants to merge 2 commits into
microsoft:mainfrom
ago1024:main

Conversation

@ago1024

Copy link
Copy Markdown

The PR replaces the call to wmic.exe with a call to powershell Get-CimInstance Win32_process.

The output of the script is similar to the old wmic output. CreationDate was omited because it's not used anywhere and the old parsing logic to parse the date does not work with the input from the script.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@ago1024

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

Comment thread src/processTree.ts
proc = spawn(wmic, [ 'process', 'get', 'CommandLine,CreationDate,ParentProcessId,ProcessId' ]);
const command = 'Get-CimInstance Win32_Process | ForEach-Object { "$($_.CommandLine) $($_.ParentProcessId) $($_.ProcessId)" }';
const powershell = join(process.env['WINDIR'] || 'C:\\Windows', 'System32', 'WindowsPowerShell', 'v1.0', 'powershell.exe');
proc = spawn(powershell, [ '-Command', command ]);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep this background query independent of user profiles and prevent interactive prompts:

Suggested change
proc = spawn(powershell, [ '-Command', command ]);
proc = spawn(powershell, [ '-NoLogo', '-NoProfile', '-NonInteractive', '-Command', command ], { windowsHide: true });

Comment thread src/processTree.ts

const wmic = join(process.env['WINDIR'] || 'C:\\Windows', 'System32', 'wbem', 'WMIC.exe');
proc = spawn(wmic, [ 'process', 'get', 'CommandLine,CreationDate,ParentProcessId,ProcessId' ]);
const command = 'Get-CimInstance Win32_Process | ForEach-Object { "$($_.CommandLine) $($_.ParentProcessId) $($_.ProcessId)" }';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make stdout UTF-8 before decoding it as UTF-8. This keeps already-UTF-8 constrained sessions working and reports a port-attach workaround when policy prevents changing the encoding:

Suggested change
const command = 'Get-CimInstance Win32_Process | ForEach-Object { "$($_.CommandLine) $($_.ParentProcessId) $($_.ProcessId)" }';
const command = `
$ErrorActionPreference = 'Stop'
if ([Console]::OutputEncoding.CodePage -ne 65001) {
if ($ExecutionContext.SessionState.LanguageMode -ne 'FullLanguage') {
throw 'UTF-8 process discovery is restricted by policy. Attach using hostName and port instead.'
}
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
}
Get-CimInstance Win32_Process | ForEach-Object { "$($_.CommandLine) $($_.ParentProcessId) $($_.ProcessId)" }
`;

@wenytang-ms

Copy link
Copy Markdown
Contributor

Alexander Gottwald (@ago1024) Thanks for submitting the PR! I left two inline suggestions for the PowerShell invocation and output encoding. Please take a look when you have a chance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants