user3542587
user3542587

Reputation: 317

Unable to compress a file with Powershell 2.0

Q1. I tried few compressed method but none of it work on my machine. I just can use external zip tool 7z.exe to compressed the file but I dont have the privilege to install the 7z.exe file in serverA nor update the powershell to v5. currently using powershell v2 Tried as below but none of it working. So, any other method can intro me for compressed file?

Q2. Below are the query I'm using 7z.exe tools (this serverB does come with 7z.exe) but I encountered error. I want to compress any file with today's date.

$timestamp = (Get-Date).ToString('yyyy-MM')
$source = "D:\csv\*.csv", "D:\csv2\*.csv"
$target = "D:\CSV2_$timestamp.zip"
$7zip = "D:\Program Files\7-Zip\7z.exe"

#Compressed file
if (-not (test-path $7zip)) {throw '$7zip needed'} 
set-alias sz $7zip  

sz a -mx=9 $target $source
{
    Get-ChildItem $source | Where{$_.LastWriteTime -gt (Get-Date).AddDays(-1)}
}

Note: Both server I also need compressed file but ServerA doesnt come 7z, but Server B does come with 7z.exe

Upvotes: 2

Views: 994

Answers (3)

mhu
mhu

Reputation: 18041

This should work:

Add-Type -Assembly "System.IO.Compression.FileSystem"
[System.IO.Compression.ZipFile]::CreateFromDirectory($sourcePath, $destinationZip)

See Add-Type: Cannot add type. The assembly 'System.IO.Compression.FileSystem' could not be found for alternative ways to load the required assembly

Upvotes: 1

Shadowfax
Shadowfax

Reputation: 586

According to your second question, where 7z is installed in ServerB, this function archive the files you want, this code do not depend on the powershell version you have.

function Compress-Items ($ItemsPaths, $dest) {
    $Path = "D:\Program Files\7-Zip\7z.exe"
    $argList = "a -tzip -y `"$dest`""
    foreach ($item in $ItemsPaths) {
        $argList += " `"$item`""
    }

    Start-Process -FilePath $Path -ArgumentList $argList
}

$source = (get-childitem -Path "D:\csv", "D:\csv2" -Include "*.csv" -Recurse).FullName
Compress-Items -ItemsPaths $source -dest $destination

Note

I modified your $source because this is the right way to get all the csv files you want.

Upvotes: 0

Bacon Bits
Bacon Bits

Reputation: 32155

You'll have to use the much older Shell.Application COM Object method.

function Extract-Zip
{
    param([string]$zipfilename, [string] $destination)

    if(test-path($zipfilename))
    {   
        $shellApplication = new-object -com shell.application
        $zipPackage = $shellApplication.NameSpace($zipfilename)
        $destinationFolder = $shellApplication.NameSpace($destination)
        $destinationFolder.CopyHere($zipPackage.Items())
    }
}

Note that I think this only works on Windows Vista or Server 2008 or later. If you're using Server 2003 -- and you should not be -- then you'll have to use third party software, as far as I'm aware.

It should go without saying, but you desperately need to update your servers. I'm not saying you need to install the most recent PowerShell. I'm saying that you're obviously using Server 2008 R2 or earlier, and this is 2019.

Upvotes: 1

Related Questions