ValidateScript parameter attribute messes with Splatted input?

This topic contains 9 replies, has 3 voices, and was last updated by  Moon Sund 1 week ago.

  • Author
    Posts
  • #104050

    Moon Sund
    Participant
    function Convert-Wim2VHD {
      
      [CmdletBinding(SupportsShouldProcess = $true, 
        PositionalBinding = $false,
        ConfirmImpact = 'Medium')]
      Param
      (
        # Path to the new VHDX file (Must end in .vhdx)
        [Parameter(Position = 0, Mandatory = $true,
          HelpMessage = 'Enter the path for the new VHDX file')]
        [ValidateNotNullorEmpty()]
        [ValidatePattern(".\.vhdx?$")]
        [ValidateScript( {
            if (Get-FullFilePath -Path $_ |
                Split-Path  |
                Resolve-Path ) {
              $true 
            } 
            else {
              Throw "Parent folder for $_ does not exist."
            } 
          })]
    

    [ValidateScript( {
    if (Get-FullFilePath -Path $_ |
    Split-Path |
    Resolve-Path ) {
    $true
    }
    else {
    Throw "Parent folder for $_ does not exist."
    }

    If I feed up this function with splatted input, I am getting error.
    Is this a feature or a bug, and how to deal with it?

  • #104053

    Don Jones
    Keymaster

    What would the error be? Can you show an example of your splatted input? It's a little tough to guess just by reading your code.

    Parameter binding happens before validation, so there shouldn't be any difference.

  • #104056

    Moon Sund
    Participant

    Thank you Don

    I tried adding imgur picture link, but..

    PS H:\DEV\HVLab> $ConvertWim2VHD_Param = @{
        Path = 'H:\DEV\HVLab\Images\W2016_CORE.vhdx'
        Index = 3    Size = 51GB    DiskLayout = 'UEFI'
        Dynamic = $true    SourcePath = 'H:\DEV\HVLab\Media\en_windows_server_2016_vl_x64_dvd_11636701.iso'
    }
    
    Convert-Wim2VHD @$ConvertWim2VHD_Param -verbose 
    Convert-Wim2VHD : Cannot validate argument on parameter 'Path'. Cannot bind argument to parameter 'Path' because it is an empty
    string.
    At line:10 char:17
    + Convert-Wim2VHD @$ConvertWim2VHD_Param -verbose
    +                 ~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidData: (:) [Convert-Wim2VHD], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,Convert-Wim2VHD

    I am calling WindowsImageTools\Convert-Wim2VHD
    https://github.com/BladeFireLight/WindowsImageTools/tree/master/WindowsImageTools

    Name Value
    —- —–
    PSVersion 5.1.17134.137

    I expand splatting, the error has gone

  • #104060

    Moon Sund
    Participant

    Thank you Don

    SCREENSHOT ADDED

    PS H:\DEV\HVLab> $ConvertWim2VHD_Param = @{
        Path = 'H:\DEV\HVLab\Images\W2016_CORE.vhdx'
        Index = 3    Size = 51GB    DiskLayout = 'UEFI'
        Dynamic = $true    SourcePath = 'H:\DEV\HVLab\Media\en_windows_server_2016_vl_x64_dvd_11636701.iso'
    }
    
    Convert-Wim2VHD @$ConvertWim2VHD_Param -verbose 
    Convert-Wim2VHD : Cannot validate argument on parameter 'Path'. Cannot bind argument to parameter 'Path' because it is an empty
    string.
    At line:10 char:17
    + Convert-Wim2VHD @$ConvertWim2VHD_Param -verbose
    +                 ~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidData: (:) [Convert-Wim2VHD], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,Convert-Wim2VHD

    I am calling WindowsImageTools\Convert-Wim2VHD
    https://github.com/BladeFireLight/WindowsImageTools/tree/master/WindowsImageTools

    Name Value
    —- —–
    PSVersion 5.1.17134.137

    I expand splatting, the error has gone

  • #104069

    Don Jones
    Keymaster

    You triggered our spam filters ;). When that happens, just chill – reposting again will get you locked out. We clear the filters every weekday.

    You're not constructing your hash table correctly, it looks like.

    PS H:\DEV\HVLab> $ConvertWim2VHD_Param = @{
        Path = 'H:\DEV\HVLab\Images\W2016_CORE.vhdx'
        Index = 3
        Size = 51GB
        DiskLayout = 'UEFI'
        Dynamic = $true
        SourcePath = 'H:\DEV\HVLab\Media\en_windows_server_2016_vl_x64_dvd_11636701.iso'
    }
    

    It's either one item per line, or you have to separate them all with semicolons. It's the ValidateNotNullOrEmpty() that's causing the error.

  • #104074

    Moon Sund
    Participant

    Of course I had formatted the hashtable properly: I have no clue where the distortion came from
    What I can suppose... I have copied it from freshly installed VSCode and perhaps bad line endings come from it

  • #104089

    Moon Sund
    Participant

    There is definitely a problem with freshly installed VSCode
    (I am trying to automate my dev client setup with DSC config, doing 'learning by doing').

    (Imgur link has been eaten by this forum)

    Please see extra whitespace injected by Code into the middle of "Q:\" string – that leads to termination
    What is wrong for me?

  • #104248

    Moon Sund
    Participant

    Please help to find out the root problem.

    Confirmed exception in the powershell console host.
    Can it be bad encoding in git repo (cloned with freshly installed git clone)?

    PS C:\dev> ipmo .\WindowsImageTools\WindowsImageTools\WindowsImageTools.psm1
    WARNING: The names of some imported commands from the module 'WindowsImageTools' include unapproved verbs that might
    make them less discoverable. To find the commands with unapproved verbs, run the Import-Module command again with the
    Verbose parameter. For a list of approved verbs, type Get-Verb.

    PS C:\dev> Convert-Wim2VHD -Path 'H:\HyperVLab\Images\W2016_GUI.vhdx' -Index 4 -Size 59GB -DiskLayout UEFI -Dynamic -Sou
    rcePath 'H:\HypervLab\Media\en_windows_server_2016_vl_x64_dvd_11636701.iso' -WhatIf
    What if: [Convert-Wim2VHD] : Overwrite partitions inside [H:\HyperVLab\Images\W2016_GUI.vhdx] with content of [H:\Hyperv
    Lab\Media\en_windows_server_2016_vl_x64_dvd_11636701.iso]

    PS C:\dev> $ConvertWim2VHD_Param = @{
    >> Path = 'H:\HyperVLab\Images\W2016_GUI.vhdx'
    >> Index = 4
    >> Size = 59GB
    >> DiskLayout = 'UEFI'
    >> Dynamic = $true
    >> SourcePath = 'H:\HyperVLab\Media\en_windows_server_2016_vl_x64_dvd_11636701\Media\en_windows_server_2016_vl_x
    64_dvd_11636701.iso'
    >> }
    PS C:\dev> Convert-Wim2VHD @$ConvertWim2VHD_Param -verbose
    Convert-Wim2VHD : Cannot validate argument on parameter 'Path'. Cannot bind argument to parameter 'Path' because it is
    an empty string.
    At line:1 char:17
    + Convert-Wim2VHD @$ConvertWim2VHD_Param -verbose
    + ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidData: (:) [Convert-Wim2VHD], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Convert-Wim2VHD

  • #104312

    bob
    Participant

    You are calling the function incorrectly

    PS C:\dev> Convert-Wim2VHD @$ConvertWim2VHD_Param -verbose
    

    When splatting remove the $ it should be called as this

    PS C:\dev> Convert-Wim2VHD @ConvertWim2VHD_Param -verbose
    

    https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_splatting?view=powershell-6

  • #104365

    Moon Sund
    Participant

    Thank you, bob.
    Shame on me for such an oversight!

    I am suffering of the race condition between chicken and egg, trying different modules and still not having a decent stable codebase.
    What a pity with https://github.com/theJasonHelmick/PS-AutoLab-Env having had become deprecated

You must be logged in to reply to this topic.