Skip to content

Replace deprecated wmic with PowerShell Get-ItemProperty in VsTest tasks (Issue #21123) #21129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions Tasks/VsTestV2/versionfinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,22 @@ export function getVsTestRunnerDetails(testConfig: models.TestConfigurations) {
return;
}

const vstestLocationEscaped = vstestexeLocation.replace(/\\/g, '\\\\');
const wmicTool = tl.tool('wmic');
const wmicArgs = ['datafile', 'where', 'name=\''.concat(vstestLocationEscaped, '\''), 'get', 'Version', '/Value'];
wmicTool.arg(wmicArgs);
let output = wmicTool.execSync({ silent: true } as tr.IExecSyncOptions).stdout;
// Use PowerShell Get-ItemProperty instead of deprecated wmic
const powershellTool = tl.tool('powershell');
const powershellArgs = ['-Command', `try { (Get-ItemProperty -Path '${vstestexeLocation}' -ErrorAction Stop).VersionInfo.FileVersion } catch { throw "Failed to get version info for ${vstestexeLocation}: $_" }`];
powershellTool.arg(powershellArgs);
let output = powershellTool.execSync({ silent: true } as tr.IExecSyncOptions).stdout;

if (utils.Helper.isNullOrWhitespace(output)) {
tl.error(tl.loc('ErrorReadingVstestVersion'));
throw new Error(tl.loc('ErrorReadingVstestVersion'));
}
output = output.trim();
tl.debug('VSTest Version information: ' + output);
const verSplitArray = output.split('=');
if (verSplitArray.length !== 2) {
tl.error(tl.loc('ErrorReadingVstestVersion'));
throw new Error(tl.loc('ErrorReadingVstestVersion'));
}

const versionArray = verSplitArray[1].split('.');
if (versionArray.length !== 4) {

// PowerShell returns version directly, no need to split by '='
const versionArray = output.split('.');
if (versionArray.length < 3) {
tl.warning(tl.loc('UnexpectedVersionString', output));
throw new Error(tl.loc('UnexpectedVersionString', output));
}
Expand All @@ -48,8 +44,8 @@ export function getVsTestRunnerDetails(testConfig: models.TestConfigurations) {
ci.publishEvent({ testplatform: `${majorVersion}.${minorVersion}.${patchNumber}` });

if (isNaN(majorVersion) || isNaN(minorVersion) || isNaN(patchNumber)) {
tl.warning(tl.loc('UnexpectedVersionNumber', verSplitArray[1]));
throw new Error(tl.loc('UnexpectedVersionNumber', verSplitArray[1]));
tl.warning(tl.loc('UnexpectedVersionNumber', output));
throw new Error(tl.loc('UnexpectedVersionNumber', output));
}

switch (majorVersion) {
Expand Down
26 changes: 11 additions & 15 deletions Tasks/VsTestV3/versionfinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,22 @@ export function getVsTestRunnerDetails(testConfig: models.TestConfigurations) {
return;
}

const vstestLocationEscaped = vstestexeLocation.replace(/\\/g, '\\\\');
const wmicTool = tl.tool('wmic');
const wmicArgs = ['datafile', 'where', 'name=\''.concat(vstestLocationEscaped, '\''), 'get', 'Version', '/Value'];
wmicTool.arg(wmicArgs);
let output = wmicTool.execSync({ silent: true } as tr.IExecSyncOptions).stdout;
// Use PowerShell Get-ItemProperty instead of deprecated wmic
const powershellTool = tl.tool('powershell');
const powershellArgs = ['-Command', `try { (Get-ItemProperty -Path '${vstestexeLocation}' -ErrorAction Stop).VersionInfo.FileVersion } catch { throw "Failed to get version info for ${vstestexeLocation}: $_" }`];
powershellTool.arg(powershellArgs);
let output = powershellTool.execSync({ silent: true } as tr.IExecSyncOptions).stdout;

if (utils.Helper.isNullOrWhitespace(output)) {
tl.error(tl.loc('ErrorReadingVstestVersion'));
throw new Error(tl.loc('ErrorReadingVstestVersion'));
}
output = output.trim();
tl.debug('VSTest Version information: ' + output);
const verSplitArray = output.split('=');
if (verSplitArray.length !== 2) {
tl.error(tl.loc('ErrorReadingVstestVersion'));
throw new Error(tl.loc('ErrorReadingVstestVersion'));
}

const versionArray = verSplitArray[1].split('.');
if (versionArray.length !== 4) {

// PowerShell returns version directly, no need to split by '='
const versionArray = output.split('.');
if (versionArray.length < 3) {
tl.warning(tl.loc('UnexpectedVersionString', output));
throw new Error(tl.loc('UnexpectedVersionString', output));
}
Expand All @@ -48,8 +44,8 @@ export function getVsTestRunnerDetails(testConfig: models.TestConfigurations) {
ci.publishEvent({ testplatform: `${majorVersion}.${minorVersion}.${patchNumber}` });

if (isNaN(majorVersion) || isNaN(minorVersion) || isNaN(patchNumber)) {
tl.warning(tl.loc('UnexpectedVersionNumber', verSplitArray[1]));
throw new Error(tl.loc('UnexpectedVersionNumber', verSplitArray[1]));
tl.warning(tl.loc('UnexpectedVersionNumber', output));
throw new Error(tl.loc('UnexpectedVersionNumber', output));
}

switch (majorVersion) {
Expand Down