pathing variable returning null

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 5 months ago.

  • Author
    Posts
  • #5232

    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} | `
    %{gwmi -Query "ASSOCIATORS OF {Win32_DiskDrive.DeviceID=`"$($_.DeviceID.replace('\','\\'))`"} WHERE AssocClass = Win32_DiskDriveToDiskPartition"} | `
    %{gwmi -Query "ASSOCIATORS OF {Win32_DiskPartition.DeviceID=`"$($_.DeviceID)`"} WHERE AssocClass = Win32_LogicalDiskToPartition"} | `
    %{$_.deviceid}

    # write-host $usbdrive
    textbox1text("Copying files to $usbdrive...`n")

    $shell_app = new-object -com shell.application
    $zip_file = $shell_app.namespace((get-location).path + "\mkbootdisk.zip")
    $destination = $shell_app.namespace("$usbdrive\")
    $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\")
    $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

You must be logged in to reply to this topic.