Cannot Reboot Windows 2019 Server Using Powershell: Privilege not held

565 Views Asked by At

I am having a hell of a time trying to get a very basic script to reboot my Windows 2019 Server. I am logged in as a Domain and Local Administrator (in the Administrator group).

When I run this basic PowerShell command: Restart-Computer -Force

it gives an error: restart-computer : Failed to restart the computer myComputer with the following error message: Privilege not held. . At line:1 char:1

  • restart-computer -force
  •   + CategoryInfo          : OperationStopped: (myComputer:String) [Restart-Computer], InvalidOperationException
      + FullyQualifiedErrorId : RestartcomputerFailed,Microsoft.PowerShell.Commands.RestartComputerCommand
    
    
    
    
    

The Microsoft article for this error here

says to download the update for your operating system. But it ends at Windows 2016 and says it will be auto-download and be installed with Windows updates. Doing a check for updates, says I am up to date. I tried to find the specific 2016 update KB4034674 just in case it may fix it, but the update is no longer on the Update Catalog.

I tried this stackoverflow article here

I stored the password in a password.bin file, and retrieved it, but it goes back to the above 'Privilege not held' error.


# Capture encrypted password once and store to file
$passwd = Read-Host "Enter password" -AsSecureString
$encpwd = ConvertFrom-SecureString $passwd
$encpwd > C:\Scripts\password.bin

# Afterwards always use this to start the script
$encpwd = Get-Content C:\Scripts\password.bin
$passwd = ConvertTo-SecureString  $encpwd
$cred = new-object System.Management.Automation.PSCredential 'corp\myadmin',$passwd
Start-Process PowerShell -Cred $cred -ArgumentList "-noexit","-file C:\Scripts\Reboot.ps1"

If I run powershell ISE as 'Run as Administrator' it works. But, I need it to run on it's own

I also tried Windows Batch files and commands, like c:\windows\system32\shutdown.exe /r) and they also don't work unless 'Run as Admin'

To add to my problem, Task Scheduler doesn't work on this server. It got hosed somehow. I am using Task Till Dawn and tried using its built-in Restart and tried to run powershell script with it but it won't work either.

Anybody know how to automatically reboot a Windows 2019 Server? This is kind of ridiculous

1

There are 1 best solutions below

2
Keith Langmead On

You need to run that command as admin, there's no way around that. Whether you're using Powershell, the ISE or cmd.exe, simply starting the app will start it in a normal user context (regardless of whether you're logged into the machine as admin). To do any commands that require admin permissions you need to elevate whatever you're using, go through UAC, and only then will Powershell be operating as admin.

Normally I'd suggest the simplest option (if you're trying to automate the process) would be to use Task Scheduler, since you can then explicitely tell it to run as admin, but as you say that that's not an option.

I assume Task Till Dawn doesn't have an option to tell the task to be elevated either? Tried having a look, but there doesn't seem to be a lot of documentation listing all available options.

What exactly are you trying to achieve? Is this intended to be a regular thing, or just an occasional restart happening at a certain time (eg you want it to restart later, but don't want to have to be there to do it in person)? If the latter than you use shutdown /r /t nnn to restart after a specific number of seconds (which can be many hours later).