Can a validation script alter the parameter itself?

Welcome Forums General PowerShell Q&A Can a validation script alter the parameter itself?

Viewing 0 reply threads
  • Author
    • #5626
      Topics: 1562
      Replies: 0
      Points: 1
      Rank: Member

      by scottbass at 2013-03-04 23:14:05

      Test script foo.ps1
      Position = 0,
      if ($_.GetType().Fullname -like "System.IO.*") {$_ = $_.Fullname}
      $Path | % {$_.GetType().Fullname}

      # call using path (string)
      .\foo.ps1 c:\temp\temp1

      The "real script" will call GCI against the path string(s), retrieving the children.

      # call using System.IO objects (either files or directories)
      $files=gci c:\temp\temp1
      .\foo.ps1 $files

      In this second scenario, I need the full path to the file objects. If I just cast the System.IO objects to strings (by changing the parameter type declaration from [Object[]] to [String[]]), I get relative paths, and the GCI call later in the script fails.

      Can reformatting of the input parameter be accomplished via a validation script?

      by mjolinor at 2013-03-05 05:44:00

      I don’t think so. The validation script block is going to run in it’s own scope. You can set a variable there, but that instantiates a new variable, and it goes away when the validation script finishes and the scope is disposed.

Viewing 0 reply threads
  • The topic ‘Can a validation script alter the parameter itself?’ is closed to new replies.