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