'Powershell Script to create Scheduled Task on non domain computers

We are trying to execute a powershell script that will import a scheduled task to non domain and domain joined computers in our organization. The task is simply to disable wifi adapter when an ethernet cable is connected. I exported the task as a .xml and then can get the task to run as admin and complete successfully. However when we attempt to run logged in as normal user, the UAC box pops up asking for admin username and password. I created the task as both domain admin and local admin accounts and tried both, with same result when running the .bat as normal user. This is my script so far (I have no previous experience in writing powershell):

Copy-Item -Path D:\WiFi -Destination C:\PCM\Utils -Recurse;
schtasks /create /tn "Ethernet On-Disable Wifi" /xml "C:\PCM\Utils\WiFi\Ethernet On-Disable Wifi.xml" /ru Domain\admin /rp domainpw;
schtasks /create /tn "Ethernet Off-Enable Wifi" /xml "C:\PCM\Utils\WiFi\Ethernet Off-Enable Wifi.xml" /ru Domain\admin /rp domainpw;
*

I'd like to be able to run this as %computername%\localadmin as that account is on all laptops... The first line copies a folder with the .xml from a thumb drive to a folder on laptop. Then it creates the tasks. If I am logged into the laptop as admin, the batch runs fine, but as a local user, it fails with the following: Error Image

Basically, it copies the folder fine, then ERROR: Access is denied. I'm pretty sure its because the user logged in does not have rights to create the task. Is there a way to have the task run as localadmin and complete?

We run this .bat file to fire off the .ps1 from the folder that gets copied to laptop.

PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& 'D:\WifiTask\WifiTask.ps1'"
pause

Sorry if the description of this issue is vague or confusing, just trying to learn as I go....thanks



Solution 1:[1]

No, a normal user will not be able to create a system scheduled task without getting prompted for elevation by UAC.

For domain computers, you could try:

  • Group policy to create the scheduled task
  • Group policy to run your script as the local system user
  • Script that connects to computers as Admin user and pushes your xml and wifitask.ps1 like Invoke-Command

Off-domain PCs are more difficult, you would want to do it the same way you currently install other software. Manually run as-admin? Software deployment agent? Remote management tools?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cpt.Whale