locked
Powershell script cannot display the value of parameter in Task Scheduler RRS feed

  • Question

  • Powershell script cannot display the value of parameter in Task Scheduler

    While putting the .ps1 file to Task Scheduler, the value of parameter cannot display.

    My script is for getting info of Disk usage, for example:

    # Get the C and D drive usage

    $DiskInfo1 = Get-WmiObject win32_logicaldisk -filter "DeviceID='C:'"    
     $DiskSize1 = $DiskInfo1.size
     $DiskFree1 = $DiskInfo1.FreeSpace
     $DiskFreeUsed1 = ($DiskFree1/$DiskSize1) * 100
     $DiskFreeUsedPer1 = "{0:N2}" -f $DiskFreeUsed1

     $DiskInfo2 = Get-WmiObject win32_logicaldisk -filter "DeviceID='D:'"
     $DiskSize2 = $DiskInfo2.size
     $DiskFree2 = $DiskInfo2.FreeSpace
     $DiskFreeUsed2 = ($DiskFree2/$DiskSize2) * 100
     $DiskFreeUsedPer2 = "{0:N2}" -f $DiskFreeUsed2

    # Export Result to file

    $objHostInfo = New-Object System.object
     $objHostInfo | Add-Member -MemberType NoteProperty -Name C-FreeDiskPercent -value $DiskFreeUsedPer1
     $objHostInfo | Add-Member -MemberType NoteProperty -Name D-FreeDiskPercent -value $DiskFreeUsedPer2

    $objHostInfo >> c:\temp\info.txt

    The normal result I run the script in ISE as below:

    The abnormal result I run the script in Task Scheduler as below:

    Anyone can help me? What's the root cause of it? Thanks a lot...


    Michael Chiang

    Wednesday, July 6, 2016 8:18 AM

All replies

  • Get-WmiObject win32_logicaldisk | Where-Object {$_.Deviceid -match 'C:|D:'} |
                        Select-Object @{Label='Drive';Ex = {$($_.DeviceID)}},
                                               @{Label='FreeDiskPercent';Ex = {"{0:N2}" -f (($_.Freespace / $_.Size)*100) }} |
                                               Out-File C:\Test\Disk.txt




    Wednesday, July 6, 2016 9:25 AM
  • Still cannot works after put the script in Task Scheduler. The script is ok, but why it is abnormal in task scheduler to run it?


    Michael Chiang

    Wednesday, July 6, 2016 10:17 AM
  • Working for me

    Wednesday, July 6, 2016 10:24 AM
  • It is odd...   I don't know if I need to retart the server, but all of my four windows server 2012 using powershell v1.0 cannot run it normally. The windows server 2008 R2 is fine.

    Michael Chiang

    Thursday, July 7, 2016 2:42 AM
  • 2012 Server has by default Powershell v3 or v4.

    Type Get-Host

    Tested it on Server 2012 it works.

    Thursday, July 7, 2016 7:12 AM
  • Using this powershell file, the task scheduler action set up as below:

    c:\windows\syswow64\windowspowershell\v1.0\powershell.exe

    -ExecutionPolicy Bypass -Command "c:\scripts\disk.ps1"


    Michael Chiang

    Friday, July 8, 2016 1:16 AM