pathing variable returning null

by ChrisL at 2012-10-25 14:11:04

Hi everyone, hoping you guys can help (this community is awesome btw).

I’m writing a script that detects usb drives plugged into a computer and allows the tech to select them to be partioned/formatted/"imaged" with a WinPE image, but I’m getting an error (you cannot call a method on a null-valued expression) during the unzip line. Here’s the code segment:


$usbdrive = get-wmiobject win32_diskdrive | where {$.index -eq $usbindex} | <br> %{gwmi -Query &quot;ASSOCIATORS OF {Win32_DiskDrive&#46;DeviceID="$($.DeviceID.replace(‘',’\‘))&quot;} WHERE AssocClass = Win32_DiskDriveToDiskPartition&quot;} |
%{gwmi -Query "ASSOCIATORS OF {Win32_DiskPartition.DeviceID=&quot;$($_&#46;DeviceID)"} WHERE AssocClass = Win32_LogicalDiskToPartition"} | <br> %{$_&#46;deviceid}<br><br># write-host $usbdrive<br> textbox1text(&quot;Copying files to $usbdrive&#46;&#46;&#46;n")

$shell_app = new-object -com shell.application
$zip_file = $shell_app.namespace((get-location).path + "\mkbootdisk.zip")
$destination = $shell_app.namespace("$usbdrive&quot;)
$destination.copyhere($zip_file.items(), 0x10)


It’s the last line $destination.copyhere($zip_file.items(), 0x10) that generates the error on character 40, which should be $zip_file.items()

So I verified $zip_file is coming back null, but as far as I can asses this should be correct, the file is present and the name matches. Any idea what I’m missing? I’m on the fringe of my powershell IQ here…
by DonJ at 2012-10-25 14:23:29
Ah, COM.

Can you verify that your $shell_app.namespace() call is returning anything on its own? I assume you’re using that COM object mainly because of it’s ZIP support?

Unfortunately, if the COM object isn’t doing what it’s supposed to… well… I dunno. Me, I’d try writing the same script in VBScript just to see if I got different results, to eliminate the possibility of it being some "COM object no likey PowerShell today" issue. If the COM objects misbehaves in VBS also, then it’s kinda out of my realm.

The VBS would be roughly:


Dim shell_app,zip_file
Set shell_app = WScript.CreateObject("Shell.Application")
Set zip_file = shell_app.namespace("hardcode-the-path-here-to-test")
’ Verify that zip_file isn’t empty or null


Man, that felt weird to type. Anyway, that might be worth a quick test.
by ChrisL at 2012-10-25 14:45:36
Thanks for the response.

$shell_app.namespace is returning System.__ComObject.namespace
Yes, I’m using com for it’s zip support to avoid having to use a third party tool like 7zip, but I can if needbe.
The VBscript returned [quote]mkbootdisk.zip[/quote]
by ChrisL at 2012-10-25 14:56:29
I figured it out… kind of; if I hardcode the path it works properly
$shell_app = new-object -com shell.application
$zip_file = $shell_app.namespace("\serverlocation\mkbootdisk.zip")
$destination = $shell_app.namespace("$usbdrive&quot;)
$destination.copyhere($zip_file.items(), 0x10)
by Klaas at 2012-10-26 01:22:11
There is a Powerzip module:
http://powershell.codeplex.com/releases/view/58369