MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3$RenameTesthook = "TestRenameComputer"4$RenameResultName = "TestRenameComputerResults"5$DefaultResultValue = 06 7try8{9 # set up for testing10 $originalDefaultParameterValues = $PSDefaultParameterValues.Clone()11 $PSDefaultParameterValues["it:skip"] = ! $IsWindows12 Enable-Testhook -testhookName $RenameTesthook13 # we also set TestStopComputer14 Enable-Testhook -testhookName TestStopComputer15 16 # TEST START HERE17 Describe "Rename-Computer" -Tag Feature,RequireAdminOnWindows {18 # if we throw in BeforeEach, the test will fail and the stop will not be called19 BeforeEach {20 if ( ! (Test-TesthookIsSet -testhookName $RenameTesthook) ) {21 throw "Testhook '${TesthookName}' is not set"22 }23 }24 25 AfterEach {26 Set-TesthookResult -testhookName $RenameResultName -value $defaultResultValue27 }28 29 It "Should rename the local computer" {30 Set-TesthookResult -testhookName $RenameResultName -value $defaultResultValue31 $newname = "mynewname"32 $result = Rename-Computer -ErrorAction Stop -ComputerName . -NewName "$newname" -Pass -WarningAction SilentlyContinue33 $result.HasSucceeded | Should -BeTrue34 $result.NewComputerName | Should -BeExactly $newname35 }36 37 # we can't really look for the string "reboot" as it will change38 # when translated. We are guaranteed that the old computer name will39 # be present, so we'll look for that40 It "Should produce a reboot warning when renaming computer" {41 Set-TesthookResult -testhookName $RenameResultName -value $defaultResultValue42 $newname = "mynewname"43 $result = Rename-Computer -ErrorAction Stop -ComputerName . -NewName "$newname" -Pass -WarningAction SilentlyContinue -WarningVariable WarnVar44 $WarnVar.Message | Should -Match $result.OldComputerName45 }46 47 It "Should not produce a reboot warning when renaming a computer with the reboot flag" {48 Set-TesthookResult -testhookName $RenameResultName -value $defaultResultValue49 $newname = "mynewname"50 $result = Rename-Computer -ErrorAction Stop -ComputerName . -NewName "$newname" -Pass -WarningAction SilentlyContinue -WarningVariable WarnVar -Restart51 $result.HasSucceeded | Should -BeTrue52 $result.NewComputerName | Should -BeExactly $newname53 $WarnVar | Should -BeNullOrEmpty54 }55 56 Context "Rename-Computer Error Conditions" {57 $testcases =58 @{ OldName = "." ; NewName = "localhost" ; ExpectedError = "FailToRenameComputer,Microsoft.PowerShell.Commands.RenameComputerCommand" },59 @{ OldName = "." ; NewName = "." ; ExpectedError = "InvalidNewName,Microsoft.PowerShell.Commands.RenameComputerCommand" },60 @{ OldName = "." ; NewName = "::1" ; ExpectedError = "InvalidNewName,Microsoft.PowerShell.Commands.RenameComputerCommand" },61 @{ OldName = "." ; NewName = "127.0.0.1" ; ExpectedError = "InvalidNewName,Microsoft.PowerShell.Commands.RenameComputerCommand" },62 @{ OldName = "." ; NewName = ${env:ComputerName} ; ExpectedError = "NewNameIsOldName,Microsoft.PowerShell.Commands.RenameComputerCommand" },63 @{ OldName = "." ; NewName = ${env:ComputerName} + "." + ${env:USERDNSDOMAIN} ; ExpectedError = "InvalidNewName,Microsoft.PowerShell.Commands.RenameComputerCommand" },64 @{ OldName = ".\$#" ; NewName = "NewName"; ExpectedError = "AddressResolutionException,Microsoft.PowerShell.Commands.RenameComputerCommand" }65 66 It "Renaming '<OldName>' to '<NewName>' creates the right error" -testcase $testcases {67 param ( $OldName, $NewName, $ExpectedError )68 Set-TesthookResult -testhookName $RenameResultName -value 0x169 { Rename-Computer -ComputerName $OldName -NewName $NewName -ErrorAction Stop } | Should -Throw -ErrorId $ExpectedError70 }71 }72 }73 74}75finally76{77 $global:PSDefaultParameterValues = $originalDefaultParameterValues78 Disable-Testhook -testhookName $RenameTestHook79 Disable-Testhook -testhookName TestStopComputer80 Set-TesthookResult -testhookName $RenameResultName -value 081}82 