Powershell script cannot access a file when run as a Scheduled Task

前端 未结 3 1383
别跟我提以往
别跟我提以往 2020-12-09 11:07

My Powershell (2.0) script has the following code snippet:

$fileName = \"c:\\reports\\1.xlsx\"
$xl = new-object -comobject excel.application
$xlFormat = [Mic         


        
相关标签:
3条回答
  • 2020-12-09 11:28

    To extend what @TessellatingHeckler provided, you can run the following commands in Powershell(As Admin/Elevated) to create the folders before opening excel, in my script this fixed the issue:

    New-Item -ItemType Directory -Force -Path C:\Windows\System32\config\systemprofile\Desktop
    if ([Environment]::Is64BitProcess -ne [Environment]::Is64BitOperatingSystem)
    {
        New-Item -ItemType Directory -Force -Path C:\Windows\SysWOW64\config\systemprofile\Desktop
    }
    
    0 讨论(0)
  • 2020-12-09 11:35

    The solutions above didn't work in my SCSM 2012 Scorch environment, instead I used PSExcel (https://github.com/RamblingCookieMonster/PSExcel) which has no dependency on having Excel installed or the ComObject.

    0 讨论(0)
  • 2020-12-09 11:37

    I think you've hit a bug in Excel:

    You have to create a folder (or two on a 64bit-windows):

    (32Bit, always)

    C:\Windows\System32\config\systemprofile\Desktop

    (64Bit)

    C:\Windows\SysWOW64\config\systemprofile\Desktop

    I have had the same problem and this was the only solution i have found.

    From TechNet Forums (via PowerShell and Excel Issue when Automating )

    0 讨论(0)
提交回复
热议问题