function Invoke-CommandAs {

<#

.SYNOPSIS

Invoke Command as System/User on Local/Remote computer using ScheduleTask.

.DESCRIPTION

Invoke Command as System/User on Local/Remote computer using ScheduleTask.

ScheduledJob will be executed with current user credentials if no -As <credential> or -AsSystem is provided.

Using ScheduledJob as they are ran in the background and the output can be retreived by any other process.

Using ScheduledTask to Run the ScheduledJob, since you can allow Tasks to run as System or provide any credentials.

Because the ScheduledJob is executed by the Task Scheduler, it is invoked locally as a seperate process and not from within the current Powershell Session.

Resolving the Double Hop limitations by Powershell Remote Sessions.

By Marc R Kellerman (@mkellerman)

.PARAMETER ComputerName

Specifies the computers on which the command runs. The default is the local computer.

When you use the ComputerName parameter, Windows PowerShell creates a temporary connection that is used only to run the specified command and is then closed. If you need a persistent connection, use the Session parameter.

Type the NETBIOS name, IP address, or fully qualified domain name of one or more computers in a comma-separated list. To specify the local computer, type the computer name, localhost, or a dot (.).

To use an IP address in the value of ComputerName , the command must include the Credential parameter. Also, the computer must be configured for HTTPS transport or the IP address of the remote computer must be included in the WinRM TrustedHosts

list on the local computer. For instructions for adding a computer name to the TrustedHosts list, see "How to Add a Computer to the Trusted Host List" in about_Remote_Troubleshooting.

On Windows Vista and later versions of the Windows operating system, to include the local computer in the value of ComputerName , you must open Windows PowerShell by using the Run as administrator option.