<#
.SYNOPSIS
Script to logoff all users logged on to specified computers
.DESCRIPTION
The script can use either a plaintext file or a computer name as input and will logoff any user sessions that are logged on Active or Disconnected
.PARAMETER InputFile
A path that contains a plaintext file with computer names
.PARAMETER Computer
This parameter can be used instead of the InputFile parameter to specify a single computer or a series of computers using a comma-separated format
.\Remote-Logoff.ps1 -Computer Server01
Description:
Will logoff all user sessions on Server01
.EXAMPLE
.\Remote-Logoff.ps1 -InputFile C:\ListofComputers.txt
Description:
Will logoff all user sessions on all servers and computernames listed in the ListofComputers file
#>
param(
[Parameter(ParameterSetName='InputFile')]
[string]
$InputFile,
[Parameter(ParameterSetName='Computer')]
[string]
$Computer
)
<#
.SYNOPSIS
Function that resolves computer names and can exit script if resolution fails
#>
if (!$InputFile) {
if (!$Computer) {
$Computer = Read-Host "Please input computer name"
}
[string[]]$Computer = $Computer.Split(',')
$Computer | ForEach-Object {
$_
Write-Host "Logging of all logged in users on `'$_`'"
try {
query user /server:$Computer 2>&1 | select -skip 1 | foreach {
logoff ($_ -split "\s+")[-6] /server:$Computer
}
Write-Host -ForegroundColor Green "Successfully completed command on `'$_`'"
} catch {
Write-Warning "$_"
}
}
}
else {
if (!(Test-Path -Path $InputFile)) {
Write-Warning "Input file not found, please enter correct path"
exit
}
Get-Content -Path $InputFile | ForEach-Object {
Write-Host "Logging of all logged in users on `'$_`'"
try {
query user /server:$Computer 2>&1 | select -skip 1 | foreach {
logoff ($_ -split "\s+")[-6] /server:$Computer
}
Write-Host -ForegroundColor Green "Successfully completed command"
} catch {
Write-Warning "$_"
}
}
}