Problem with Powershell Zip folder Script

(imported topic written by JavierCO)

Hello,

I have a problem with the execution of powershell script in a task.

This powershell runs properly from command line, but when I try to execute it with a task, I get this error:

New-Object : Cannot load COM type Shell.Application.
At C:\Program Files (x86)\BigFix Enterprise\BES Client__BESData\CustomSite_MOR
SE-2dGestion\massforensics-win.ps1:233 char:27

  • $shellApp = New-Object <<<<  -Com Shell.Application
    
    • CategoryInfo : InvalidType: (:slight_smile: [New-Object], PSArgumentExcepti
      on
    • FullyQualifiedErrorId : CannotLoadComObjectType,Microsoft.PowerShell.Com
      mands.NewObjectCommand

I don’t know how solve this problem, I’m not the script’s creator.

The function that includes the problematic line tries to zip a folder. This is the code:

function ZipFolder(
[IO.DirectoryInfo] $directory)
{
If ($directory -eq $null)
{
Throw “Value cannot be null: directory”
}

Write-Host ("Creating zip file for folder (" + $directory.FullName + ")...")

[IO.DirectoryInfo] $parentDir = $directory.Parent

[string] $zipFileName

If ($parentDir.FullName.EndsWith("\") -eq $true)
{
    # e.g. $parentDir = "C:\"
    $zipFileName = $parentDir.FullName + $directory.Name + ".zip"
}
Else
{
    $zipFileName = $parentDir.FullName + "\" + $directory.Name + ".zip"
}

If (Test-Path $zipFileName)
{
    Throw "Zip file already exists ($zipFileName)."
}

Set-Content $zipFileName ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
    
$shellApp = New-Object -Com Shell.Application
$zipFile = $shellApp.NameSpace($zipFileName)

If ($zipFile -eq $null)
{
    Throw "Failed to get zip file object."
}

[int] $expectedCount = (Get-ChildItem $directory -Force -Recurse).Count
$expectedCount += 1 # account for the top-level folder

$zipFile.CopyHere($directory.FullName,1564)

# wait for CopyHere operation to complete
WaitForZipOperationToFinish $zipFile $expectedCount

Write-Host -Fore Green ("Successfully created zip file for folder (" `
    + $directory.FullName + ").")

}

Somebody can help me, please?