Automate Creation of Bootable USB keys with PowerShell

This is a small module that enables two new cmdlets GET-DISKPARTINFO INVOKE-USBBOOT GET-DISKPARTINFO will parse DISKPART and return it's information as a PowerShell object INVOKE-USBBOOT will create DISKPART scripts to make attached USB drives bootable

 
 
 
 
 
5 Star
(1)
2,591 times
Add to favorites
Hardware
4/1/2015
E-mail Twitter del.icio.us Digg Facebook
Sign in to ask a question


  • problem with line 14
    1 Posts | Last post April 26, 2017
    • I'm getting different results with the script as written on two different systems. Win 10 seems to use 8 lines, but Win 7 seems to use 7.
      
      Using a hard coded count of lines isn't a particularly safe approach, given these results. I think a better solution would be something along the lines of 
      
         $totaldisk = ($listdisk | Select-String -Pattern "Disk [0-9]").count
      
      The pattern "Disk [0-9]" will get only those lines that match the word "Disk " followed by a 0 - 9.
      
  • How do you run it?
    4 Posts | Last post April 01, 2015
    • As someone who is very new to Powershell, how do I use this script?
      
      Thanks.
    • You need to import the module and then use the function 
      Import-Module C:\Downloads\usbbootmodule.psm1
    • If I run the command you specified, I get:
      
      PS K:\> Import-Module C:\Scripts\usbbootmodule.psm1
      At C:\Scripts\usbbootmodule.psm1:64 char:73
      + $drivelist=$info | where { $_.Type -eq 'USB' -and $_.DiskSize -gt 7.5GB $_.DiskS ...
      +                                                                         ~~
      Unexpected token '$_' in expression or statement.
          + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
          + FullyQualifiedErrorId : UnexpectedToken
      
      Import-Module : The specified module 'C:\Scripts\usbbootmodule.psm1' was not loaded because no valid module file was
      found in any module directory.
      At line:1 char:1
      + Import-Module C:\Scripts\usbbootmodule.psm1
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : ResourceUnavailable: (C:\Scripts\usbbootmodule.psm1:String) [Import-Module], FileNotFoun
         dException
          + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
    • Working on elimination the 0 byte bug.  the rest of the module has been updated with your critique.  Thanks for the feedback!
  • Typos
    4 Posts | Last post April 01, 2015
    • Hi,
      
      I am testing this on win 7 x64 service pack 1, and I needed to make these changes. Am I doing somethign wrong?
      
      Function syntax error:
      [cmdletbinding]
      
      on my system needs to be
      [cmdletbinding()]
      Param()
      
      Line 14:
      $totaldisk=$listdisk.count-9
      should be
      $totaldisk=$listdisk.count-8
      
      Line 21 and 22:
      $size=$listdisk[8+$d].substring(25,9).replace(" ","")
      $diskid=$listdisk[8+$d].substring(7,5).trim()
      should be:
      
      $size=$listdisk[7+$d].substring(25,9).replace(" ","")
      $diskid=$listdisk[7+$d].substring(7,5).trim()
      
      And an unfortunate bug I don't suppose would be easy to fix.
      I have multiple partitions in my Disk 0, C, D, and W. The drive letter W: is the one reported. Not a big deal, but confusing when you are trying to troublshoot.
      
      I'm unable to test it on another system, because I've got a usb mounted SD card reader (built into my moniotr USB hub) that comesu up as Disk 1, size 0b. The script chokes on it:
      
      Exception calling "ToInt16" with "2" argument(s): "Index was out of range. Must be non-negative and less than the size
      of the collection.
      Parameter name: startIndex"
      At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\get-diskpartinfo\get-diskpartinfo.psm1:55 char:31
      + $disktotal=([convert]::ToInt16 <<<< ($intsize,10))*$mult
          + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
          + FullyQualifiedErrorId : DotNetMethodException
      
      
    • Same problem...
    • Line 16 should also be changed
      for ($d=0;$d -le $totaldisk;$d++)
      
      should be
      for ($d=0;$d -lt $totaldisk;$d++)
      
      Otherwise you end up looping one extra time
    • Working on elimination the 0 byte bug.  the rest of the module has been updated with your critique.  Thanks for the feedback!