2020-03-24 11:08:37 -07:00
|
|
|
# Copyright (c) Microsoft Corporation.
|
2018-02-13 09:23:53 -08:00
|
|
|
# Licensed under the MIT License.
|
|
|
|
|
|
2017-11-01 10:59:41 -07:00
|
|
|
# the testhook for restart-computer is the same as for stop-computer
|
|
|
|
|
$restartTesthookName = "TestStopComputer"
|
|
|
|
|
$restartTesthookResultName = "TestStopComputerResults"
|
|
|
|
|
$DefaultResultValue = 0
|
|
|
|
|
|
2018-02-13 09:23:53 -08:00
|
|
|
try
|
2017-11-01 10:59:41 -07:00
|
|
|
{
|
|
|
|
|
# set up for testing
|
|
|
|
|
Enable-Testhook -testhookName $restartTesthookName
|
|
|
|
|
|
2018-11-09 15:11:02 -08:00
|
|
|
Describe "Restart-Computer" -Tag Feature,RequireAdminOnWindows {
|
2017-11-01 10:59:41 -07:00
|
|
|
# if we throw in BeforeEach, the test will fail and the restart will not be called
|
|
|
|
|
BeforeEach {
|
|
|
|
|
if ( ! (Test-TesthookIsSet -testhookName $restartTesthookName) ) {
|
|
|
|
|
throw "Testhook '${restartTesthookName}' is not set"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AfterEach {
|
|
|
|
|
Set-TesthookResult -testhookName $restartTesthookResultName -value $defaultResultValue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It "Should restart the local computer" {
|
|
|
|
|
Set-TesthookResult -testhookName $restartTesthookResultName -value $defaultResultValue
|
2020-01-11 15:41:59 +00:00
|
|
|
Restart-Computer -ErrorAction Stop | Should -BeNullOrEmpty
|
2017-11-01 10:59:41 -07:00
|
|
|
}
|
|
|
|
|
|
2020-02-03 14:57:17 -08:00
|
|
|
It "Should support -computer parameter" -Skip:(!$IsWindows) {
|
2017-11-01 10:59:41 -07:00
|
|
|
Set-TesthookResult -testhookName $restartTesthookResultName -value $defaultResultValue
|
|
|
|
|
$computerNames = "localhost","${env:COMPUTERNAME}"
|
2020-01-11 15:41:59 +00:00
|
|
|
Restart-Computer -Computer $computerNames -ErrorAction Stop | Should -BeNullOrEmpty
|
2017-11-01 10:59:41 -07:00
|
|
|
}
|
|
|
|
|
|
2020-02-03 14:57:17 -08:00
|
|
|
It "Should support WsmanAuthentication types" -Skip:(!$IsWindows) {
|
2017-11-01 10:59:41 -07:00
|
|
|
$authChoices = "Default","Basic","Negotiate","CredSSP","Digest","Kerberos"
|
|
|
|
|
foreach ( $auth in $authChoices ) {
|
2018-03-09 15:13:15 -08:00
|
|
|
Restart-Computer -WsmanAuthentication $auth | Should -BeNullOrEmpty
|
2017-11-01 10:59:41 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-13 09:23:53 -08:00
|
|
|
# this requires setting a test hook, so we wrap the execution with try/finally of the
|
|
|
|
|
# set operation. Internally, we want to suppress the progress, so
|
2017-11-01 10:59:41 -07:00
|
|
|
# that is also wrapped in try/finally
|
2020-02-03 14:57:17 -08:00
|
|
|
It "Should wait for a remote system" -Skip:(!$IsWindows) {
|
2017-11-01 10:59:41 -07:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Enable-Testhook -testhookname TestWaitStopComputer
|
|
|
|
|
$timeout = 3
|
2018-02-13 09:23:53 -08:00
|
|
|
try
|
2017-11-01 10:59:41 -07:00
|
|
|
{
|
|
|
|
|
$pPref = $ProgressPreference
|
|
|
|
|
$ProgressPreference="SilentlyContinue"
|
2018-02-13 09:23:53 -08:00
|
|
|
$duration = Measure-Command {
|
2018-03-09 15:13:15 -08:00
|
|
|
Restart-Computer -computer localhost -Wait -Timeout $timeout -ErrorAction Stop | Should -BeNullOrEmpty
|
2017-11-01 10:59:41 -07:00
|
|
|
}
|
|
|
|
|
}
|
2018-02-13 09:23:53 -08:00
|
|
|
finally
|
2017-11-01 10:59:41 -07:00
|
|
|
{
|
|
|
|
|
$ProgressPreference=$pPref
|
|
|
|
|
}
|
2018-03-09 15:13:15 -08:00
|
|
|
$duration.TotalSeconds | Should -BeGreaterThan $timeout
|
2017-11-01 10:59:41 -07:00
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Disable-Testhook -testhookname TestWaitStopComputer
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Context "Restart-Computer Error Conditions" {
|
|
|
|
|
It "Should return the proper error when it occurs" {
|
|
|
|
|
Set-TesthookResult -testhookName $restartTesthookResultName -value 0x300000
|
2019-01-03 07:59:14 +00:00
|
|
|
Restart-Computer -ErrorVariable RestartError 2> $null
|
2018-03-09 15:13:15 -08:00
|
|
|
$RestartError.Exception.Message | Should -Match 0x300000
|
2017-11-01 10:59:41 -07:00
|
|
|
}
|
|
|
|
|
|
2020-02-03 14:57:17 -08:00
|
|
|
It "Should produce an error when 'Delay' is specified" -Skip:(!$IsWindows) {
|
2018-05-17 14:42:04 -07:00
|
|
|
{ Restart-Computer -Delay 30 } | Should -Throw -ErrorId "RestartComputerInvalidParameter,Microsoft.PowerShell.Commands.RestartComputerCommand"
|
2017-11-01 10:59:41 -07:00
|
|
|
}
|
|
|
|
|
|
2020-02-03 14:57:17 -08:00
|
|
|
It "Should not support timeout on Unix" -Skip:($IsWindows) {
|
2020-05-07 13:00:30 +01:00
|
|
|
{ Restart-Computer -Timeout 3 -ErrorAction Stop } | Should -Throw -ErrorId "NamedParameterNotFound,Microsoft.PowerShell.Commands.RestartComputerCommand"
|
2020-02-03 14:57:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It "Should not support Delay on Unix" -Skip:($IsWindows) {
|
|
|
|
|
{ Restart-Computer -Delay 30 } | Should -Throw -ErrorId "NamedParameterNotFound,Microsoft.PowerShell.Commands.RestartComputerCommand"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It "Should not support timeout on localhost" -Skip:(!$IsWindows) {
|
2017-11-01 10:59:41 -07:00
|
|
|
Set-TesthookResult -testhookName $restartTesthookResultName -value $defaultResultValue
|
2020-05-07 13:00:30 +01:00
|
|
|
{ Restart-Computer -Timeout 3 -ErrorAction Stop } | Should -Throw -ErrorId "RestartComputerInvalidParameter,Microsoft.PowerShell.Commands.RestartComputerCommand"
|
2017-11-01 10:59:41 -07:00
|
|
|
}
|
|
|
|
|
|
2020-02-03 14:57:17 -08:00
|
|
|
It "Should not support timeout on localhost" -Skip:(!$IsWindows) {
|
2017-11-01 10:59:41 -07:00
|
|
|
Set-TesthookResult -testhookName $restartTesthookResultName -value $defaultResultValue
|
2020-05-07 13:00:30 +01:00
|
|
|
{ Restart-Computer -Timeout 3 -ErrorAction Stop } | Should -Throw -ErrorId "RestartComputerInvalidParameter,Microsoft.PowerShell.Commands.RestartComputerCommand"
|
2017-11-01 10:59:41 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
$PSDefaultParameterValues.Remove("it:skip")
|
|
|
|
|
Disable-Testhook -testhookName $restartTesthookName
|
|
|
|
|
Set-TesthookResult -testhookName $restartTesthookResultName -value 0
|
|
|
|
|
}
|