mardi 20 novembre 2018

PowerShell script not zipping on different computer

#zipping old files and sending them to a file on desktop if older than 60 days

Function Zip

{

Param

(

[Parameter(Mandatory)]

[string]$zipFile

,

[Parameter(Mandatory)]

[String[]]$toBeZipped

)

$null = & "C:\Program Files\7-Zip\7z.exe" A -tzip $zipFile $toBeZipped

}

$Now = Get-Date

$Days = "60"

$LastWrite = (Get-Date).Date.AddDays(-$Days)

$TargetFolder = "D:\Testing\*"

$Files = Get-Childitem $TargetFolder -Recurse | Where {$_.LastWriteTime -le "$LastWrite"} | Select-Object -ExpandProperty Fullname

$Files

Zip "$($ENV:USERPROFILE)\Desktop\TEST.zip" $Files

#deleting old files from source folder

Get-ChildItem $targetfolder -Recurse | WHERE {($_.CreationTime -le $(Get-Date).AddDays(-$Days))} | Remove-Item -Force

I have this script for zipping and deleting old files. I tried it and tested it on a VM and a computer. I tried using it on another different computer, but on this one it doesn't work. This is what I get:

Program '7z.exe' failed to run: The filename or extension is too longAt 
C:\Users\Admin\Documents\zip help.ps1:23 char:9
+ $null = & "C:\Program Files\7-Zip\7z.exe" A -tzip $zipFile $toBeZippe ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.
At C:\Users\Admin\Documents\zip help.ps1:23 char:1
+ $null = & "C:\Program Files\7-Zip\7z.exe" A -tzip $zipFile $toBeZippe ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (:) [], ApplicationFailedEx 
   ception
    + FullyQualifiedErrorId : NativeCommandFailed

The script can't use 7-zip. The other computers had 7-Zip in the exact same file path so I am unsure of what my problem is. Any input will be appreciated.

Aucun commentaire:

Enregistrer un commentaire