Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Script loops when scheduled - works fine when run manually

$
0
0

This script runs fine when run manually but when its run from a scheduled task it gets stuck in the while loop.  It doesn't appear to be executing the Excel calls.  I have tried adding -executionPolicy bypass to the parameters of the scheduled task.

# Report Variables

$reportfile = "C:\Support\Reports\Test2.xlsx"

 

# Create new Excel Object 

$xl = New-Object -ComObject "Excel.Application"

$wkb = $xl.Workbooks.Open("$reportfile")

# Add new sheet

# Check for duplicate sheet

$sheet = $wkb.Worksheets.Add([System.Reflection.Missing]::Value,$wkb.Worksheets.Item($wkb.Worksheets.count))

$sheetname = (date -f yyyy-MM-dd)

$sheetsuffix = $null

$sheetnum = 1

while ($sheet.Name -ne ($sheetname + $sheetsuffix)) 

{

try 

{

        $sheet.Name = $sheetname + $sheetsuffix

    }

    catch [system.exception] 

{

        $sheetnum++

        $sheetsuffix = " ($sheetnum)"

   

}

$wkb.Save()

$wkb.Close()

$xl.Quit()


Viewing all articles
Browse latest Browse all 6937

Trending Articles