Skip to content

Commit dcb5d6e

Browse files
committed
Tests for [AvoidUsingWMICmdlet Rule
1 parent 5e2a145 commit dcb5d6e

6 files changed

+61
-51
lines changed

Tests/Rules/AvoidUsingWMICmdlet.ps1

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#Script violates the rule because Get-CIMInstance is available on PS 3.0 and needs to use that
2+
3+
#requires -version 3.0
4+
5+
function TestFunction
6+
{
7+
Get-WmiObject -Class Win32_ComputerSystem
8+
9+
Invoke-WMIMethod -Path Win32_Process -Name Create -ArgumentList notepad.exe
10+
11+
Register-WMIEvent -Class Win32_ProcessStartTrace -SourceIdentifier "ProcessStarted"
12+
13+
Set-WMIInstance -Class Win32_Environment -Argument @{Name='MyEnvVar';VariableValue='VarValue';UserName='<SYSTEM>'}
14+
}
15+
16+
TestFunction
17+
18+
Remove-WmiObject -Class Win32_OperatingSystem -Verbose
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Import-Module PSScriptAnalyzer
2+
$WMIRuleName = "PSAvoidUsingWMICmdlet"
3+
$violationMessage = "File 'AvoidUsingWMICmdlet.ps1' uses WMI cmdlet. For PowerShell 3.0 and above, use CIM cmdlet which perform the same tasks as the WMI cmdlets. The CIM cmdlets comply with WS-Management (WSMan) standards and with the Common Information Model (CIM) standard, which enables the cmdlets to use the same techniques to manage Windows computers and those running other operating systems."
4+
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
5+
$violations = Invoke-ScriptAnalyzer $directory\AvoidUsingWMICmdlet.ps1 -IncludeRule $WMIRuleName
6+
$noViolations = Invoke-ScriptAnalyzer $directory\AvoidUsingWMICmdletNoViolations.ps1 -IncludeRule $WMIRuleName
7+
8+
Describe "AvoidUsingWMICmdlet" {
9+
Context "Script contains references to WMI cmdlets - Violation" {
10+
It "Have 5 WMI cmdlet Violations" {
11+
$violations.Count | Should Be 5
12+
}
13+
14+
It "has the correct description message for WMI rule violation" {
15+
$violations[0].Message | Should Be $violationMessage
16+
}
17+
}
18+
19+
Context "Script contains no calls to WMI cmdlet - No violation" {
20+
It "results in no rule violations" {
21+
$noViolations.Count | Should Be 0
22+
}
23+
}
24+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# No Rule violations since this script requires PS 2.0 and Get-CIMInstance is not available for this version
2+
# So using Get-WMIObject is OK
3+
4+
#requires -Version 2.0
5+
6+
Invoke-WMIMethod -Path Win32_Process -Name Create -ArgumentList notepad.exe
7+
8+
function TestFunction
9+
{
10+
Get-WmiObject -Class Win32_ComputerSystem
11+
12+
Register-WMIEvent -Class Win32_ProcessStartTrace -SourceIdentifier "ProcessStarted"
13+
14+
Set-WMIInstance -Class Win32_Environment -Argument @{Name='MyEnvVar';VariableValue='VarValue';UserName='<SYSTEM>'}
15+
}
16+
17+
TestFunction
18+
19+
Remove-WmiObject -Class Win32_OperatingSystem -Verbose

Tests/Rules/AvoidUsingWMIObjectCmdlet.ps1

Lines changed: 0 additions & 13 deletions
This file was deleted.

Tests/Rules/AvoidUsingWMIObjectCmdlet.tests.ps1

Lines changed: 0 additions & 24 deletions
This file was deleted.

Tests/Rules/AvoidUsingWMIObjectCmdletNoViolations.ps1

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)