Hi! I'm trying to run a PowerShell script on Win 7 x64 as a scheduled task to open and refresh an Excel 2013 (x32) file's data sources, then save it. I get the following error:
PS E:\> e:\RefreshExcelFiles.ps1 e:\Test.xlsx
The term 'e:\RefreshExcelFiles.ps1' is not recognized as the name of a cmdlet, function, script file, or operable progr
am. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:25
+ e:\RefreshExcelFiles.ps1 <<<< e:\Test.xlsx
+ CategoryInfo : ObjectNotFound: (e:\RefreshExcelFiles.ps1:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
I assume this means I have to import some module, but which one, and where do I find it?
This is the script code:
try { # Creating the excel COM Object $xl = New-Object -ComObject Excel.Application; # Setting up Excel to run without UI and without alerts $xl.DisplayAlerts = $false; $xl.Visible = $false; } Catch { Write-EventLog -EventId "5001" -LogName "Application" -Message "Failed to start Excel" -Source "Application" Exit } foreach ($i in $args) { write-host "handling $i" try { # Allow update only if we can perform check out If ($xl.workbooks.CanCheckOut($i)) { # Opening the workbook, can be local path or SharePoint URL $wb = $xl.workbooks.open($i); # Perform the check out $xl.workbooks.checkout($i) # Calling the refresh $wb.RefreshAll(); # Saving and closing the workbook $wb.CheckInWithVersion(); # in case you are not using checkout/checkin perform a save and close #$wb.Save(); #$wb.Close(); #Release Workbook [System.Runtime.Interopservices.Marshal]::ReleaseComObject($wb) } else { write-host "Check out failed for: $i" Write-EventLog -EventId "5001" -LogName "Application" -Message "Workbook can't be checked out $i" -Source "Application" } } catch { Write-EventLog -EventId "5001" -LogName "Application" -Message "Failed refreshing the workbook $i $_" -Source "Application" } } #Quiting Excel $xl.quit(); #Release Excel [System.Runtime.Interopservices.Marshal]::ReleaseComObject($xl)
TIA!
Re
Dennis
- Edited by sgude1 Sunday, December 15, 2013 5:08 AM Typo