67 lines
2.2 KiB
PowerShell
67 lines
2.2 KiB
PowerShell
|
function Invoke-Shutdown {
|
||
|
<#
|
||
|
.SYNOPSIS
|
||
|
Wrap the default Windows shutdown command with more helper information
|
||
|
You should just use Stop-Computer or Restart-Computer
|
||
|
|
||
|
.PARAMETER Now
|
||
|
Shutdown the current computer now
|
||
|
|
||
|
.PARAMETER Reboot
|
||
|
Reboot on shutdown
|
||
|
|
||
|
.PARAMETER ComputerName
|
||
|
One or more computers to shut down.
|
||
|
|
||
|
.PARAMETER Minutes
|
||
|
How long to wait before shutting down. Defaults to 1 minute.
|
||
|
|
||
|
.PARAMETER Reason
|
||
|
Provide a reason why we are shutting this computer down
|
||
|
#>
|
||
|
[CmdletBinding()]
|
||
|
[OutputType([void])]
|
||
|
param (
|
||
|
[Parameter(ParameterSetName = 'Now')]
|
||
|
[switch]$Now,
|
||
|
[Parameter(ParameterSetName = 'Computer', Mandatory = $true)]
|
||
|
[ValidateNotNullOrEmpty()]
|
||
|
[string[]]$ComputerName,
|
||
|
[Parameter(ParameterSetName = 'Computer', Mandatory = $false)]
|
||
|
[int]$Minutes = 1,
|
||
|
[Parameter()]
|
||
|
[switch]$Reboot,
|
||
|
[Parameter()]
|
||
|
[string]$Reason
|
||
|
)
|
||
|
|
||
|
$arguments = @("/s") # shutdown command
|
||
|
if ($Reboot) {
|
||
|
$arguments = @("/r") # reboot, not shutdown
|
||
|
}
|
||
|
if (-not [string]::IsNullOrWhiteSpace($Reason)) {
|
||
|
$arguments += "/c `"$Reason`""
|
||
|
}
|
||
|
$arguments += "/f" # force it to kill apps
|
||
|
|
||
|
if ($PSCmdlet.ParameterSetName -eq 'Now') {
|
||
|
Write-Host "Shutting system down now"
|
||
|
$arguments += "/t"
|
||
|
$arguments += "0" # now
|
||
|
Invoke-CallOperatorWithPathAndParameters -Path "C:\Windows\system32\shutdown.exe" -Arguments $arguments
|
||
|
} else {
|
||
|
# The timeout value is expressed in seconds, not minutes
|
||
|
$arguments += "/t"
|
||
|
$arguments += "$($Minutes * 60)"
|
||
|
foreach ($computer in $ComputerName) {
|
||
|
if (-not [string]::IsNullOrWhiteSpace($computer)) {
|
||
|
Write-Host "Shutting down remote server $computer"
|
||
|
$computerArguments = @($arguments)
|
||
|
$computerArguments += "/m"
|
||
|
$computerArguments += "\\$computer"
|
||
|
Write-Host "C:\Windows\system32\shutdown.exe $($computerArguments -join ' ')"
|
||
|
Invoke-CallOperatorWithPathAndParameters -Path "C:\Windows\system32\shutdown.exe" -Arguments $computerArguments
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|