mkellerman / Invoke-CommandAs

Invoke Command As System/Interactive/GMSA/User on Local/Remote machine & returns PSObjects.
https://www.powershellgallery.com/packages/Invoke-CommandAs/
MIT License
457 stars 70 forks source link

Error: InvalidOperation RuntimeException UsingWithoutInvokeCommand #29

Closed WayneSherman closed 5 years ago

WayneSherman commented 5 years ago

Using file \Scripts\Invoke-CommandAs.ps1 downloaded from github. Testing on Windows 10 IoT Enterprise LTSC 2019 (ver 1809)

test.ps1 has 2 lines:

. $PSScriptRoot\Invoke-CommandAs.ps1
Invoke-CommandAs -ScriptBlock { Get-Process } -AsSystem

When running I get several error messages, the first one is:

A Using variable cannot be retrieved. A Using variable can be used only with Invoke-Command, Start-Job, or InlineScript in the script workflow. When it is used with Invoke-Command, the Using variable is valid only if the script block is invoked on a remote computer. At C:\util\Invoke-CommandAs.ps1:555 char:21

  • ... $Using:Function | ForEach-Object { Invoke-Expression $ ...
  • 
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : UsingWithoutInvokeCommand
mkellerman commented 5 years ago

Seems the issue is when you're not using the Invoke-CommandAs on a remote machine (ie: you're running it locally).

WayneSherman commented 5 years ago

Correct. For my use case I need a simple way to execute PowerShell commands and scripts as "NT AUTHORITY\SYSTEM" on the local computer.

mkellerman commented 5 years ago

You can use the Invoke-ScheduledTask function in the private folder to run it locally. That’s what I’ll be doing in the next version. If no ComputerName/Session provided, run it directly as a ScheduleTask.

WayneSherman commented 5 years ago

Thanks, Invoke-ScheduledTask is working.

mkellerman commented 5 years ago

3.1.4 has been publish to the gallery. This should work now with Invoke-CommandAs locally.