Name_me
Name_me

Reputation: 39

powershell null-valued expression error while unzip

I am trying to download a zip file from S3 and unzip to a particular folder using the below script:

$zipfile = "myapp.zip"
$dest_loc = "C:\test\"
aws s3 cp s3://apptxtmy/$zipfile $dest_loc
$shell = New-Object -Com Shell.Application
$zip = $shell.NameSpace("$dest_loc\$zipfile")
if (!(Test-Path "C:\test\appname\")) { 
    mkdir C:\test\appname
}
$shell.Namespace("C:\test\appname\").CopyHere($zip.items())

But I keep on getting the below error:

You cannot call a method on a null-valued expression. At C:\Users\Administrator\Desktop\deploy.ps1:9 char:1 + $shell.Namespace("C:\test\appname\").CopyHere($zip.items())

Any help would be appreciated.

Thanks

Upvotes: 2

Views: 409

Answers (1)

henrycarteruk
henrycarteruk

Reputation: 13227

If you want to stick to native Powershell commands...

You can use Expand-Archive (if you're on PS v4+) to extract the zip file.

And Read-S3Object (part of AWS Tools for PowerShell) to get the file from S3.

Join-Path can also be used to ensure you don't have issues with double slash \\ in your paths.

$zipfile = "myapp.zip"
$dest_loc = "C:\test"
$appname = "appname"
$bucket = "apptxtmy"

$unzip_loc = Join-Path $dest_loc $appname
$zip_loc = Join-Path $dest_loc $zipfile

Read-S3Object -BucketName $bucket -Key $zipfile -file $zip_loc

if ((Test-Path $unzip_loc) -eq $false) { 
    New-Item $unzip_loc -ItemType Directory
}

Expand-Archive -Path $zip_loc -DestinationPath $unzip_loc

Upvotes: 1

Related Questions