private/Select-OSDeployDiskUSB.ps1
|
function Select-OSDeployDiskUSB { <# .SYNOPSIS Selects a USB disk by disk number .DESCRIPTION Uses supplied disk objects or discovers USB disks with Get-OSDeployDisk, then excludes disks outside the configured size limits. Eligible disks are displayed as a table and the function repeatedly prompts for a valid disk number. SelectOne returns a sole eligible disk without prompting. Skip adds S as a valid response and returns Boolean false when selected. This function does not modify disks. .PARAMETER Input Specifies disk objects to filter and select instead of calling Get-OSDeployDisk. This parameter accepts pipeline input. .PARAMETER MinimumSizeGB Specifies the exclusive minimum disk size in GiB. Default is 8. Aliases are Min, MinGB, and MinSize. .PARAMETER MaximumSizeGB Specifies the exclusive maximum disk size in GiB. Default is 1800. Aliases are Max, MaxGB, and MaxSize. .PARAMETER Skip Allows S at the Read-Host prompt and returns Boolean false when the user skips selection. .PARAMETER SelectOne Returns the eligible disk automatically when exactly one disk is available. .EXAMPLE PS> Select-OSDeployDiskUSB -MinimumSizeGB 16 -MaximumSizeGB 256 -Skip Displays USB disks between the size limits and allows the user to enter S to skip. .INPUTS System.Object. Accepts disk objects through Input. .OUTPUTS Microsoft.Management.Infrastructure.CimInstance. Returns the selected MSFT_Disk object. System.Boolean. Returns false when Skip is specified and the user enters S. .NOTES Author: David Segura Company: Recast Software Version: 1.0.0 Date: 2026-08-28 .LINK Get-OSDeployDisk #> [CmdletBinding()] param ( [Parameter(ValueFromPipeline = $true)] [Object]$Input, [Alias('Min','MinGB','MinSize')] [int]$MinimumSizeGB = 8, [Alias('Max','MaxGB','MaxSize')] [int]$MaximumSizeGB = 1800, [System.Management.Automation.SwitchParameter]$Skip, [System.Management.Automation.SwitchParameter]$SelectOne ) #================================================= # Get-Disk #================================================= if ($Input) { $Results = $Input } else { $Results = Get-OSDeployDisk -BusType USB | Where-Object {($_.Size -gt ($MinimumSizeGB * 1GB)) -and ($_.Size -lt ($MaximumSizeGB * 1GB))} } #================================================= # Process Results #================================================= if ($Results) { #================================================= # There was only 1 Item, then we will select it automatically #================================================= if ($PSBoundParameters.ContainsKey('SelectOne')) { Write-Verbose "[$($MyInvocation.MyCommand.Name)] Automatically select " if (($Results | Measure-Object).Count -eq 1) { $SelectedItem = $Results Return $SelectedItem } } #================================================= # Table of Items #================================================= $Results | Select-Object -Property Number, BusType, MediaType,` @{Name='SizeGB';Expression={[int]($_.Size / 1000000000)}},` FriendlyName, Model, PartitionStyle,` @{Name='Partitions';Expression={$_.NumberOfPartitions}} |` Format-Table | Out-Host #================================================= # Select an Item #================================================= if ($PSBoundParameters.ContainsKey('Skip')) { do {$Selection = Read-Host -Prompt "Select a USB Disk by Number, or press S to SKIP"} until (($Selection -ge 0) -and ($Selection -in $Results.Number) -or ($Selection -eq 'S')) if ($Selection -eq 'S') {Return $false} } else { do {$Selection = Read-Host -Prompt "Select a USB Disk by Number"} until (($Selection -ge 0) -and ($Selection -in $Results.Number)) } #================================================= # Return Selection #================================================= Return ($Results | Where-Object {$_.Number -eq $Selection}) #================================================= } } |