Multi USB OSDBootMedia Solution

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 2 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
localpct
Posts: 397
Last visit: Thu Oct 27, 2022 5:57 am

Multi USB OSDBootMedia Solution

Post by localpct »

Hello,

I'm looking to build a solution for our techs to update their OSD boot USBs - I have this but I have 1 question and am occurring 1 error
  1. $USBs = Get-Disk | Select-Object -Property * | Where-Object {$_.BusType -eq 'USB' -and $_.Size -gt 0} | Select-Object DiskNumber, FriendlyName, @{Name="Size (GB)"; Expression={[math]::round($_.size/1GB, 1)}}, @{Name="Serial"; Expression={ $_.Path.Split('#')[2].replace('&0','').toupper() }} | sort DiskNumber
  2. $MatchSerial = (gwmi Win32_USBControllerDevice |%{[wmi]($_.Dependent)} | Where-Object {($_.Description -like '*mass*')} | Select-Object -ExpandProperty Path | ForEach-Object { Split-Path $_ -Leaf}) -replace('"','')
  3.  
  4. #Build Drives
  5. #robocopy \\server\OSDBootMedia\Media C:\Windows\TechDir\TempOSDBuild /MIR /mt:128
  6. $USBs |Where-Object {$_.Serial -in $MatchSerial} | ForEach-Object {
  7.         $USB = $_
  8.         write-host "Prepping $($USB.FriendlyName)"
  9.         $FreeDriveLetter = ((69..90 | ForEach-Object{$L=[char]$_;if ((Get-PSDrive).Name -notContains $L){$L}})[0])
  10.         $USB | Clear-Disk -RemoveData -RemoveOEM -Confirm:$false | Out-Null
  11.         $USB | New-Partition -UseMaximumSize -DriveLetter $FreeDriveLetter | Format-Volume -FileSystem FAT32 -Force | Out-Null
  12.         $tempLetter = $FreeDriveLetter+':'
  13.         $drive = Get-WmiObject -Class win32_volume | Where-Object {$_.DriveLetter -eq $tempLetter}
  14.         Set-WmiInstance -input $drive -Arguments @{DriveLetter=$tempLetter”; Label=”OSDBoot”} | Out-Null
  15.         write-host "Writing to $($USB.FriendlyName)"
  16.         robocopy C:\Windows\TechDir\TempOSDBuild "$($FreeDriveLetter):" /NFL /NDL /NJH /NJS /nc /ns /np /MIR /MT:128
  17. }
1st Question, how can I also get drive letter of the USB in my $USBs variable? I'm aware of Get-WmiObject Win32_Volume -Filter "DriveType='2'" but I can't seen to find a link to verify I'm working the right drive

Error - it appears on the last drive ( I'm testing with 4 different USBs) it doesn't complete the New-Partition so the USB gets lost, and I need to go to diskmgmt.msc to format it correctly
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Multi USB OSDBootMedia Solution

Post by jvierra »

You will have to assign a drive letter sa teh letter acquired when it mounts will become invalid when you "clear" the USB of formatting and partitioning.

Just set the parameter to auto assign the next available drive letter when it is partitioned.
User avatar
localpct
Posts: 397
Last visit: Thu Oct 27, 2022 5:57 am

Re: Multi USB OSDBootMedia Solution

Post by localpct »

I'm talking before the loop. Sorry if that wasn't clear in my sentence.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Multi USB OSDBootMedia Solution

Post by jvierra »

How can you know what the drive is before it is created? Are you just asking how to detect the drive letter of a drive? Just use Get-PsDrive.
User avatar
localpct
Posts: 397
Last visit: Thu Oct 27, 2022 5:57 am

Re: Multi USB OSDBootMedia Solution

Post by localpct »

lets back up

First two lines of my code detect current connected USBs on the Load event to display to my users. Currently I display just the friendlyname, but I would like to include the drive letter.
Snag_acaa2.png
Snag_acaa2.png (8.06 KiB) Viewed 3551 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Multi USB OSDBootMedia Solution

Post by jvierra »

As I noted, you can use Get-PsDrive to get the drive letter of any storage device.
User avatar
localpct
Posts: 397
Last visit: Thu Oct 27, 2022 5:57 am

Re: Multi USB OSDBootMedia Solution

Post by localpct »

We have :)
But that doesn't appear to be a solution for me as I can't find the linkage to verify.
Unless I'm missing something as you can see from the image below, I have no way of linking from any of the cmds
Snag_cc70b35.png
Snag_cc70b35.png (115.66 KiB) Viewed 3375 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Multi USB OSDBootMedia Solution

Post by jvierra »

You have to know something in advance. What is it that you know that can tell you which device is the one you want to select? Consider asking the user that inserted the device to tell you what the label on the device is then search for the label using Get-PsDrive.
User avatar
localpct
Posts: 397
Last visit: Thu Oct 27, 2022 5:57 am

Re: Multi USB OSDBootMedia Solution

Post by localpct »

When the application is opened, this code is executed in my Load function and the GUI looks like the screenshot above. Because the techs have already inserted their USB drives before launching the application that makes their USB Drives.
How would I use PSDrive within this script to also display the drive so the screen looks like the inline screen shot here
  1. $global:USBs = Get-Disk | Select-Object -Property * | Where-Object { $_.BusType -eq 'USB' -and $_.Size -gt 0 } | Select-Object DiskNumber, FriendlyName, @{ Name = "Size (GB)"; Expression = { [math]::round($_.size/1GB, 1) } }, @{ Name = "Serial"; Expression = { $_.Path.Split('#')[2].replace('&0', '').toupper() } } | sort DiskNumber
  2.         foreach ($USB in $USBs)
  3.         {
  4.             $treeview1.Nodes.Add($USB.FriendlyName)
  5.             $statusbar1.Text = "Hello $(whoami), you have $($USBs.FriendlyName.count) devices connected."
  6.         }
Snag_1307d3.png
Snag_1307d3.png (8.15 KiB) Viewed 3328 times
ask the user lol - that's a good one
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Multi USB OSDBootMedia Solution

Post by jvierra »

Use Get-PsDrive to match the label on the disk or disoplay all USB disks and labels with Get-PsDrive and ask the user to select the one they want to have prepared.
This topic is 2 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked