|
| 1 | +// |
| 2 | +// Copyright (c) Microsoft Corporation. |
| 3 | +// |
| 4 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 5 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 6 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 7 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 8 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 9 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 10 | +// THE SOFTWARE. |
| 11 | +// |
| 12 | + |
| 13 | +using System; |
| 14 | +using System.Collections.ObjectModel; |
| 15 | +using System.Collections.Generic; |
| 16 | +using System.Linq; |
| 17 | +using System.Text; |
| 18 | +using System.Threading.Tasks; |
| 19 | +using System.Management.Automation; |
| 20 | +using System.Management.Automation.Language; |
| 21 | +using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic; |
| 22 | +using System.ComponentModel.Composition; |
| 23 | +using System.Resources; |
| 24 | +using System.Globalization; |
| 25 | +using System.Threading; |
| 26 | +using System.Reflection; |
| 27 | + |
| 28 | +namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules |
| 29 | +{ |
| 30 | + /// <summary> |
| 31 | + /// AvoidUsingWMIObjectCmdlet: Verify that Get-WMIObject, Remove-WMIObject are not used |
| 32 | + /// </summary> |
| 33 | + [Export(typeof(IScriptRule))] |
| 34 | + public class AvoidUsingWMIObjectCmdlet : IScriptRule |
| 35 | + { |
| 36 | + /// <summary> |
| 37 | + /// AnalyzeScript: Verify that Get-WMIObject, Remove-WMIObject are not used |
| 38 | + /// </summary> |
| 39 | + public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName) |
| 40 | + { |
| 41 | + if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage); |
| 42 | + |
| 43 | + // Rule is applicable only when PowerShell Version is < 3.0, since Get-CIMInstance was introduced in 3.0 |
| 44 | + int majorPSVersion = GetPSMajorVersion(ast); |
| 45 | + if (!(3 > majorPSVersion && 0 < majorPSVersion)) |
| 46 | + { |
| 47 | + // Finds all CommandAsts. |
| 48 | + IEnumerable<Ast> commandAsts = ast.FindAll(testAst => testAst is CommandAst, true); |
| 49 | + |
| 50 | + // Iterate all CommandAsts and check the command name |
| 51 | + foreach (CommandAst cmdAst in commandAsts) |
| 52 | + { |
| 53 | + if (cmdAst.GetCommandName() != null && (String.Equals(cmdAst.GetCommandName(), "get-wmiobject", StringComparison.OrdinalIgnoreCase) || String.Equals(cmdAst.GetCommandName(), "remove-wmiobject", StringComparison.OrdinalIgnoreCase))) |
| 54 | + { |
| 55 | + yield return new DiagnosticRecord(String.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingWMIObjectCmdletError, System.IO.Path.GetFileName(fileName)), |
| 56 | + cmdAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// GetPSMajorVersion: Retrieves Major PowerShell Version when supplied using #requires keyword in the script |
| 64 | + /// </summary> |
| 65 | + /// <returns>The name of this rule</returns> |
| 66 | + private int GetPSMajorVersion(Ast ast) |
| 67 | + { |
| 68 | + if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage); |
| 69 | + |
| 70 | + IEnumerable<Ast> scriptBlockAsts = ast.FindAll(testAst => testAst is ScriptBlockAst, true); |
| 71 | + |
| 72 | + foreach (ScriptBlockAst scriptBlockAst in scriptBlockAsts) |
| 73 | + { |
| 74 | + if (null != scriptBlockAst.ScriptRequirements && null != scriptBlockAst.ScriptRequirements.RequiredPSVersion) |
| 75 | + { |
| 76 | + return scriptBlockAst.ScriptRequirements.RequiredPSVersion.Major; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // return a non valid Major version if #requires -Version is not supplied in the Script |
| 81 | + return -1; |
| 82 | + } |
| 83 | + |
| 84 | + /// <summary> |
| 85 | + /// GetName: Retrieves the name of this rule. |
| 86 | + /// </summary> |
| 87 | + /// <returns>The name of this rule</returns> |
| 88 | + public string GetName() |
| 89 | + { |
| 90 | + return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidUsingWMIObjectCmdletName); |
| 91 | + } |
| 92 | + |
| 93 | + /// <summary> |
| 94 | + /// GetCommonName: Retrieves the common name of this rule. |
| 95 | + /// </summary> |
| 96 | + /// <returns>The common name of this rule</returns> |
| 97 | + public string GetCommonName() |
| 98 | + { |
| 99 | + return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingWMIObjectCmdletCommonName); |
| 100 | + } |
| 101 | + |
| 102 | + /// <summary> |
| 103 | + /// GetDescription: Retrieves the description of this rule. |
| 104 | + /// </summary> |
| 105 | + /// <returns>The description of this rule</returns> |
| 106 | + public string GetDescription() |
| 107 | + { |
| 108 | + return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingWMIObjectCmdletDescription); |
| 109 | + } |
| 110 | + |
| 111 | + /// <summary> |
| 112 | + /// GetSourceType: Retrieves the type of the rule: builtin, managed or module. |
| 113 | + /// </summary> |
| 114 | + public SourceType GetSourceType() |
| 115 | + { |
| 116 | + return SourceType.Builtin; |
| 117 | + } |
| 118 | + |
| 119 | + /// <summary> |
| 120 | + /// GetSourceName: Retrieves the module/assembly name the rule is from. |
| 121 | + /// </summary> |
| 122 | + public string GetSourceName() |
| 123 | + { |
| 124 | + return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments