I am working on combining 2 scripts to check if a computer is on pending reboot and restart it if there is no user logged in.
Below is the script. When I run it locally it works, but when I push it through Kbox it restarts the computer even if I am logged in.
Any help will be appreciated.
#Based on <http://gallery.technet.microsoft.com/scriptcenter/Get-PendingReboot-Query-bdb79542>
function Test-RebootRequired
{
$result = @{
CBSRebootPending =$false
WindowsUpdateRebootRequired = $false
FileRenamePending = $false
SCCMRebootPending = $false
}
#Check CBS Registry
$key = Get-ChildItem "HKLM:Software\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending" -ErrorAction SilentlyContinue
if ($key -ne $null)
{
$result.CBSRebootPending = $true
}
#Check Windows Update
$key = Get-Item "HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired" -ErrorAction SilentlyContinue
if($key -ne $null)
{
$result.WindowsUpdateRebootRequired = $true
}
#Check PendingFileRenameOperations
$prop = Get-ItemProperty "HKLM:SYSTEM\CurrentControlSet\Control\Session Manager" -Name PendingFileRenameOperations -ErrorAction SilentlyContinue
if($prop -ne $null)
{
$result.FileRenamePending = $true }
#Check SCCM Client <http://gallery.technet.microsoft.com/scriptcenter/Get-PendingReboot-Query-bdb79542/view/Discussions#content>
try
{
$util = [wmiclass]"\\.\root\ccm\clientsdk:CCM_ClientUtilities"
$status = $util.DetermineIfRebootPending()
if(($status -ne $null) -and $status.RebootPending){
$result.SCCMRebootPending = $true
}
}catch{}
#Return Reboot required
return $result.ContainsValue($true)
}
#Now Check if there is no user logged in to reboot.
if(Test-RebootRequired -eq "True")
{
$QuiryLoggedUser = quser
If (!$QuiryLoggedUser)
{
Write-Output 'No user logged in.'
shutdown -r -t 0 -d U:00:00
}
else{
Write-Output 'Computer in use.'
}
}
Kbox is Dell-Quest endpoint management and inventory appliance. We use it for endpoints inventory, patching, pushing script (run as system), and many.
KACE is the name of the appliance and Kbox is the name of the application.
I have tried it and it’s giving me relatively the same error message.
#Now Check if there is no user logged in to reboot.
if(Test-RebootRequired -eq "True")
{
$QuiryLoggedUser = c:\Windows\System32\quser.exe
If (!$QuiryLoggedUser)
{
Write-Output 'No user logged in.'
#shutdown -r -t 0 -d U:00:00
}
else{
Write-Output 'Computer in use.'
}
}
else{ return "False"}
<#
c:\Windows\System32\quser.exe : The term 'c:\Windows\System32\quser.exe' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the
path is correct and try again.
At C:\ProgramData\Quest\KACE\kbots_cache\packages\kbots\173\RestartComputerOnPendingRebootStatus.ps1:36 char:20
+ $QuiryLoggedUser = c:\Windows\System32\quser.exe
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (c:\Windows\System32\quser.exe:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
No user logged in.
#>
Running as SYSTEM
& : The term 'C:\Windows\System32\quser.exe' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At C:\ProgramData\Quest\KACE\kbots_cache\packages\kbots\173\RestartComputerOnPendingRebootStatus.ps1:36 char:22
+ $QuiryLoggedUser = & "C:\Windows\System32\quser.exe"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Windows\System32\quser.exe:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
No user logged in.
I have tried that but it does not get the RDP user. The result is blank when the user is RDPing in.
PS C:\temp\Upgrade> (Get-CimInstance -ClassName Win32_ComputerSystem).Username
PS C:\temp\Upgrade> C:\Windows\System32\quser.exe
USERNAME SESSIONNAME ID STATE IDLE TIME LOGON TIME
>lkossi rdp-tcp#3 2 Active . 4/15/2022 7:51 AM
PS C:\temp\Upgrade>
Running with credntials provided: domain [rochmn] user [admpctest]
Get-LoggedInUser : The term 'quser' is not recognized as the name of a cmdlet, function, script
file, or operable program. Check the spelling of the name, or if a path was included, verify that
the path is correct and try again.
At C:\ProgramData\Quest\KACE\kbots_cache\packages\kbots\173\GetUserLoggedIn.ps1:69 char:1
+ Get-LoggedInUser | Out-File C:\temp\UserloggedIn.txt
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Get-LoggedInUser
Launched Process: powershell.exe
But When I run it locally, it gets me the user logged in.
Again … if you have issues using the quser.exe you may try another approach. It does not make that much sense to waste your time troubleshooting a non working solution when you could easily get an equally valid result with another solution.
I don’t get why this should make any difference!? There are always ways to circumvent annoying obstacles.
If we’re talking about domain joined computers where you have administrative access to with PowerShell remoting you can query the registry for the key “Volatile Environment”. It will return the logged on users no matter if they are loggod on remotely or locally.