MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3try {4 #skip all tests on non-windows platform5 $defaultParamValues = $PSDefaultParameterValues.Clone()6 $PSDefaultParameterValues["it:skip"] = !$IsWindows7 8Describe "Basic Registry Provider Tests" -Tags @("CI", "RequireAdminOnWindows") {9 BeforeAll {10 if ($IsWindows) {11 $restoreLocation = Get-Location12 $registryBase = "HKLM:\software\Microsoft\PowerShell\3\"13 $parentKey = "TestKeyThatWillNotConflict"14 $testKey = "TestKey"15 $testKey2 = "TestKey2"16 $testPropertyName = "TestEntry"17 $testPropertyValue = 118 $defaultPropertyName = "(Default)"19 $defaultPropertyValue = "something"20 $otherPropertyValue = "other"21 }22 }23 24 AfterAll {25 if ($IsWindows) {26 #restore the previous environment27 Set-Location -Path $restoreLocation28 }29 }30 31 BeforeEach {32 if ($IsWindows) {33 #create a parent key that can be easily removed and will not conflict34 Set-Location $registryBase35 New-Item -Path $parentKey > $null36 #create the test keys/test properties for the tests to manipulate37 Set-Location $parentKey38 New-Item -Path $testKey > $null39 New-Item -Path $testKey2 > $null40 New-ItemProperty -Path $testKey -Name $testPropertyName -Value $testPropertyValue > $null41 }42 }43 44 AfterEach {45 if ($IsWindows) {46 Set-Location $registryBase47 Remove-Item -Path $parentKey -Recurse -Force -ErrorAction SilentlyContinue48 }49 }50 51 Context "Validate basic registry provider Cmdlets" {52 It "Verify Test-Path" {53 Test-Path -IsValid Registry::HKCU/Software | Should -BeTrue54 Test-Path -IsValid Registry::foo/Softare | Should -BeFalse55 }56 57 It "Verify Get-Item" {58 $item = Get-Item $testKey59 $item.PSChildName | Should -BeExactly $testKey60 }61 62 It "Verify Get-Item on inaccessible path" {63 { Get-Item HKLM:\SAM\SAM -ErrorAction Stop } | Should -Throw -ErrorId "System.Security.SecurityException,Microsoft.PowerShell.Commands.GetItemCommand"64 }65 66 It "Verify Get-ChildItem" {67 $items = Get-ChildItem68 $items.Count | Should -BeExactly 269 $Items.PSChildName -contains $testKey | Should -BeTrue70 $Items.PSChildName -contains $testKey2 | Should -BeTrue71 }72 73 It "Verify Get-ChildItem can get subkey names" {74 $items = Get-ChildItem -Name75 $items.Count | Should -BeExactly 276 $items -contains $testKey | Should -BeTrue77 $items -contains $testKey2 | Should -BeTrue78 }79 80 It "Verify New-Item" {81 $newKey = New-Item -Path "NewItemTest"82 Test-Path "NewItemTest" | Should -BeTrue83 Split-Path $newKey.Name -Leaf | Should -BeExactly "NewItemTest"84 }85 86 It "Verify Copy-Item" {87 $copyKey = Copy-Item -Path $testKey -Destination "CopiedKey" -PassThru88 Test-Path "CopiedKey" | Should -BeTrue89 Split-Path $copyKey.Name -Leaf | Should -BeExactly "CopiedKey"90 }91 92 It "Verify Move-Item" {93 $movedKey = Move-Item -Path $testKey -Destination "MovedKey" -PassThru94 Test-Path "MovedKey" | Should -BeTrue95 Split-Path $movedKey.Name -Leaf | Should -BeExactly "MovedKey"96 }97 98 It "Verify Rename-Item" {99 $existBefore = Test-Path $testKey100 $renamedKey = Rename-Item -Path $testKey -NewName "RenamedKey" -PassThru101 $existAfter = Test-Path $testKey102 $existBefore | Should -BeTrue103 $existAfter | Should -BeFalse104 Test-Path "RenamedKey" | Should -BeTrue105 Split-Path $renamedKey.Name -Leaf | Should -BeExactly "RenamedKey"106 }107 }108 109 Context "Valdiate basic registry property Cmdlets" {110 It "Verify New-ItemProperty" {111 New-ItemProperty -Path $testKey -Name "NewTestEntry" -Value 99 > $null112 $property = Get-ItemProperty -Path $testKey -Name "NewTestEntry"113 $property.NewTestEntry | Should -Be 99114 $property.PSChildName | Should -BeExactly $testKey115 }116 117 It "Verify Set-ItemProperty" {118 Set-ItemProperty -Path $testKey -Name $testPropertyName -Value 2119 $property = Get-ItemProperty -Path $testKey -Name $testPropertyName120 $property."$testPropertyName" | Should -Be 2121 }122 123 It "Verify Set-Item" {124 Set-Item -Path $testKey -Value $defaultPropertyValue125 $property = Get-ItemProperty -Path $testKey -Name $defaultPropertyName126 $property."$defaultPropertyName" | Should -BeExactly $defaultPropertyValue127 }128 129 It "Verify Set-Item with -WhatIf" {130 Set-Item -Path $testKey -Value $defaultPropertyValue131 Set-Item -Path $testKey -Value $otherPropertyValue -WhatIf132 $property = Get-ItemProperty -Path $testKey -Name $defaultPropertyName133 $property."$defaultPropertyName" | Should -BeExactly $defaultPropertyValue134 }135 136 It "Verify Get-ItemPropertyValue" {137 $propertyValue = Get-ItemPropertyValue -Path $testKey -Name $testPropertyName138 $propertyValue | Should -Be $testPropertyValue139 }140 141 It "Verify Copy-ItemProperty" {142 Copy-ItemProperty -Path $testKey -Name $testPropertyName -Destination $testKey2143 $property1 = Get-ItemProperty -Path $testKey -Name $testPropertyName -ErrorAction SilentlyContinue144 $property2 = Get-ItemProperty -Path $testKey2 -Name $testPropertyName -ErrorAction SilentlyContinue145 $property2."$testPropertyName" | Should -BeExactly $property1."$testPropertyName"146 $property1.PSChildName | Should -BeExactly $testKey147 $property2.PSChildName | Should -BeExactly $testKey2148 }149 150 It "Verify Move-ItemProperty" {151 Move-ItemProperty -Path $testKey -Name $testPropertyName -Destination $testKey2152 $property1 = Get-ItemProperty -Path $testKey -Name $testPropertyName -ErrorAction SilentlyContinue153 $property2 = Get-ItemProperty -Path $testKey2 -Name $testPropertyName -ErrorAction SilentlyContinue154 $property1 | Should -BeNullOrEmpty155 $property2."$testPropertyName" | Should -BeExactly $testPropertyValue156 $property2.PSChildName | Should -BeExactly $testKey2157 }158 159 It "Verify Rename-ItemProperty" {160 Rename-ItemProperty -Path $testKey -Name $testPropertyName -NewName "RenamedProperty"161 $property1 = Get-ItemProperty -Path $testKey -Name $testPropertyName -ErrorAction SilentlyContinue162 $property2 = Get-ItemProperty -Path $testKey -Name "RenamedProperty" -ErrorAction SilentlyContinue163 $property1 | Should -BeNullOrEmpty164 $property2.RenamedProperty | Should -BeExactly $testPropertyValue165 $property2.PSChildName | Should -BeExactly $testKey166 }167 168 It "Verify Clear-ItemProperty" {169 Clear-ItemProperty -Path $testKey -Name $testPropertyName170 $property = Get-ItemProperty -Path $testKey -Name $testPropertyName171 $property."$testPropertyName" | Should -Be 0172 }173 174 It "Verify Clear-Item" {175 Set-ItemProperty -Path $testKey -Name $testPropertyName -Value $testPropertyValue176 Set-Item -Path $testKey -Value $defaultPropertyValue177 Clear-Item -Path $testKey178 $key = Get-Item -Path $testKey179 $key.Property.Length | Should -BeExactly 0180 }181 182 It "Verify Clear-Item with -WhatIf" {183 Set-ItemProperty -Path $testKey -Name $testPropertyName -Value $testPropertyValue184 Set-Item -Path $testKey -Value $defaultPropertyValue185 Clear-Item -Path $testKey -WhatIf186 $key = Get-Item -Path $testKey187 $key.Property.Length | Should -BeExactly 2188 }189 190 It "Verify Remove-ItemProperty" {191 Remove-ItemProperty -Path $testKey -Name $testPropertyName192 $properties = @(Get-ItemProperty -Path $testKey)193 $properties.Count | Should -Be 0194 }195 }196}197 198Describe "Extended Registry Provider Tests" -Tags @("Feature", "RequireAdminOnWindows") {199 BeforeAll {200 if ($IsWindows) {201 $restoreLocation = Get-Location202 $registryBase = "HKLM:\software\Microsoft\PowerShell\3\"203 $parentKey = "TestKeyThatWillNotConflict"204 $testKey = "TestKey"205 $testKey2 = "TestKey2"206 $testPropertyName = "TestEntry"207 $testPropertyValue = 1208 }209 }210 211 AfterAll {212 if ($IsWindows) {213 #restore the previous environment214 Set-Location -Path $restoreLocation215 }216 }217 218 BeforeEach {219 if ($IsWindows) {220 #create a parent key that can be easily removed and will not conflict221 Set-Location $registryBase222 New-Item -Path $parentKey > $null223 #create the test keys/test properties for the tests to manipulate224 Set-Location $parentKey225 New-Item -Path $testKey > $null226 New-Item -Path $testKey2 > $null227 New-ItemProperty -Path $testKey -Name $testPropertyName -Value $testPropertyValue > $null228 New-ItemProperty -Path $testKey2 -Name $testPropertyName -Value $testPropertyValue > $null229 }230 }231 232 AfterEach {233 if ($IsWindows) {234 Set-Location $registryBase235 Remove-Item -Path $parentKey -Recurse -Force -ErrorAction SilentlyContinue236 }237 }238 239 Context "Valdiate New-ItemProperty Parameters" {240 BeforeEach {241 #remove the current properties so that we can remake them with different parameters242 Remove-ItemProperty -Path $testKey -Name $testPropertyName -Force -ErrorAction SilentlyContinue243 Remove-ItemProperty -Path $testKey2 -Name $testPropertyName -Force -ErrorAction SilentlyContinue244 }245 246 It "Verify Filter" {247 { $result = New-ItemProperty -Path ".\*" -Filter "Test*" -Name $testPropertyName -Value $testPropertyValue -ErrorAction Stop } | Should -Throw -ErrorId "NotSupported,Microsoft.PowerShell.Commands.NewItemPropertyCommand"248 }249 250 It "Verify Include" {251 $result = New-ItemProperty -Path ".\*" -Include "*2" -Name $testPropertyName -Value $testPropertyValue252 $result."$testPropertyName" | Should -Be $testPropertyValue253 $result.PSChildName | Should -BeExactly $testKey2254 }255 256 It "Verify Exclude" {257 $result = New-ItemProperty -Path ".\*" -Exclude "*2" -Name $testPropertyName -Value $testPropertyValue258 $result."$testPropertyName" | Should -Be $testPropertyValue259 $result.PSChildName | Should -BeExactly $testKey260 }261 262 It "Verify Confirm can be bypassed" {263 $result = New-ItemProperty -Path $testKey -Name $testPropertyName -Value $testPropertyValue -Force -Confirm:$false264 $result."$testPropertyName" | Should -Be $testPropertyValue265 $result.PSChildName | Should -BeExactly $testKey266 }267 268 It "Verify WhatIf" {269 $result = New-ItemProperty -Path $testKey -Name $testPropertyName -Value $testPropertyValue -WhatIf270 $result | Should -BeNullOrEmpty271 }272 }273 274 Context "Valdiate Get-ItemProperty Parameters" {275 It "Verify Name" {276 $result = Get-ItemProperty -Path $testKey -Name $testPropertyName277 $result."$testPropertyName" | Should -Be $testPropertyValue278 $result.PSChildName | Should -BeExactly $testKey279 }280 281 It "Verify Path but no Name" {282 $result = Get-ItemProperty -Path $testKey283 $result."$testPropertyName" | Should -Be $testPropertyValue284 $result.PSChildName | Should -BeExactly $testKey285 }286 287 It "Verify Filter" {288 { $result = Get-ItemProperty -Path ".\*" -Filter "*Test*" -ErrorAction Stop } | Should -Throw -ErrorId "NotSupported,Microsoft.PowerShell.Commands.GetItemPropertyCommand"289 }290 291 It "Verify Include" {292 $result = Get-ItemProperty -Path ".\*" -Include "*2"293 $result."$testPropertyName" | Should -Be $testPropertyValue294 $result.PSChildName | Should -BeExactly $testKey2295 }296 297 It "Verify Exclude" {298 $result = Get-ItemProperty -Path ".\*" -Exclude "*2"299 $result."$testPropertyName" | Should -Be $testPropertyValue300 $result.PSChildName | Should -BeExactly $testKey301 }302 }303 304 Context "Valdiate Get-ItemPropertyValue Parameters" {305 It "Verify Name" {306 $result = Get-ItemPropertyValue -Path $testKey -Name $testPropertyName307 $result | Should -Be $testPropertyValue308 }309 }310 311 Context "Valdiate Set-ItemPropertyValue Parameters" {312 BeforeAll {313 $newPropertyValue = 2314 }315 316 It "Verify Name" {317 Set-ItemProperty -Path $testKey -Name $testPropertyName -Value $newPropertyValue318 $result = Get-ItemProperty -Path $testKey -Name $testPropertyName319 $result."$testPropertyName" | Should -Be $newPropertyValue320 }321 322 It "Verify PassThru" {323 $result = Set-ItemProperty -Path $testKey -Name $testPropertyName -Value $newPropertyValue -PassThru324 $result."$testPropertyName" | Should -Be $newPropertyValue325 }326 327 It "Verify Piped Default Parameter" {328 $prop = Get-ItemProperty -Path $testKey -Name $testPropertyName329 $prop | Set-ItemProperty -Name $testPropertyName -Value $newPropertyValue330 $result = Get-ItemProperty -Path $testKey -Name $testPropertyName331 $result."$testPropertyName" | Should -Be $newPropertyValue332 }333 334 It "Verify WhatIf" {335 $result = Set-ItemProperty -Path $testKey -Name $testPropertyName -Value $newPropertyValue -PassThru -WhatIf336 $result = Get-ItemProperty -Path $testKey -Name $testPropertyName337 $result."$testPropertyName" | Should -Be $testPropertyValue338 }339 340 It "Verify Confirm can be bypassed" {341 $result = Set-ItemProperty -Path $testKey -Name $testPropertyName -Value $newPropertyValue -PassThru -Confirm:$false342 $result = Get-ItemProperty -Path $testKey -Name $testPropertyName343 $result."$testPropertyName" | Should -Be $newPropertyValue344 }345 }346 347 Context "Valdiate Copy-ItemProperty Parameters" {348 BeforeEach {349 #remove the current property so that we have a place to copy to350 Remove-ItemProperty -Path $testKey2 -Name $testPropertyName -Force -ErrorAction SilentlyContinue351 }352 353 It "Verify PassThru" {354 #passthru returns the property on testKey not testKey2355 $property1 = Copy-ItemProperty -Path $testKey -Name $testPropertyName -Destination $testKey2 -PassThru356 $property2 = Get-ItemProperty -Path $testKey2 -Name $testPropertyName -ErrorAction SilentlyContinue357 $property2."$testPropertyName" | Should -Be $property1."$testPropertyName"358 $property1.PSChildName | Should -BeExactly $testKey359 $property2.PSChildName | Should -BeExactly $testKey2360 }361 362 It "Verify Confirm can be bypassed" {363 Copy-ItemProperty -Path $testKey -Name $testPropertyName -Destination $testKey2 -Confirm:$false364 $property1 = Get-ItemProperty -Path $testKey -Name $testPropertyName -ErrorAction SilentlyContinue365 $property2 = Get-ItemProperty -Path $testKey2 -Name $testPropertyName -ErrorAction SilentlyContinue366 $property2."$testPropertyName" | Should -Be $property1."$testPropertyName"367 $property1.PSChildName | Should -BeExactly $testKey368 $property2.PSChildName | Should -BeExactly $testKey2369 }370 371 It "Verify WhatIf" {372 Copy-ItemProperty -Path $testKey -Name $testPropertyName -Destination $testKey2 -WhatIf373 { Get-ItemProperty -Path $testKey2 -Name $testPropertyName -ErrorAction Stop } | Should -Throw -ErrorId "System.Management.Automation.PSArgumentException,Microsoft.PowerShell.Commands.GetItemPropertyCommand"374 }375 }376 377 Context "Valdiate Move-ItemProperty Parameters" {378 BeforeEach {379 #remove the current property so that we have a place to move to380 Remove-ItemProperty -Path $testKey2 -Name $testPropertyName -Force -ErrorAction SilentlyContinue381 }382 383 It "Verify PassThru" {384 $property2 = Move-ItemProperty -Path $testKey -Name $testPropertyName -Destination $testKey2 -PassThru385 $property1 = Get-ItemProperty -Path $testKey -Name $testPropertyName -ErrorAction SilentlyContinue386 $property1 | Should -BeNullOrEmpty387 $property2."$testPropertyName" | Should -Be $testPropertyValue388 $property2.PSChildName | Should -BeExactly $testKey2389 }390 391 It "Verify Confirm can be bypassed" {392 Move-ItemProperty -Path $testKey -Name $testPropertyName -Destination $testKey2 -Confirm:$false393 $property1 = Get-ItemProperty -Path $testKey -Name $testPropertyName -ErrorAction SilentlyContinue394 $property2 = Get-ItemProperty -Path $testKey2 -Name $testPropertyName -ErrorAction SilentlyContinue395 $property1 | Should -BeNullOrEmpty396 $property2."$testPropertyName" | Should -Be $testPropertyValue397 $property2.PSChildName | Should -BeExactly $testKey2398 }399 400 It "Verify WhatIf" {401 Move-ItemProperty -Path $testKey -Name $testPropertyName -Destination $testKey2 -WhatIf402 $property1 = Get-ItemProperty -Path $testKey -Name $testPropertyName -ErrorAction SilentlyContinue403 $property2 = Get-ItemProperty -Path $testKey2 -Name $testPropertyName -ErrorAction SilentlyContinue404 $property1."$testPropertyName" | Should -Be $testPropertyValue405 $property1.PSChildName | Should -BeExactly $testKey406 $property2 | Should -BeNullOrEmpty407 }408 }409 410 Context "Valdiate Rename-ItemProperty Parameters" {411 BeforeAll {412 $newPropertyName = "NewEntry"413 }414 415 It "Verify Confirm can be bypassed" {416 Rename-ItemProperty -Path $testKey -Name $testPropertyName -NewName $newPropertyName -Confirm:$false417 $property1 = Get-ItemProperty -Path $testKey -Name $testPropertyName -ErrorAction SilentlyContinue418 $property2 = Get-ItemProperty -Path $testKey -Name $newPropertyName -ErrorAction SilentlyContinue419 $property1 | Should -BeNullOrEmpty420 $property2."$newPropertyName" | Should -Be $testPropertyValue421 }422 423 It "Verify WhatIf" {424 Rename-ItemProperty -Path $testKey -Name $testPropertyName -NewName $newPropertyName -WhatIf425 $property1 = Get-ItemProperty -Path $testKey -Name $testPropertyName -ErrorAction SilentlyContinue426 $property2 = Get-ItemProperty -Path $testKey -Name $newPropertyName -ErrorAction SilentlyContinue427 $property1."$testPropertyName" | Should -Be $testPropertyValue428 $property2 | Should -BeNullOrEmpty429 }430 }431 432 Context "Valdiate Clear-ItemProperty Parameters" {433 It "Verify Confirm can be bypassed" {434 Clear-ItemProperty -Path $testKey -Name $testPropertyName -Confirm:$false435 $result = Get-ItemProperty -Path $testKey -Name $testPropertyName436 $result."$testPropertyName" | Should -Be 0437 }438 439 It "Verify WhatIf" {440 Clear-ItemProperty -Path $testKey -Name $testPropertyName -WhatIf441 $result = Get-ItemProperty -Path $testKey -Name $testPropertyName442 $result."$testPropertyName" | Should -Be $testPropertyValue443 }444 }445 446 Context "Valdiate Remove-ItemProperty Parameters" {447 It "Verify Confirm can be bypassed" {448 Remove-ItemProperty -Path $testKey -Name $testPropertyName -Confirm:$false449 { Get-ItemProperty -Path $testKey -Name $testPropertyName -ErrorAction Stop } | Should -Throw -ErrorId "System.Management.Automation.PSArgumentException,Microsoft.PowerShell.Commands.GetItemPropertyCommand"450 }451 452 It "Verify WhatIf" {453 Remove-ItemProperty -Path $testKey -Name $testPropertyName -WhatIf454 $result = Get-ItemProperty -Path $testKey -Name $testPropertyName455 $result."$testPropertyName" | Should -Be $testPropertyValue456 }457 }458 459 Context "Validate -LiteralPath" {460 It "Verify New-Item and Remove-Item work with asterisk" {461 try {462 $tempPath = "HKCU:\_tmp"463 $testPath = "$tempPath\*\sub"464 $null = New-Item -Force $testPath465 $testPath | Should -Exist466 Remove-Item -LiteralPath $testPath467 $testPath | Should -Not -Exist468 }469 finally {470 Remove-Item -Recurse $tempPath -ErrorAction SilentlyContinue471 }472 }473 }474 475 Context "Validate Get-ItemProperty Cast Exception" {476 BeforeAll {477 if ($IsWindows) {478 $registrySubkeyPath = 'HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\badreg'479 480 # Below will import .reg file with 64 bit integer in 32 bit DWORD481 $badRegistryContent = @"482Windows Registry Editor Version 5.00483 484[$registrySubkeyPath]485"NoModify"=hex(4):01,00,00,00,00,00,00,00486"@487 488 $badRegistryPath = Join-Path -Path $TestDrive -ChildPath badreg.reg489 $badRegistryContent | Set-Content -Path $badRegistryPath490 reg.exe import $badRegistryPath491 492 $registryProviderSubkeyPath = 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\badreg'493 }494 }495 496 It "Validate non-terminating error for cast" {497 Get-ItemProperty -Path $registryProviderSubkeyPath -ErrorVariable err -ErrorAction SilentlyContinue498 $err | Should -HaveCount 1499 $err[0].Exception | Should -BeOfType [System.InvalidCastException]500 $err[0].TargetObject | Should -BeExactly $registrySubkeyPath501 $err[0].CategoryInfo.Category | Should -BeExactly 'ReadError'502 $err[0].FullyQualifiedErrorId | Should -BeExactly 'System.InvalidCastException,Microsoft.PowerShell.Commands.GetItemPropertyCommand'503 }504 505 It "Validate terminating error for cast" {506 { Get-ItemProperty -Path $registryProviderSubkeyPath -ErrorAction Stop } | Should -Throw -ErrorId 'System.InvalidCastException,Microsoft.PowerShell.Commands.GetItemPropertyCommand'507 }508 509 AfterAll {510 if ($IsWindows) {511 reg.exe delete $registrySubkeyPath /f512 }513 }514 }515}516 517} finally {518 $global:PSdefaultParameterValues = $defaultParamValues519}520 