0

OS windows 8, I am scheduling a script to import free diskspaces to import in a csv file, using windows task schedular.The script runs manually but no output it gives when scheduled is task schedular . The code used is as follows

            Set-ExecutionPolicy Unrestricted -Force

            $filepath = "C:\Users\asdf\Downloads\Powershell\CheckFreeSpaceV3"
            $servername = "abc"

            #delete reports older than 7 days
            $OldReports = (Get-Date).AddDays(-7)

            #Removing Report older than 7 Days
            Get-ChildItem $filepath\DiskReport*.* | `
            Where-Object { $_.LastWriteTime -le $OldReports} | `
            Remove-Item -Recurse -Force -ErrorAction SilentlyContinue  

            #Create variable for log date
            $LogDate = get-date -f yyyyMMddhhmm

            $DiskReport = Get-WmiObject win32_logicaldisk `
            -ComputerName $servername -Filter "Drivetype=3" `
            -ErrorAction SilentlyContinue | 
            #return only disks with free space less than or equal to 0.1 (40%)
            Where-Object {   ($_.freespace/$_.size) -le '0.4'}

            #Create Reports
            $DiskReport | 
            Select-Object @{Label = "Server Name";Expression = {$_.SystemName}},
            @{Label = "Drive";Expression = {$_.DeviceID}},
            @{Label = "Total Space(GB)";Expression = {"{0:N1}" -f( $_.Size / 1gb)}},
            @{Label = "Free Space (GB)";Expression = {"{0:N1}" -f( $_.Freespace / 1gb ) }},
            @{Label = 'Free Space (%)'; Expression = {"{0:P0}" -f ($_.freespace/$_.size)}} |


I put the following arguments in taskschdular arguments
-NoProfile -NoLogo -NonInteractive -ExecutionPolicy Bypass -File powershell_Script_path
Ahmad Raza
  • 39
  • 10

2 Answers2

0

Your script has not creating any report file as an Output and also an extra pipeline was throwing exception.

I have created an HTML file with your script and it is working fine now.

The HTML file will get generated at D:\ drive with the name HTML_Report.html. You can simple view it with Google Chrome.

So, each time the script will create new file and if the file is already there, then it will overwrite the existing.

Now,Place the script in your desired path and you can create a task in task scheduler with the mentioned script path which will trigger and will create the output file in the above mentioned location.

Note: If you do not have the drive , please point it to a different location.

        Set-ExecutionPolicy Unrestricted -Force

        $filepath = "C:\Users\farooq.GENIE\Downloads\Powershell\CheckFreeSpaceV3"
        $servername = "GT030"

        #delete reports older than 7 days
        $OldReports = (Get-Date).AddDays(-7)

        #Removing Report older than 7 Days
        Get-ChildItem $filepath\DiskReport*.* | `
        Where-Object { $_.LastWriteTime -le $OldReports} | `
        Remove-Item -Recurse -Force -ErrorAction SilentlyContinue  

        #Create variable for log date
        $LogDate = get-date -f yyyyMMddhhmm

        $DiskReport = Get-WmiObject win32_logicaldisk `
        -ComputerName $servername -Filter "Drivetype=3" `
        -ErrorAction SilentlyContinue | 
        #return only disks with free space less than or equal to 0.1 (40%)
        Where-Object {   ($_.freespace/$_.size) -le '0.4'}

        #Create Reports
        $DiskReport | 
        Select-Object @{Label = "Server Name";Expression = {$_.SystemName}},
        @{Label = "Drive";Expression = {$_.DeviceID}},
        @{Label = "Total Space(GB)";Expression = {"{0:N1}" -f( $_.Size / 1gb)}},
        @{Label = "Free Space (GB)";Expression = {"{0:N1}" -f( $_.Freespace / 1gb ) }},
        @{Label = 'Free Space (%)'; Expression = {"{0:P0}" -f ($_.freespace/$_.size)}} |ConvertTo-Html  | Out-File D:\HTML_Report.html -Force
Ranadip Dutta
  • 8,857
  • 3
  • 29
  • 45
  • thanks Ranadip , i have issue in scheduling the script in task schedular, although the script is working fine but it does not work when scheduled. – Ahmad Raza Nov 29 '16 at 07:03
  • @AhmadRaza : I am pasting another script which will create scheduled task via powershell. Change the script path and the additional details as per you requirement. If you are satisfied with the Answers , please like it ; That will increase view and also will help others. – Ranadip Dutta Nov 29 '16 at 08:34
0
#Task scheduler Script as discussed

$jobname = "Powershell_Task"
$script =  "D:\PsScript.ps1"
$repeat = New-TimeSpan -Hours 1
$Start_Time= Get-Date
$action = New-ScheduledTaskAction –Execute "$pshome\powershell.exe" -Argument  "$script; quit"
$duration = ([timeSpan]::maxvalue)
$trigger = New-ScheduledTaskTrigger -Once -At $Start_Time -RepetitionInterval $repeat -RepetitionDuration $duration
$settings = New-ScheduledTaskSettingsSet -WakeToRun -DontStopIfGoingOnBatteries -StartWhenAvailable -RunOnlyIfNetworkAvailable -DontStopOnIdleEnd

Register-ScheduledTask -TaskName $jobname -Action $action -Trigger $trigger -Settings $settings
Ranadip Dutta
  • 8,857
  • 3
  • 29
  • 45