Hello
I'm having trouble running a PowerShell Script with the Task Scheduler.
It works just fine when I run it directly in PowerShell but as soon as I try to use the Task Schedular it seems to only work partly.
I already created the desktop Folder as suggested in a post I found
C:\Windows\SysWOW64\config\systemprofile\Desktop
#Starting Transcript to file
$go = "D:\Transcript.txt:"
Start-Transcript -Path $gitgud
#Silently continus the Script incase an error occures
$ErrorActionPreference = "Silentlycontinue"
#Delete last months Excel so folder wont get too big
$path = "D:\Script"
Get-ChildItem -Path $path -include *.xls, *.xlsx -recurse |
foreach($_) { Remove-Item $_.fullname } | Out-Null
#Create excel COM object
$excel = New-Object -ComObject excel.application
#Make Visible
$excel.Visible = $True
$excel.DisplayAlerts = $false
#Add a workbook
$workbook = $excel.Workbooks.Add()
#Remove other worksheets
1..2 | ForEach {
$Workbook.worksheets.item(2).Delete()
}
#Connect to first worksheet to rename and make active
$serverInfoSheet = $workbook.Worksheets.Item(1)
$serverInfoSheet.Activate() | Out-Null
#Create a Title for the first worksheet
$row = 1
$Column = 1
$serverInfoSheet.Cells.Item($row,$column)= "Disk Space Information $(get-date -f yyyy-MM-dd)"
$Selection = $serverInfoSheet.Range("A1:F2")
$Selection.Merge()
$Selection.VerticalAlignment = -4160
#Give it a nice Style so it stands out
$serverInfoSheet.Cells.Item($row,$column).Font.Size = 18
$serverInfoSheet.Cells.Item($row,$column).Font.Bold=$True
$serverInfoSheet.Cells.Item($row,$column).Font.Name = "Cambria"
$serverInfoSheet.Cells.Item($row,$column).Font.ThemeFont = 1
$serverInfoSheet.Cells.Item($row,$column).Font.ThemeColor = 4
$serverInfoSheet.Cells.Item($row,$column).Font.ColorIndex = 55
$serverInfoSheet.Cells.Item($row,$column).Font.Color = 8210719
#Increment row for next set of data
$row++;$row++
#Save the initial row so it can be used later to create a border
$initalRow = $row
#Create a header for Disk Space Report; set each cell to Bold and add a background color
$serverInfoSheet.Cells.Item($row,$column)= 'Computername'
$serverInfoSheet.Cells.Item($row,$column).Interior.ColorIndex =48
$serverInfoSheet.Cells.Item($row,$column).Font.Bold=$True
$Column++
$serverInfoSheet.Cells.Item($row,$column)= 'DeviceID'
$serverInfoSheet.Cells.Item($row,$column).Interior.ColorIndex =48
$serverInfoSheet.Cells.Item($row,$column).Font.Bold=$True
$Column++
$serverInfoSheet.Cells.Item($row,$column)= 'TotalSizeGB'
$serverInfoSheet.Cells.Item($row,$column).Interior.ColorIndex =48
$serverInfoSheet.Cells.Item($row,$column).Font.Bold=$True
$Column++
$serverInfoSheet.Cells.Item($row,$column)= 'UsedSpaceGB'
$serverInfoSheet.Cells.Item($row,$column).Interior.ColorIndex =48
$serverInfoSheet.Cells.Item($row,$column).Font.Bold=$True
$Column++
$serverInfoSheet.Cells.Item($row,$column)= 'FreeSpaceGB'
$serverInfoSheet.Cells.Item($row,$column).Interior.ColorIndex =48
$serverInfoSheet.Cells.Item($row,$column).Font.Bold=$True
$Column++
$serverInfoSheet.Cells.Item($row,$column)= '%Free'
$serverInfoSheet.Cells.Item($row,$column).Interior.ColorIndex =48
$serverInfoSheet.Cells.Item($row,$column).Font.Bold=$True
#Increment Row and reset Column back to first column
$row++
$Column = 1
#Getting Server Names
$disk = Get-Content D:\servers.txt, D:\testservers.txt
$servers = Get-Content D:\servers.txt, D:\testservers.txt
#Pinging all servers and writing output to Error file
foreach ($server in $servers) {
if (test-Connection -ComputerName $server -Count 2 -Quiet ) {
Write-Output "$server is alive"
} else
{ Write-Output "$server seems dead"
}
}
#Get the drives and filter out CD/DVD drives
$diskDrives = Get-WmiObject win32_LogicalDisk -Filter "DriveType='3'" -ComputerName $disk
#Process each disk in the collection and write to spreadsheet
ForEach ($disk in $diskDrives) {
$serverInfoSheet.Cells.Item($row,$column)= $disk.__Server
$Column++
$serverInfoSheet.Cells.Item($row,$column)= $disk.DeviceID
$Column++
$serverInfoSheet.Cells.Item($row,$column)= ($disk.Size /1GB).ToString("#0.00")
$Column++
$serverInfoSheet.Cells.Item($row,$column)= (($disk.Size - $disk.FreeSpace)/1GB).ToString("#0.00")
$Column++
$serverInfoSheet.Cells.Item($row,$column)= ($disk.FreeSpace / 1GB).ToString("#0.00")
$Column++
$serverInfoSheet.Cells.Item($row,$column)= ("{0:P}" -f ($disk.FreeSpace / $disk.Size))
$Column++
#Check to see if space is near empty and use appropriate background colors
$range = $serverInfoSheet.Range(("A{0}" -f $row),("F{0}" -f $row))
$range.Select() | Out-Null
#Determine if disk needs to be flagged for warning or critical alert
#Critical threshold
If ($disk.FreeSpace -lt 5GB) {
$range.Interior.ColorIndex = 3
#Warning threshold
} ElseIf ($disk.FreeSpace -lt 10GB) {
$range.Interior.ColorIndex = 6
}
#Add row and reset Column for the next Server
$row++
$Column = 1
}
#Creat Borders for the Data
$row--
$dataRange = $serverInfoSheet.Range(("A{0}" -f $initalRow),("F{0}" -f $row))
7..12 | ForEach {
$dataRange.Borders.Item($_).LineStyle = 1
$dataRange.Borders.Item($_).Weight = 2
}
#All numbers from right to left for readability
$border = $serverInfoSheet.Range(("C{0}" -f $initalRow),("F{0}" -f $row))
7..12 | ForEach {
$border.HorizontalAlignment = -4152
}
#Auto fit everything so it looks better
$usedRange = $serverInfoSheet.UsedRange
$usedRange.EntireColumn.AutoFit() | Out-Null
#Save the file
$workbook.SaveAs("D:\Script\$(get-date -f yyyy-MM-dd) DiskSpace.xlsx")
#Closing the file and releasing it
$excel.Quit()
[System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$excel) | Out-Null
#Convert all exels in this Folder and its Subfolders to Pdf
$path = "D:\Script"
$xlFixedFormat = “Microsoft.Office.Interop.Excel.xlFixedFormatType” -as [type]
$excelFiles = Get-ChildItem -Path $path -include *.xls, *.xlsx -recurse
$objExcel = New-Object -ComObject excel.application
$objExcel.visible = $false
foreach($wb in $excelFiles)
{
$filepath = Join-Path -Path $path -ChildPath ($wb.BaseName + “.pdf”)
$workbook = $objExcel.workbooks.open($wb.fullname, 3)
$workbook.ExportAsFixedFormat($xlFixedFormat::xlTypePDF, $filepath)
$objExcel.Workbooks.close()
}
#Closing the file
$objExcel.Quit()
#Stoping the Transcript
Stop-Transcript
kill -processname Excel
It safes the Transcript file, deletes all excel files in the designated folder and opens Excel in the Background but wont safe the Excel file.
What are the possible reasons why it wouldnt work as intended with the Task Scheduler?