Scheduled Task PowerShell or CMD script - "Run As Administrator" - powershell

I've created a PowerShell script which lists the scheduled tasks and the LastRunResult. I'm using Get-ScheduledTask command. I need to put it at the end of every scheduled task to send a notification related to the LastRunResult.
The problem is that Get-ScheduledTask command doesn't work properly and do not show ALL scheduled tasks but just some of them. To be able to see all scheduled tasks the Get-ScheduledTask command needs to be run from the CMD or PowerShell console with administrative privileges. And it works from there. But it doesn't work when I run such script from Task Scheduler. Checking Run with highest privileges doesn't work. Specifying Policy -Bypass doesn't work as well.
Does anyone know how to execute Get-ScheduledTask from Task Scheduler ran in administrator mode to list all scheduled tasks?

This can be caused if you have UAC enabled or are logged on as a user who is not an administrator.
Ensure the runas account is a local administrator.

Related

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
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?

Task Scheduler - Powershell elevated task

I have a script which works when it is manually executed the script works under these conditions:
1) I start start.ps1 which then elevates with admin user then executes main.ps1 script
2) the start.ps1 is being run without any rights and I am elevating it by using
Start-Process -FilePath Powershell -LoadUserProfile -Credential $credential -ArgumentList '-File', $script
Everything works fine, please note that new powershell window appear and main.ps1 is being executed there.
now, when I create a scheduled task, then run it - I can see in the task manager that main.ps1 is being executed but noting is happening apart from task being stuck in scheduler. I cant use admin user in task scheduler it has to be executed as non privileged user.
Do you have any ideas how to fix this issue?
I have tried running it under local user, with/or without execution policy set to bypass/unrestricted etc nothing helped.
Thanks in advance

How can I run this Powershell script as a user?

I am trying to run the following script upon startup of my machine because I cannot get a service to start on startup. I can schedule it to run. The problem I am facing seems to be a user vs administrator issue.
This is for a Windows 7 machine with PowerShell 4.0.
I have set the execution policy to unrestricted.
When I try to run the script as the User I designated to have administrative privileges I see the following error.
When I right click the powershell icon, run as administrator the command works fine.
The command is
get-service -name "*vmauthd*" |start-service
Is it possible to run this as my user account?
Solution I was able to get this script to run on startup as I initially desired. I turned off UAC and set the execution policy to unrestricted. I am not sure if the UAC was the issue before but the script runs now. I created a cmd file with this in the code. I set the cmd file to run at startup using Windows Task Scheduler and set the task to run whether logged in or not.
PowerShell -Command "Set-ExecutionPolicy Unrestricted"
PowerShell -Command "get-service -name "vmauthd" | start-service"
pause
Here is an image of the cmd file

Are 'Scheduled Job" and "Scheduled Task" the same thing in Powershell context?

I am trying to create a scheduled task from Powershell.
I was readin the document for cmdlet: New-ScheduledJobOption.
And I am a bit confused...
Question:
Are 'Scheduled Job" and "Scheduled Task" the same thing in Powershell context? Or there is a difference?
So just to put an actual answer out there, in addition to the article Valamus put out:
In a PowerShell context Scheduled Jobs and Scheduled Tasks are not the same thing. From a task scheduler perspective they are exactly the same thing.
The PowerShell team provided a toolset for creating a special type of job that would be stored in the Task scheduler library. The Scheduled Jobs cmdlets will not allow you to interact with regular scheduled tasks. The Task Scheduler will let you interact with regular tasks and scheduled jobs interchangeably.
Scheduled Task is just a Windows thing and PowerShell can control it in the same way it (PowerShell) can control many other Windows things.
Scheduled Job is, in the other hand, mostly PowerShell thing. Namely, scheduled job is some PowerShell code scheduled for execution in a "Windows Task Scheduler" manner and executed within PowerShell environment (naturally).
So, an activity run as a Scheduled Task is anything you can run from command prompt (eg. .EXE, .BAT, .COM, .CMD etc. - nothing PowerShell-ish, unless you decide to start powershell.exe and give it some script name to execute. Yet, you can control all of that from PowerShell.
On the other hand, Scheduled Job is exclusively PowerShell code (be it script block or script file) that PowerShell registers with Windows Task Scheduler, and Windows Task Scheduler knows how to activate it. Method of repetitive activation is in both cases the same ("triggers").
So, Powershell Scheduled Task is a Windows thing (controllable also from PowerShell), while PowerShell Scheduled Job is a exclusively PowerShell thing.

How to administer scheduledjobs in powershell 3?

According to documentation, get-scheduledjob only returns scheduled jobs where owner is the current user. Other scheduled job commandlets like set-scheduledjob also only work for scheduled jobs where owner is current user. This makes it impossible for non owners to get job status, modify the job (such as setting other credentials), etc.
In a proper IT organization, I'm going to say its crucial for these jobs to allow adminstration from various administrators.
Am I missing some way to administer, review results, etc (other than looking directly at the powershell output files in the owner's appdata)?
To clarify - I'm looking for a method to work with Powershell created and administered ScheduledJobs. If you modify the scheduled task that executes the scheduled job through the UI, schtask or other scheduled task specific tool, you'll get unexpected results. If you change owner/credentials, the scheduled task will fail. You can use UI/schtasks to change schedule without causing any problems. In addition to changing owner, I want to get at the results of get-job in order to monitor the jobs progress.
The only way I have ever been able to get this to work using Powershell was by invoking the schtask.exe utility:
Note: "/U" is for local administration and "/RU" is for remote administration, also "/S" is not needed with working locally.
Create by importing an XML previously exported from Task Scheduler
[string]$string = 'schtasks.exe /create /RU yourdomain\username /RP $password /TN Task-Name /XML "D:\Path\To\ExportedXML.xml" /S ServerName'
Delete:
[string]$string = 'schtasks.exe /delete /RU yourdomain\username /P $password /TN Task-Name /S ServerName /F'
Query:
[string]$string = 'schtasks.exe /query /RU yourdomain\username /P $password'
Run Locally:
Invoke-Expression -Command $string
Run Remotely:
Invoke-Command -ScriptBlock {$string}
I've written a few PowerShell scripts for managing scheduled tasks (they use the TaskService COM object):
Rename Scheduled Tasks in Windows 7, Windows Server 2008, and Windows Vista
How-To: Use PowerShell to Report on Scheduled Tasks
Updating a Scheduled Task's Credentials
Bill