Gwmi not working when running as PSScheduled job

Answered Gwmi not working when running as PSScheduled job

  • Sunday, October 07, 2012 3:30 PM
     
      Has Code

    I have the following simple script to reboot computers in batches of 5, and then it runs through the list, getting the last bootup time, puts this all into an array of custom objects, and exports to csv. (PS V3).

    Because I've had trouble with the Task Scheduler not running my scripts properly lately, I configure this one as a V3 Powershell scheduled job.  It runs without fail, and it does its job, except:

    When I run it interactively, the csv file gets populated correctly.  When run as a scheduled job, the lastbootuptime is blank.  I have tried setting this task to run with elevated priveliges, but this made no difference. 

    Here is the script:

    $results = @()
    $pcs = Get-Content 'c:\scripts\pcs.txt' -ReadCount 5
    foreach ($pc in $pcs) {Restart-Computer $pc -Wait -Force}
    foreach ($pc in $pcs) {
        foreach ($p in $pc) {
            $os = gwmi -Class win32_operatingsystem -ComputerName $p 
            $lastboot = $os.convertToDateTime($os.lastbootuptime)
            $results += New-Object psObject -Property @{'Computer'=$p; 'LastBoot' = $lastboot}
            }
        }
    $results | sort lastboot | select computer, lastboot | Export-Csv 'c:\scripts\lastboot.csv' -NoTypeInformation
    


    Grant Ward, a.k.a. Bigteddy

All Replies

  • Sunday, October 07, 2012 4:27 PM
     
     Answered
    Update:  I implemented it as a regular Scheduled Task, and, when run on demand, works fine.  I will have to wait until tomorrow to see if it runs with no-one logged on.

    Grant Ward, a.k.a. Bigteddy


  • Monday, October 08, 2012 5:35 AM
     
     

    After implementing this as a normal Scheduled Task, it runs unattended, and produces the full output.  It is only when run as a PSScheduled-Job that I have this problem.

    As not many people will have used this before (it's new in V3), I don't expect an answer, but I'll leave this thread open in case anyone has any bright ideas.


    Grant Ward, a.k.a. Bigteddy

  • Thursday, December 20, 2012 11:31 PM
    Moderator
     
     

    Hi Grant,

    Since no answer appears forthcoming, I'm going to go ahead and close this thread.

    Bill