Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

My first PowerShell script, also my first C# code -- extracting a zipped file

$
0
0

I'm familiar with coding in VBA but this is my first PowerShell script and my first C# code.

I've been tasked with writing a PowerShell script to unzip a file and then change the filename. Unzipping the file works fine if I hardcode the filename:

function Extract-Zip {
   param([string]$zipfilename, [string] $destination)
   $shellApplication = new-object -com shell.application
  
#   $zipPackage = $shellApplication.NameSpace($zipfilename) 
   $zipPackage = $shellApplication.NameSpace("C:\MyPath\SimpleZipname.zip")

   $destinationFolder = $shellApplication.NameSpace("C:\MyPath")
  
   $myfile = $destinationFolder.CopyHere($zipPackage.Items())
   Write-Host $myfile
}

Extract-Zip "C:\MyPath\SimpleZipname.zip", "C:\MyPath"

But if I try to use the same path passed in as a parameter, I get the error "You cannot call a method on a null-valued expression":

function Extract-Zip {
   param([string]$zipfilename, [string] $destination)
   $shellApplication = new-object -com shell.application
  
   $zipPackage = $shellApplication.NameSpace($zipfilename)
#   $zipPackage = $shellApplication.NameSpace("C:\MyPath\SimpleZipname.zip")

   $destinationFolder = $shellApplication.NameSpace("C:\MyPath")
  
   $myfile = $destinationFolder.CopyHere($zipPackage.Items())
   Write-Host $myfile
}

Extract-Zip "C:\MyPath\SimpleZipname.zip", "C:\MyPath"

What am I doing wrong?

I'm on Windows 7. This will eventually go on a Windows 2008 Server.

Thanks,

Greg


Viewing all articles
Browse latest Browse all 21975

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>