diff --git a/Rules/AvoidUsingWMIObjectCmdlet.cs b/Rules/AvoidUsingWMICmdlet.cs
similarity index 78%
rename from Rules/AvoidUsingWMIObjectCmdlet.cs
rename to Rules/AvoidUsingWMICmdlet.cs
index 6c71f1646..079f0ec14 100644
--- a/Rules/AvoidUsingWMIObjectCmdlet.cs
+++ b/Rules/AvoidUsingWMICmdlet.cs
@@ -28,19 +28,19 @@
namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules
{
///
- /// AvoidUsingWMIObjectCmdlet: Verify that Get-WMIObject, Remove-WMIObject are not used
+ /// AvoidUsingWMICmdlet: Avoid Using Get-WMIObject, Remove-WMIObject, Invoke-WmiMethod, Register-WmiEvent, Set-WmiInstance
///
[Export(typeof(IScriptRule))]
- public class AvoidUsingWMIObjectCmdlet : IScriptRule
+ public class AvoidUsingWMICmdlet : IScriptRule
{
///
- /// AnalyzeScript: Verify that Get-WMIObject, Remove-WMIObject are not used
+ /// AnalyzeScript: Avoid Using Get-WMIObject, Remove-WMIObject, Invoke-WmiMethod, Register-WmiEvent, Set-WmiInstance
///
public IEnumerable AnalyzeScript(Ast ast, string fileName)
{
if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage);
- // Rule is applicable only when PowerShell Version is < 3.0, since Get-CIMInstance was introduced in 3.0
+ // Rule is applicable only when PowerShell Version is < 3.0, since CIM cmdlet was introduced in 3.0
int majorPSVersion = GetPSMajorVersion(ast);
if (!(3 > majorPSVersion && 0 < majorPSVersion))
{
@@ -50,9 +50,15 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName)
// Iterate all CommandAsts and check the command name
foreach (CommandAst cmdAst in commandAsts)
{
- if (cmdAst.GetCommandName() != null && (String.Equals(cmdAst.GetCommandName(), "get-wmiobject", StringComparison.OrdinalIgnoreCase) || String.Equals(cmdAst.GetCommandName(), "remove-wmiobject", StringComparison.OrdinalIgnoreCase)))
+ if (cmdAst.GetCommandName() != null &&
+ (String.Equals(cmdAst.GetCommandName(), "get-wmiobject", StringComparison.OrdinalIgnoreCase)
+ || String.Equals(cmdAst.GetCommandName(), "remove-wmiobject", StringComparison.OrdinalIgnoreCase)
+ || String.Equals(cmdAst.GetCommandName(), "invoke-wmimethod", StringComparison.OrdinalIgnoreCase)
+ || String.Equals(cmdAst.GetCommandName(), "register-wmievent", StringComparison.OrdinalIgnoreCase)
+ || String.Equals(cmdAst.GetCommandName(), "set-wmiinstance", StringComparison.OrdinalIgnoreCase))
+ )
{
- yield return new DiagnosticRecord(String.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingWMIObjectCmdletError, System.IO.Path.GetFileName(fileName)),
+ yield return new DiagnosticRecord(String.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingWMICmdletError, System.IO.Path.GetFileName(fileName)),
cmdAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
}
}
@@ -87,7 +93,7 @@ private int GetPSMajorVersion(Ast ast)
/// The name of this rule
public string GetName()
{
- return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidUsingWMIObjectCmdletName);
+ return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidUsingWMICmdletName);
}
///
@@ -96,7 +102,7 @@ public string GetName()
/// The common name of this rule
public string GetCommonName()
{
- return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingWMIObjectCmdletCommonName);
+ return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingWMICmdletCommonName);
}
///
@@ -105,7 +111,7 @@ public string GetCommonName()
/// The description of this rule
public string GetDescription()
{
- return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingWMIObjectCmdletDescription);
+ return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingWMICmdletDescription);
}
///
diff --git a/Rules/ScriptAnalyzerBuiltinRules.csproj b/Rules/ScriptAnalyzerBuiltinRules.csproj
index f84a2072d..7cc1c0446 100644
--- a/Rules/ScriptAnalyzerBuiltinRules.csproj
+++ b/Rules/ScriptAnalyzerBuiltinRules.csproj
@@ -66,7 +66,7 @@
-
+
diff --git a/Rules/Strings.resx b/Rules/Strings.resx
index f7501a45c..607777527 100644
--- a/Rules/Strings.resx
+++ b/Rules/Strings.resx
@@ -666,16 +666,16 @@
UseShouldProcessForStateChangingFunctions
-
- Avoid Using Get-WMIObject, Remove-WMIObject
+
+ Avoid Using Get-WMIObject, Remove-WMIObject, Invoke-WmiMethod, Register-WmiEvent, Set-WmiInstance
-
- Depricated. Starting in Windows PowerShell 3.0, these cmdlets have been superseded by CimInstance cmdlets.
+
+ Depricated. Starting in Windows PowerShell 3.0, these cmdlets have been superseded by CIM cmdlets.
-
- File '{0}' uses WMIObject cmdlet. For PowerShell 3.0 and above, this is not recommended because the cmdlet is based on a non-standard DCOM protocol. Use CIMInstance cmdlet instead. This is CIM and WS-Man standards compliant and works in a heterogeneous environment.
+
+ File '{0}' 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.
-
- AvoidUsingWMIObjectCmdlet
+
+ AvoidUsingWMICmdlet
\ No newline at end of file
diff --git a/Tests/Rules/AvoidUsingWMICmdlet.ps1 b/Tests/Rules/AvoidUsingWMICmdlet.ps1
new file mode 100644
index 000000000..81c03f64d
--- /dev/null
+++ b/Tests/Rules/AvoidUsingWMICmdlet.ps1
@@ -0,0 +1,18 @@
+#Script violates the rule because Get-CIMInstance is available on PS 3.0 and needs to use that
+
+#requires -version 3.0
+
+function TestFunction
+{
+ Get-WmiObject -Class Win32_ComputerSystem
+
+ Invoke-WMIMethod -Path Win32_Process -Name Create -ArgumentList notepad.exe
+
+ Register-WMIEvent -Class Win32_ProcessStartTrace -SourceIdentifier "ProcessStarted"
+
+ Set-WMIInstance -Class Win32_Environment -Argument @{Name='MyEnvVar';VariableValue='VarValue';UserName=''}
+}
+
+TestFunction
+
+Remove-WmiObject -Class Win32_OperatingSystem -Verbose
\ No newline at end of file
diff --git a/Tests/Rules/AvoidUsingWMICmdlet.tests.ps1 b/Tests/Rules/AvoidUsingWMICmdlet.tests.ps1
new file mode 100644
index 000000000..b8c7a6e44
--- /dev/null
+++ b/Tests/Rules/AvoidUsingWMICmdlet.tests.ps1
@@ -0,0 +1,24 @@
+Import-Module PSScriptAnalyzer
+$WMIRuleName = "PSAvoidUsingWMICmdlet"
+$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."
+$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
+$violations = Invoke-ScriptAnalyzer $directory\AvoidUsingWMICmdlet.ps1 -IncludeRule $WMIRuleName
+$noViolations = Invoke-ScriptAnalyzer $directory\AvoidUsingWMICmdletNoViolations.ps1 -IncludeRule $WMIRuleName
+
+Describe "AvoidUsingWMICmdlet" {
+ Context "Script contains references to WMI cmdlets - Violation" {
+ It "Have 5 WMI cmdlet Violations" {
+ $violations.Count | Should Be 5
+ }
+
+ It "has the correct description message for WMI rule violation" {
+ $violations[0].Message | Should Be $violationMessage
+ }
+ }
+
+ Context "Script contains no calls to WMI cmdlet - No violation" {
+ It "results in no rule violations" {
+ $noViolations.Count | Should Be 0
+ }
+ }
+}
\ No newline at end of file
diff --git a/Tests/Rules/AvoidUsingWMICmdletNoViolations.ps1 b/Tests/Rules/AvoidUsingWMICmdletNoViolations.ps1
new file mode 100644
index 000000000..51809547a
--- /dev/null
+++ b/Tests/Rules/AvoidUsingWMICmdletNoViolations.ps1
@@ -0,0 +1,19 @@
+# No Rule violations since this script requires PS 2.0 and Get-CIMInstance is not available for this version
+# So using Get-WMIObject is OK
+
+#requires -Version 2.0
+
+Invoke-WMIMethod -Path Win32_Process -Name Create -ArgumentList notepad.exe
+
+function TestFunction
+{
+ Get-WmiObject -Class Win32_ComputerSystem
+
+ Register-WMIEvent -Class Win32_ProcessStartTrace -SourceIdentifier "ProcessStarted"
+
+ Set-WMIInstance -Class Win32_Environment -Argument @{Name='MyEnvVar';VariableValue='VarValue';UserName=''}
+}
+
+TestFunction
+
+Remove-WmiObject -Class Win32_OperatingSystem -Verbose
\ No newline at end of file
diff --git a/Tests/Rules/AvoidUsingWMIObjectCmdlet.ps1 b/Tests/Rules/AvoidUsingWMIObjectCmdlet.ps1
deleted file mode 100644
index 13f0412c9..000000000
--- a/Tests/Rules/AvoidUsingWMIObjectCmdlet.ps1
+++ /dev/null
@@ -1,13 +0,0 @@
-#Script violates the rule because Get-CIMInstance is available on PS 3.0 and needs to use that
-
-#requires -version 3.0
-
-function TestFunction
-{
- Get-WmiObject -Class Win32_ComputerSystem
-
-}
-
-TestFunction
-
-Remove-WmiObject -Class Win32_OperatingSystem -Verbose
\ No newline at end of file
diff --git a/Tests/Rules/AvoidUsingWMIObjectCmdlet.tests.ps1 b/Tests/Rules/AvoidUsingWMIObjectCmdlet.tests.ps1
deleted file mode 100644
index 0a96ad16e..000000000
--- a/Tests/Rules/AvoidUsingWMIObjectCmdlet.tests.ps1
+++ /dev/null
@@ -1,24 +0,0 @@
-Import-Module PSScriptAnalyzer
-$wmiObjectRuleName = "PSAvoidUsingWMIObjectCmdlet"
-$violationMessage = "File 'AvoidUsingWMIObjectCmdlet.ps1' uses WMIObject cmdlet. For PowerShell 3.0 and above, this is not recommended because the cmdlet is based on a non-standard DCOM protocol. Use CIMInstance cmdlet instead. This is CIM and WS-Man standards compliant and works in a heterogeneous environment."
-$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
-$violations = Invoke-ScriptAnalyzer $directory\AvoidUsingWMIObjectCmdlet.ps1 | Where-Object {$_.RuleName -eq $wmiObjectRuleName}
-$noViolations = Invoke-ScriptAnalyzer $directory\AvoidUsingWMIObjectCmdletNoViolations.ps1 | Where-Object {$_.RuleName -eq $wmiObjectRuleName}
-
-Describe "AvoidUsingWMIObjectCmdlet" {
- Context "Script contains references to WMIObject cmdlets - Violation" {
- It "Have 2 WMIObject cmdlet Violations" {
- $violations.Count | Should Be 2
- }
-
- It "has the correct description message for WMIObject rule violation" {
- $violations[0].Message | Should Match $violationMessage
- }
- }
-
- Context "Script contains no calls to WMIObject cmdlet - No violation" {
- It "results in no rule violations" {
- $noViolations.Count | Should Be 0
- }
- }
-}
\ No newline at end of file
diff --git a/Tests/Rules/AvoidUsingWMIObjectCmdletNoViolations.ps1 b/Tests/Rules/AvoidUsingWMIObjectCmdletNoViolations.ps1
deleted file mode 100644
index b5d2e6f14..000000000
--- a/Tests/Rules/AvoidUsingWMIObjectCmdletNoViolations.ps1
+++ /dev/null
@@ -1,14 +0,0 @@
-# No Rule violations since this script requires PS 2.0 and Get-CIMInstance is not available for this version
-# So using Get-WMIObject is OK
-
-#requires -Version 2.0
-
-function TestFunction
-{
- Remove-WmiObject -Class Win32_ComputerSystem
-
-}
-
-TestFunction
-
-Get-WmiObject -Class Win32_OperatingSystem -Verbose
\ No newline at end of file