Access Denied with -RunElevated
wrighbr opened this issue · comments
Trying to run the below code in a non elevated powershell and get the following error message
$scriptblock = {get-process}
Invoke-CommandAs -ScriptBlock $scriptblock -RunElevated
Invoke-ScheduledTask : An access denied error occurred when registering scheduled job definition e44e0537-40c1-4a28-81ae-e56b4275aa69. Try running Windows PowerShell with elevated user right
s; that is, Run As Administrator.
At C:\Program Files\WindowsPowerShell\Modules\Invoke-CommandAs\3.1.5\public\Invoke-CommandAs.ps1:399 char:17
+ Invoke-ScheduledTask @Parameters
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Invoke-ScheduledTask
$PSVersionTable
Name Value
---- -----
PSVersion 5.1.17763.503
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.17763.503
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
Get-Module
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Script 3.1.5 Invoke-CommandAs Invoke-CommandAs
Script 1.0.0.0 ISE {Get-IseSnippet, Import-IseSnippet, New-IseSnippet}
Manifest 3.1.0.0 Microsoft.PowerShell.Management {Add-Computer, Add-Content, Checkpoint-Computer, Clear-Content...}
Manifest 3.0.0.0 Microsoft.PowerShell.Security {ConvertFrom-SecureString, ConvertTo-SecureString, Get-Acl, Get-AuthenticodeSignature...}
Manifest 3.1.0.0 Microsoft.PowerShell.Utility {Add-Member, Add-Type, Clear-Variable, Compare-Object...}
Manifest 3.0.0.0 Microsoft.WSMan.Management {Connect-WSMan, Disable-WSManCredSSP, Disconnect-WSMan, Enable-WSManCredSSP...}
Binary 1.1.0.0 PSScheduledJob {Add-JobTrigger, Disable-JobTrigger, Disable-ScheduledJob, Enable-JobTrigger...}
Manifest 1.0.0.0 ScheduledTasks {Disable-ScheduledTask, Enable-ScheduledTask, Export-ScheduledTask, Get-ClusteredScheduledTask...}
Yeah, this is a ‘normal’ issue. To create a schedule task locally, you must run in an elevated powershell console to begin with.
Not sure how to circumvent this.
And this is not particularly related to -RunElevated.
You could self elevate your script:
http://www.expta.com/2017/03/how-to-self-elevate-powershell-script.html
Register-ScheduledTask requires a Elevated powershell session (Run As Administrator)