1

I have a Powershell script that exports a list of all the services on my local computer using the following command:

Get-CimInstance win32_service | Export-Csv $PSScriptRoot\Local\services.csv

It works fine when I run it under a local admin account from a powershell prompt. I tried running it as a scheduled task, using the same local admin account to run the scheduled task, and nothing gets output to the .csv file.

I know the scheduled task itself is running OK, because there are other output files that are being updated as I would expect. What's different about running this script as a scheduled task that is breaking it?

UPDATE: I ran the script both ways with ProcMon running in the background. The one difference I noticed is that the scheduled task's process had a Session ID of 0, where as running the script directly had a Session ID of 4.

1

I think the problem is that I had marked the task to:

(o) Run whether user is logged on or not

According to Microsoft,

You can specify that a task should run even if the account under which the task is scheduled to run is not logged on when the task is triggered. To do this, select the radio button labeled Run whether user is logged on or not . If this radio button is selected, tasks will not run interactively. To make a task run interactively, select the Run only when user is logged on radio button.

The key part of that is this sentence:

To make a task run interactively, select the Run only when user is logged on radio button.

I'm guessing that powershell needs to run interactively in order for the Get-CimInstance cmdlet to run properly. I changed my task to Run only when user is logged on and everything seems to be working now.

1

I ran into this same issue with Win32_Process. I was trying to monitor usage of a certain .exe, but this line of code returns nothing when ran as a Scheduled Task with "Run whether user is logged on or not":

$Procs = Get-CimInstance Win32_Process -Computer $ComputerName -Filter "Name = '$MonProcName'"

My organization doesn't allow staying logged on all the time, so I went back to an earlier draft I had started with using Get-WmiObject, and it works logged off!:

$Procs = Get-WmiObject -Computer $ComputerName -Class Win32_Process -Filter "Name = '$MonProcName'"

The only reason I switched to Get-CimInstance was it's better .CreationDate datetime format. It was worth the hassle to reformat Get-WmiObject .CreationDate so that I can run this logged off.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.