My first PowerShell script, also my first C# code

by GregL65 at 2012-11-09 09:41:54

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]


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]

What am I doing wrong?

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


Thanks,

Greg
by DonJ at 2012-11-09 10:13:35
Lose the comma in your command. Parameters aren’t separated with commas.

Extract-Zip -zipfilename c:\simplezipname.zip -destination c:\mypath
by GregL65 at 2012-11-11 09:46:20
Thanks! :slight_smile: