简体   繁体   中英

I cant get powershell to run in task scheduler

I am trying to run the following Powershell script via the Task Scheduler but although it gives me a successful run (0x0), nothing happens. If I run the script manually from the same machine as a standard user the script executes without any issue.

$folderpath = "\\shared_path\excel.xls"
Add-Type -AssemblyName Microsoft.Office.Interop.Excel
$xlFixedFormat = [Microsoft.Office.Interop.Excel.XlFileFormat]::xlOpenXMLWorkbook
write-host $xlFixedFormat
$excel = New-Object -ComObject excel.application
$excel.visible = $false
$filetype ="*xls"
Get-ChildItem -Path $folderpath -Include $filetype -recurse | 
ForEach-Object `
{
    $path = ($_.fullname).substring(0, ($_.FullName).lastindexOf("."))
    
    $workbook = $excel.workbooks.open($_.fullname)
 
    $path += ".xlsx"
    $excel.DisplayAlerts = $false;
    $workbook.saveas($path, $xlFixedFormat)
    $workbook.close()
    
}
$excel.Quit()
$excel = $null
[gc]::collect()
[gc]::WaitForPendingFinalizers()

Just to clarify, I have tried different options in task scheduler such as running it under arguments like "-WindowStyle Hidden -NonInteractive -ExecutionPolicy Bypass -File "C:\Temp\powershell.ps1" or SYSTEM and currently logged on user but nothing made a difference.

My system is a domain joined Windows 10 with unrestricted access to the shared location (Everyone access)

Any ideas?

I usually configure Task Scheduler to run a PowerShell script with the following settings:

New Action

Action: "Start a program".

Program/script: "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"

Add arguments (optional): .\ ScriptName .ps1 - Arg1 Value1

Start in (optional): Path\to\Script\Directory

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM