Null-Valued Expression and Zip Extraction

Hi Guys,

I am currently trying to write a powershell script that at some point will extract a zip file into a folder and then the zip files within that folder.

So far my code goes like this. 


##GETZIP() - Retreive the zip folder and extract it to an output location##
function getZip()
{
#Change the Path variable
$location= Read-host 'Enter your zip folder path'
$oDestination = $path + \Output 

extractZip $location $oDestination 

}

function extractZip($path, $destination)
{

#Force the location into existence
New-Item -ItemType Directory -Force -Path $destination$shell_app= New-Object -com shell.application

#Get .zip from path location$files = Get-ChildItem -Path $path -filter *.zip -recurse

#for each file within the zip extract to the output folder
foreach($file in $files) 
{  
$zip_file = $shell_app.namespace($file.FullName)  $copyHere = $shell_app.namespace($destination)  $copyHere.Copyhere($zip_file.items())
}
}

Now when I run this code I get a "You cannot call a method on a null-valued expression...$copyHere.CopyHere <<<< ($zip_files())" is causing the issue. Yet if I revert to some older code this works without issue. 

##GETZIP() - Retreive the zip folder and extract it to an ouput location##

function getZip()
{

# Change the Path variable
$path = Read-host 'Enter your zip folder path'
$destination = $path + \Output
 
#Force the location into existence
New-Item -ItemType Directory -Force -Path $destination

$shell_app= New-Object -com shell.application

#Get .zip from path location
$files = Get-ChildItem -Path $path -filter *.zip -recurse

#for each file within the zip extract to the output folder 

foreach($file in $files) {

  $zip_file = $shell_app.namespace($file.FullName)

  $copyHere = $shell_app.namespace($destination)

  $copyHere.Copyhere($zip_file.items())

}
}

What gives?

February 26th, 2015 11:57am

Hi Karnee,

                The problem that you have with the script is a simple name change in the variable. In the old code you used:

$path = Read-host 'Enter your zip folder path'
$destination = $path + \Output

but in the new one you used:

$location= Read-host 'Enter your zip folder path'
$oDestination = $path + \Output 

you changed the variable from $path to $location, when capturing the output, but you did not changed it when using the value, so you are still referring to the value stored in $path that is now empty.

you are sending an incomplete path to the extractZip function that it's unable to process the copy because the path does not exists.

change one of the 2 rows and this will work like  a charm.

Hope it helps

Free Windows Admin Tool Kit Click here and download it now
February 26th, 2015 12:59pm

The function order is fine.  All functions have been created before either one is called.

The issue is an old one. You cannot reliably call shell methods from script. They were designed as extensions to explorer and for uose in GUI applications.  PowerShell has no method of handling this.  The shell extraction will run async in the background causing issues with further calls.

Use the compression classes to extract zipped files.

add-type -AssemblyName System.IO.Compression.FileSystem
[System.IO.Compression.ZipFile]::ExtractToDirectory($archive,$target)

February 26th, 2015 1:01pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics