Function Reset-Computer { <# .Synopsis Remotely restart or shutdown a computer. .Description The function will Restart, Shutdown, PowerDown, or Logoff a remote computer. .Parameter Computername What is the name of the computer to remotely control. This parameter has an alias of CN. .Parameter Force If specifed, force all applications to close on the remote machine. .Parameter Operation What do you want to do with the remote computer. Valid choices are Restart, Shutdown, PowerDown, or Logoff. The default is Restart. This parameter has an alias of OP. .Example PS C:\> reset-computer server01 -force This will restart computer server01, forcing any open applications to close. .Example PS C:\> get-content fleservers.txt | reset-computer -op Logoff In this example, all the computers listed in fleservers.txt will be piped to Restart-Computer. Each computer will be logged off. .Example PS C:\> "lab01","lab02","lab03" | reset-computer -op shutdown -force -verbose Shutdown computers lab01, lab02 and lab03 forcing applications close and displaying verbose information about what the function is doing. .ReturnValue [BOOLEAN] .Link Get-WMIObject .Notes NAME: Reset-Computer VERSION: 1.1 AUTHOR: Don Jones/Jeffery Hicks LASTEDIT: 4/22/2009 5:00:00 PM #requires -version 2.0 #> [CmdletBinding( SupportsShouldProcess=$True, ConfrmImpact="high" )] param ( [Parameter( ValueFromPipeline=$True, Position=0, Mandatory=$True, HelpMessage="Computer name(s) to target")] [Alias("CN")] [String[]]$computerName, [Parameter( Mandatory=$false, Position=1, HelpMessage="Restart,Shutdown,PowerDown, or Logoff")] [Alias("OP")] [ValidateSet("Restart","Shutdown","PowerDown","Logoff")] [String]$operation="Restart", [Parameter( HelpMessage="Forces applications to close")] [Switch]$force ) BEGIN { Write-Debug "In the BEGIN script block." Write-Debug "passing value of operation $Operation to switch" switch ($operation) { "Logoff" { $opcode = 0 } "Restart" { $opcode = 2 } "Shutdown" { $opcode = 1 } "PowerDown" { $opcode = 8 } } if ($force) { Write-Verbose "Using Force option" Write-Debug "Adding 4 to opcode ($opcode)" $opcode += 4 } Write-Debug "Win32Shutdown opcode is $opcode" } PROCESS { trap { Write-Debug "trap called" Set-Variable -Name connected -Scope 1 -Value $false Write-Verbose "Connection to $computername failed" continue } Write-Verbose "Attempting connection to $computername" $connected = $True $wmi = Get-WmiObject win32_operatingsystem -computername $computerName -enableAllPrivileges -ea "Stop" Write-Debug "Creating custom PSObject" $obj = New-Object PSObject $obj | Add-Member NoteProperty ComputerName $($computerName) $obj | Add-Member NoteProperty Success $false if ($connected) { Write-Debug "Connected is True" foreach ($os in $wmi) { Write-Verbose "Attempting operation $opcode on $computername" if ($pscmdlet.shouldprocess($computername)) { Write-Debug "Calling the Win32Shutdown() method" $return = $os.win32shutdown($opcode) Write-Debug "return is $($return.returnvalue)" if ($return.returnvalue -eq 0) { $obj.Success = $true } else { Write-Debug "Action failed. Return is $return.returnvalue" } } } } Write-Debug "writing custom object to the pipeline" Write-Output $obj } END { Write-Debug "In the End script block" } } #end function