PowerShell not processing arguments correctly at least I think so?

Viewed 99

I've written this code to format Flash Drives:

[CmdLetBinding()]

Param (
  [Parameter(Mandatory=$True)]
    [String] $DrvLtr,
  [Parameter(Mandatory=$False)]
    [ ValidateSet("NTFS","ReFS","exFAT",
                  "FAT32","FAT")]
    [String] $FileSys = "NTFS" ,
  [Parameter(Mandatory=$False)]
    [Switch] $TestIt
)

$FVArgs = @{DriveLetter = $DrvLtr
            FileSystem  = $FileSys
            Force       = $True
            Full        = $True
            Confirm     = $True
            WhatIf      = $($TestIt.isPresent)
           }

 Format-Volume @FVArgs

When I call the code as follows:

PS> .\Dev-New-Format-FlashDrive.ps1 -TestIt -DrvLtr x

It runs as though the -TestIt (value for -WhatIf) and -Confirm parameters to Format-Volume don't exist. The Format-Volume is processed and the drive is formatted.

I ran the code with a break-point at the Volume-Format line and tested the parameters with the following results:

PS> .\Dev-New-Format-FlashDrive.ps1 -TestIt -DrvLtr x

Hit Line breakpoint on 'G:\BEKDocs\Scripts\Dev-New-Format-FlashDrive.ps1:24'
PS> $FVArgs

Name                           Value                                           
----                           -----                                           
FileSystem                     NTFS                                            
Force                          True                                            
Confirm                        True                                            
WhatIf                         True                                            
DriveLetter                    x                                               
Full                           True                                            

PS> 

Ideas?

Edited: Theo, in response to your post:

Test Results:

PS> .\Test\Test-Format-FlashDriveOrdered.ps1 -DrvLtr "X" -FileSys FAT32 -TestIt

DriveLetter FriendlyName FileSystemType DriveType HealthStatus OperationalStatu
                                                               s               
----------- ------------ -------------- --------- ------------ ----------------
X                        FAT32          Removable Healthy      OK              

PS> .\Test\Test-Format-FlashDriveMovedSwitches.ps1 -DrvLtr "X" -FileSys FAT32 -TestIt

DriveLetter FriendlyName FileSystemType DriveType HealthStatus OperationalStatu
                                                               s               
----------- ------------ -------------- --------- ------------ ----------------
X                        FAT32          Removable Healthy      OK              

# Force set to False on all the following...

# WhatIf Removed
PS> .\Test\Test-Format-FlashDrive.ps1 -DrvLtr "X" -FileSys FAT32 -TestIt1
DriveLetter FriendlyName FileSystemType DriveType HealthStatus OperationalStatu
                                                               s               
----------- ------------ -------------- --------- ------------ ----------------
X                        FAT32          Removable Healthy      OK              


# Whatif Replaced Confirm Removed
PS> .\Test\Test-Format-FlashDrive.ps1 -DrvLtr "X" -FileSys FAT32 -TestIt

DriveLetter FriendlyName FileSystemType DriveType HealthStatus OperationalStatu
                                                               s               
----------- ------------ -------------- --------- ------------ ----------------
X                        FAT32          Removable Healthy      OK              
1 Answers

This is not an answer, I merely want to know more about this, so kindly, could you tell us what happens in the following scenarios:

About the Confirm switch

By default, the Shell automatically applies the Confirm switch to cmdlets that have the following verbs:

Clear, Disable, Dismount, Move , Remove, Stop, Suspend, Uninstall

Format seems not to be among those..
The page also states that "If you want to manually apply the Confirm switch to a command, include the Confirm switch at the end of the command"

Almost the same goes for the WhatIf switch: Again "When you run a command together with the WhatIf switch, you put the WhatIf switch at the end of the command"

This to me could imply that you cannot use these switches in a splatting Hashtable (??)

Out of curiousity, what happens if you try

[CmdLetBinding()]

Param (
  [Parameter(Mandatory=$True)]
    [String] $DrvLtr,
  [Parameter(Mandatory=$False)]
    [ ValidateSet("NTFS","ReFS","exFAT","FAT32","FAT")]
    [String] $FileSys = "NTFS" ,
  [Parameter(Mandatory=$False)]
    [Switch] $TestIt
)

# try with an [ordered] hash, so the Confirm and WhatIf switches are last
$FVArgs = [ordered]@{
    DriveLetter = $DrvLtr
    FileSystem  = $FileSys
    Force       = $True
    Full        = $True
    Confirm     = $True
    WhatIf      = $($TestIt.isPresent)
}

Format-Volume @FVArgs

And what happens if you take them out of the splatting hashtable like:

[CmdLetBinding()]

Param (
  [Parameter(Mandatory=$True)]
    [String] $DrvLtr,
  [Parameter(Mandatory=$False)]
    [ ValidateSet("NTFS","ReFS","exFAT","FAT32","FAT")]
    [String] $FileSys = "NTFS" ,
  [Parameter(Mandatory=$False)]
    [Switch] $TestIt
)

$FVArgs = @{
    DriveLetter = $DrvLtr
    FileSystem  = $FileSys
    Force       = $True
    Full        = $True
}

Format-Volume @FVArgs -Confirm -WhatIf:($TestIt.isPresent)
Related