1
1
# Copyright (c) Microsoft Corporation. All rights reserved.
2
2
# Licensed under the MIT License.
3
3
4
+ $script :RunningInCIOnUbuntu = $IsLinux -and ($env: TF_BUILD -or $env: APPVEYOR ) # some test cases randomly start and stop to fail in Ubuntu CI tests
4
5
$script :RuleName = ' PSUseCompatibleCommands'
5
6
$script :AnyProfileConfigKey = ' AnyProfilePath'
6
7
$script :TargetProfileConfigKey = ' TargetProfiles'
@@ -23,6 +24,7 @@ $script:CompatibilityTestCases = @(
23
24
@ { Target = $script :Srv2012_3_profile ; Script = ' "Hello World" | ConvertFrom-String | Get-Member' ; Commands = @ (" ConvertFrom-String" ); Version = " 3.0" ; OS = " Windows" ; ProblemCount = 1 }
24
25
@ { Target = $script :Srv2012_3_profile ; Script = ' Compress-Archive -LiteralPath C:\Reference\Draftdoc.docx, C:\Reference\Images\diagram2.vsd -CompressionLevel Optimal -DestinationPath C:\Archives\Draft.Zip' ; Commands = @ (" Compress-Archive" ); Version = " 3.0" ; OS = " Windows" ; ProblemCount = 1 }
25
26
@ { Target = $script :Srv2012_3_profile ; Script = ' Get-Runspace -Id 2' ; Commands = @ (" Get-Runspace" ); Version = " 3.0" ; OS = " Windows" ; ProblemCount = 1 }
27
+ @ {
Target = $script :Srv2012_3_profile ;
Script = ' $Protected = "Hello World" | Protect-CmsMessage -To "*[email protected] *"' ;
Commands = @ (
" Protect-CmsMessage" );
Version = " 3.0" ;
OS = " Windows" ;
ProblemCount = 1 }
26
28
@ { Target = $script :Srv2012_3_profile ; Script = ' Format-Hex -Path "C:\temp\temp.t7f"' ; Commands = @ (" Format-Hex" ); Version = " 3.0" ; OS = " Windows" ; ProblemCount = 1 }
27
29
@ { Target = $script :Srv2012_3_profile ; Script = ' Set-Clipboard -Value "This is a test string"' ; Commands = @ (" Set-Clipboard" ); Version = " 3.0" ; OS = " Windows" ; ProblemCount = 1 }
28
30
@ { Target = $script :Srv2012_3_profile ; Script = ' Clear-RecycleBin -Force' ; Commands = @ (" Clear-RecycleBin" ); Version = " 3.0" ; OS = " Windows" ; ProblemCount = 1 }
@@ -62,7 +64,6 @@ $script:CompatibilityTestCases = @(
62
64
@ { Target = $script :Srv2019_5_profile ; Script = ' gci .' ; Commands = @ (); Version = " 5.1" ; OS = " Windows" ; ProblemCount = 0 }
63
65
@ { Target = $script :Srv2019_5_profile ; Script = ' iex $expr | % { Transform $_ }' ; Commands = @ (); Version = " 5.1" ; OS = " Windows" ; ProblemCount = 0 }
64
66
@ { Target = $script :Srv2019_5_profile ; Script = ' fhx $filePath' ; Commands = @ (); Version = " 5.1" ; OS = " Windows" ; ProblemCount = 0 }
65
-
66
67
@ { Target = $script :Srv2019_6_1_profile ; Script = " Add-PSSnapIn MySnapIn" ; Commands = @ (" Add-PSSnapIn" ); Version = " 6.1" ; OS = " Windows" ; ProblemCount = 1 }
67
68
@ { Target = $script :Srv2019_6_1_profile ; Script = " Get-PSSnapIn MySnapIn" ; Commands = @ (" Get-PSSnapIn" ); Version = " 6.1" ; OS = " Windows" ; ProblemCount = 1 }
68
69
@ { Target = $script :Srv2019_6_1_profile ; Script = " Get-Content $pshome \about_signing.help.txt | Out-Printer" ; Commands = @ (" Out-Printer" ); Version = " 6.1" ; OS = " Windows" ; ProblemCount = 1 }
@@ -78,6 +79,23 @@ $script:CompatibilityTestCases = @(
78
79
@ { Target = $script :Srv2019_6_1_profile ; Script = ' Get-ChildItem ./ | Format-List' ; Commands = @ (); Version = " 3.0" ; OS = " Windows" ; ProblemCount = 0 }
79
80
@ { Target = $script :Srv2019_6_1_profile ; Script = ' gci .' ; Commands = @ (); Version = " 6.1" ; OS = " Windows" ; ProblemCount = 0 }
80
81
@ { Target = $script :Srv2016_6_1_profile ; Script = ' iex $expr | % { Transform $_ }' ; Commands = @ (); Version = " 6.1" ; OS = " Windows" ; ProblemCount = 0 }
82
+ @ { Target = $script :Srv2019_6_1_profile ; Script = ' ConvertFrom-String $str' ; Commands = @ (" ConvertFrom-String" ); Version = " 6.1" ; OS = " Windows" ; ProblemCount = 1 }
83
+ @ { Target = $script :Srv2019_6_1_profile ; Script = " Get-WmiObject -Class Win32_Process" ; Commands = @ (" Get-WmiObject" ); Version = " 6.1" ; OS = " Windows" ; ProblemCount = 1 }
84
+ @ { Target = $script :Srv2019_6_1_profile ; Script = " Invoke-WmiMethod -Path win32_process -Name create -ArgumentList notepad.exe" ; Commands = @ (" Invoke-WmiMethod" ); Version = " 6.1" ; OS = " Windows" ; ProblemCount = 1 }
85
+ @ { Target = $script :Srv2019_6_1_profile ; Script = ' $np | Remove-WmiObject' ; Commands = @ (" Remove-WmiObject" ); Version = " 6.1" ; OS = " Windows" ; ProblemCount = 1 }
86
+ @ { Target = $script :Srv2019_6_1_profile ; Script = ' Set-Clipboard -Value "This is a test string"' ; Commands = @ (" Set-Clipboard" ); Version = " 6.1" ; OS = " Windows" ; ProblemCount = 1 }
87
+ @ { Target = $script :Srv2019_6_1_profile ; Script = " Set-WmiInstance -Class Win32_WMISetting -Argument @{LoggingLevel=2}" ; Commands = @ (" Set-WmiInstance" ); Version = " 6.1" ; OS = " Windows" ; ProblemCount = 1 }
88
+ @ { Target = $script :Srv2019_6_1_profile ; Script = ' Add-Computer -DomainName "Domain01" -Restart' ; Commands = @ (" Add-Computer" ); Version = " 6.1" ; OS = " Windows" ; ProblemCount = 1 }
89
+ @ { Target = $script :Srv2019_6_1_profile ; Script = ' Checkpoint-Computer -Description "Install MyApp"' ; Commands = @ (" Checkpoint-Computer" ); Version = " 6.1" ; OS = " Windows" ; ProblemCount = 1 }
90
+ @ { Target = $script :Srv2019_6_1_profile ; Script = ' Clear-EventLog "Windows PowerShell"' ; Commands = @ (" Clear-EventLog" ); Version = " 6.1" ; OS = " Windows" ; ProblemCount = 1 }
91
+ @ { Target = $script :Srv2019_6_1_profile ; Script = ' Clear-RecycleBin' ; Commands = @ (" Clear-RecycleBin" ); Version = " 6.1" ; OS = " Windows" ; ProblemCount = 1 }
92
+ @ { Target = $script :Srv2019_6_1_profile ; Script = ' Start-Transaction; New-Item MyCompany -UseTransaction; Complete-Transaction' ; Commands = @ (" Start-Transaction" , " Complete-Transaction" ); Version = " 6.1" ; OS = " Windows" ; ProblemCount = 2 }
93
+ @ { Target = $script :Srv2019_6_1_profile ; Script = ' Disable-ComputerRestore -Drive "C:\"' ; Commands = @ (" Disable-ComputerRestore" ); Version = " 6.1" ; OS = " Windows" ; ProblemCount = 1 }
94
+ @ { Target = $script :Srv2019_6_1_profile ; Script = ' Enable-ComputerRestore -Drive "C:\", "D:\"' ; Commands = @ (" Enable-ComputerRestore" ); Version = " 6.1" ; OS = " Windows" ; ProblemCount = 1 }
95
+ @ { Target = $script :Srv2019_6_1_profile ; Script = ' Get-ControlPanelItem -Name "*Program*", "*App*"' ; Commands = @ (" Get-ControlPanelItem" ); Version = " 6.1" ; OS = " Windows" ; ProblemCount = 1 }
96
+ @ { Target = $script :Srv2019_6_1_profile ; Script = ' Get-EventLog -Newest 5 -LogName "Application"' ; Commands = @ (" Get-EventLog" ); Version = " 6.1" ; OS = " Windows" ; ProblemCount = 1 }
97
+ @ { Target = $script :Srv2019_6_1_profile ; Script = ' Get-HotFix -Description "Security*" -ComputerName "Server01", "Server02" -Cred "Server01\admin01"' ; Commands = @ (" Get-HotFix" ); Version = " 6.1" ; OS = " Windows" ; ProblemCount = 1 }
98
+ @ { Target = $script :Srv2019_6_1_profile ; Script = ' $zip = New-WebServiceProxy -Uri "http://www.webservicex.net/uszip.asmx?WSDL"' ; Commands = @ (" New-WebServiceProxy" ); Version = " 6.1" ; OS = " Windows" ; ProblemCount = 1 }
81
99
82
100
@ { Target = $script :Ubuntu1804_6_1_profile ; Script = ' Get-AuthenticodeSignature ./script.ps1' ; Commands = @ (" Get-AuthenticodeSignature" ); Version = " 6.1" ; OS = " Linux" ; ProblemCount = 1 }
83
101
@ { Target = $script :Ubuntu1804_6_1_profile ; Script = ' Get-Service systemd' ; Commands = @ (" Get-Service" ); Version = " 6.1" ; OS = " Linux" ; ProblemCount = 1 }
@@ -120,6 +138,9 @@ $script:ParameterCompatibilityTestCases = @(
120
138
@ { Target = $script :Srv2012_3_profile ; Script = ' Get-Command -ShowCommandInfo' ; Commands = @ (' Get-Command' ); Parameters = @ (' ShowCommandInfo' ); Version = ' 3.0' ; OS = ' Windows' ; ProblemCount = 1 }
121
139
@ { Target = $script :Srv2012_3_profile ; Script = ' Import-Module -FullyQualifiedName @{ ModuleName = "PSScriptAnalyzer"; ModuleVersion = "1.17" }' ; Commands = @ (' Import-Module' ); Parameters = @ (' FullyQualifiedName' ); Version = ' 3.0' ; OS = ' Windows' ; ProblemCount = 1 }
122
140
@ { Target = $script :Srv2012_3_profile ; Script = ' Remove-Module -FullyQualifiedName @{ ModuleName = "PSScriptAnalyzer"; ModuleVersion = "1.17" }' ; Commands = @ (' Remove-Module' ); Parameters = @ (' FullyQualifiedName' ); Version = ' 3.0' ; OS = ' Windows' ; ProblemCount = 1 }
141
+ @ { Target = $script :Srv2012_3_profile ; Script = ' Register-ScheduledJob -RunNow -Trigger $t' ; Commands = @ (' Register-ScheduledJob' ); Parameters = @ (' RunNow' ); Version = ' 3.0' ; OS = ' Windows' ; ProblemCount = 1 }
142
+ @ { Target = $script :Srv2012_3_profile ; Script = ' New-JobTrigger -At "1/20/2012 3:00 AM" -RepeatIndefinitely' ; Commands = @ (' New-JobTrigger' ); Parameters = @ (' RepeatIndefinitely' ); Version = ' 3.0' ; OS = ' Windows' ; ProblemCount = 1 }
143
+ @ { Target = $script :Srv2012_3_profile ; Script = ' $t = Get-ScheduledJob | Get-JobTrigger | Enable-JobTrigger -PassThru' ; Commands = @ (' Enable-JobTrigger' ); Parameters = @ (' PassThru' ); Version = ' 3.0' ; OS = ' Windows' ; ProblemCount = 1 }
123
144
@ { Target = $script :Srv2012_3_profile ; Script = ' Save-Help -FullyQualifiedModule @{ ModuleName = "MyModule"; MaximumVersion = "2.7" }' ; Commands = @ (' Save-Help' ); Parameters = @ (' FullyQualifiedModule' ); Version = ' 3.0' ; OS = ' Windows' ; ProblemCount = 1 }
124
145
@ { Target = $script :Srv2012_3_profile ; Script = ' Export-PSSession -FullyQualifiedModule @{ ModuleName = "MyModule"; RequiredVersion = $reqVer }' ; Commands = @ (' Export-PSSession' ); Parameters = @ (' FullyQualifiedModule' ); Version = ' 3.0' ; OS = ' Windows' ; ProblemCount = 1 }
125
146
@ { Target = $script :Srv2012_3_profile ; Script = ' Get-Command -FullyQualifiedModule @{ ModuleName = $m; MaximumVersion = $maxVer }' ; Commands = @ (' Get-Command' ); Parameters = @ (' FullyQualifiedModule' ); Version = ' 3.0' ; OS = ' Windows' ; ProblemCount = 1 }
@@ -155,23 +176,9 @@ $script:ParameterCompatibilityTestCases = @(
155
176
@ { Target = $script :Srv2019_6_1_profile ; Script = ' Start-Service "eventlog" -ComputerName "MyComputer"' ; Commands = @ (' Start-Service' ); Parameters = @ (' ComputerName' ); Version = ' 6.1' ; OS = ' Windows' ; ProblemCount = 1 }
156
177
)
157
178
158
- # Disabled on AppVeyor's Ubuntu image until fixed
159
- if (-not $IsLinux -and $env: APPVEYOR ) {
160
- $script :CompatibilityTestCases += @ (
161
- @ {
Target = $script :Srv2012_3_profile ;
Script = ' $Protected = "Hello World" | Protect-CmsMessage -To "*[email protected] *"' ;
Commands = @ (
" Protect-CmsMessage" );
Version = " 3.0" ;
OS = " Windows" ;
ProblemCount = 1 }
162
- )
163
-
164
- $script :ParameterCompatibilityTestCases += @ (
165
- @ { Target = $script :Srv2012_3_profile ; Script = ' Register-ScheduledJob -RunNow -Trigger $t' ; Commands = @ (' Register-ScheduledJob' ); Parameters = @ (' RunNow' ); Version = ' 3.0' ; OS = ' Windows' ; ProblemCount = 1 }
166
- @ { Target = $script :Srv2012_3_profile ; Script = ' New-JobTrigger -At "1/20/2012 3:00 AM" -RepeatIndefinitely' ; Commands = @ (' New-JobTrigger' ); Parameters = @ (' RepeatIndefinitely' ); Version = ' 3.0' ; OS = ' Windows' ; ProblemCount = 1 }
167
- @ { Target = $script :Srv2012_3_profile ; Script = ' $t = Get-ScheduledJob | Get-JobTrigger | Enable-JobTrigger -PassThru' ; Commands = @ (' Enable-JobTrigger' ); Parameters = @ (' PassThru' ); Version = ' 3.0' ; OS = ' Windows' ; ProblemCount = 1 }
168
- )
169
- }
170
-
171
-
172
179
Describe ' UseCompatibleCommands' {
173
180
Context ' Targeting a single profile' {
174
- It " Reports <ProblemCount> command incompatibilties with '<Script>' on <OS> with PowerShell <Version>" - TestCases $script :CompatibilityTestCases {
181
+ It " Reports <ProblemCount> command incompatibilties with '<Script>' on <OS> with PowerShell <Version>" - TestCases $script :CompatibilityTestCases - Skip: $ script :RunningInCIOnUbuntu {
175
182
param ($Script , [string ]$Target , [string []]$Commands , [version ]$Version , [string ]$OS , [int ]$ProblemCount )
176
183
177
184
$settings = @ {
@@ -197,7 +204,7 @@ Describe 'UseCompatibleCommands' {
197
204
}
198
205
}
199
206
200
- It " Reports <ProblemCount> parameter incompatibilties for '<Parameters>' on '<Commands>' with '<Script>' on <OS> with PowerShell <Version>" - TestCases $script :ParameterCompatibilityTestCases {
207
+ It " Reports <ProblemCount> parameter incompatibilties for '<Parameters>' on '<Commands>' with '<Script>' on <OS> with PowerShell <Version>" - TestCases $script :ParameterCompatibilityTestCases - Skip: $ script :RunningInCIOnUbuntu {
201
208
param ($Script , [string ]$Target , [string []]$Commands , [string []]$Parameters , [version ]$Version , [string ]$OS , [int ]$ProblemCount )
202
209
203
210
$settings = @ {
@@ -225,7 +232,7 @@ Describe 'UseCompatibleCommands' {
225
232
}
226
233
227
234
Context " Checking a file against many targets" {
228
- It " Finds all command problems" {
235
+ It " Finds all command problems" - Skip: $ script :RunningInCIOnUbuntu {
229
236
$settings = @ {
230
237
Rules = @ {
231
238
$script :RuleName = @ {
@@ -244,7 +251,7 @@ Describe 'UseCompatibleCommands' {
244
251
$diagnostics = Invoke-ScriptAnalyzer - Path " $PSScriptRoot /CompatibilityRuleAssets/IncompatibleScript.ps1" - IncludeRule $script :RuleName - Settings $settings `
245
252
| Where-Object { $_.RuleName -eq $script :RuleName }
246
253
247
- $expectedNumber = if ( $ env: TF_BUILD -and $IsLinux ) { 13 } else { 14 }
254
+ $expectedNumber = 14
248
255
249
256
$diagnostics.Count | Should - Be $expectedNumber - Because " Got diagnostics: $ ( $diagnostics.Message -join " , " ) "
250
257
0 commit comments