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