0

So I'm diving into PowerShell for the first time. trying to just write a BitLocker script that will check the hard disk size before enabling BitLocker. Need a little help with outputting values. Problem I'm having is when im trying print out the values of the current size of the drive calling $test = NewSize Write-host or Write-Output it will skip past it and just move on to confirm

$DiskDrive = $env:SystemDrive
$BLV = Get-BitLockerVolume -MountPoint $DiskDrive
$OnlyDriveLetter = $DiskDrive.Substring(0,1)
$TotalSize = Get-PartitionSupportedSize -DriveLetter $OnlyDriveLetter
$MaxSize = (Get-PartitionSupportedSize -DriveLetter $OnlyDriveLetter).sizeMax
$UsedSize = (Get-PartitionSupportedSize -DriveLetter $OnlyDriveLetter).sizeMin

function NewSize()
{
    $NewDriveSize= Get-PartitionSupportedSize -DriveLetter $OnlyDriveLetter
    return $NewDriveSize | Select @{Name="Min. Size (GB)"; Expression={[Math]::Round($_.SizeMin/1GB, 2)}}, @{Name="Max Size (GB)";Expression={[Math]::Round($_.SizeMax/1GB, 2)}}
}


function Confirm($stringInput)
{
    do
    {
        choice /c yn /m $stringInput
        $response = $LASTEXITCODE
    
        if($response -eq 1)
        {
            Write-Host "Expanding Drive Size"
            #Resize-Partition -DriveLetter $OnlyDriveLetter-Size $TotalSize
            Write-Host "New Drive Size is:"
            NewSize
            exit
            
        }
        $response = 2
    } until ($response -eq 2)
        Write-Output "BitLocker encryption could not Be enabled, Exiting program!"
        
}

if(($BLV.VolumeStatus -eq 'FullyDecrypted') -and ($UsedSize -lt $MaxSize))
{
    $confirmation = "Would you like to expand drive Partition to Maximum Size? "
    $NotExtended = "Drive is Decrypted and Partition is not fully Extended" 
    $sizeOfDrive = "Current Drive Size is: "
    $CurrentDriveAllocation = NewSize
    
    Write-Host $NotExtended -ForegroundColor Cyan
    Write-Host $sizeOfDrive
    $test = NewSize
    Write-Host $test
    Confirm($confirmation)

}
  • 1
    You are probably hitting the same issue as here: [Output of a PowerShell command doesn't show result until after Write-Host and Read-Host](https://stackoverflow.com/questions/47359808/output-of-a-powershell-command-doesnt-show-result-until-after-write-host-and-re). Also, as a general rule, don't call PowerShell functions using parentheses. Instead of `Confirm($confirmation)` use `Confirm $confirmation`, or more formally: `Confirm -stringInput $confirmation` – boxdog Jun 14 '23 at 05:37
  • Note that the ```exit``` command in your ```Confirm``` function will terminate the entire script immediately (possibly even before pending output is rendered to the console) - see https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_language_keywords?view=powershell-7.3#exit. If you just want to leave the function and yield control back to the calling code, use ```return``` instead - https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_return?view=powershell-7.3 – mclayton Jun 14 '23 at 08:07
  • I'm not familiar with `Get-PartitionSupportedSize` so I could be wrong, but according to [the documentation](https://learn.microsoft.com/en-us/powershell/module/storage/get-partitionsupportedsize#outputs), that command only returns a `UInt64` object, yet you're trying to access the `SizeMin` and `SizeMax` properties from `$NewDriveSize`, which don't exist on `UInt64`. That could be why you're not getting any output. Try running `Get-PartitionSupportedSize` by itself and verify whether those properties exist on what gets returned. – jerdub1993 Jun 14 '23 at 21:43

0 Answers0