This topic contains 3 replies, has 2 voices, and was last updated by
November 13, 2015 at 4:53 pm #31957ParticipantPoints: 3Rank: Member
I am attempting to write a semi-advanced function that will accept paths as parameters. However, I want one of the parameters to depend on an earlier parameter. Basically, it assumes the second item is a direct child item of the parent (a directory). But I'm not sure how to write the code. Here is what I have so far:
[cmdletbinding()] Param ( #Source file for parsing [string]$sourcedir = "E:\desired\source\directory", [string]$sourcefile = "$sourcedir\$sourcefile", [string]$archivedir = "$sourcedir\Archived", [string]$To = "firstname.lastname@example.org", [string]$From = "email@example.com" )
The $sourcefile parameter is what I'm struggling with. Since I'm feeding it the $sourcedir explicitly with the full path, I want the full path of the $sourcefile to be determined by only feeding the parameter the $sourcefile file name. Does that make sense?
November 13, 2015 at 6:48 pm #31959ParticipantPoints: 0Rank: Member
You would not do this in your param section. The only reason to set your various parameters equal to something in the param section is if you want it to default to a specific value if the parameter is not specified.
So you would typically do something like this where $archivedir would equal "Archived" unless someone specified -archivedir "archives" or such. The $archivedir would be whatever they set.
[cmdletbinding()] Param ( #Source file for parsing [string]$sourcedir, [string]$sourcefile, [string]$archivedir = "Archived", [string]$To, [string]$From ) $sourcefilepath = "$sourcedir\$sourcefile" $archivedirpath = "$sourcedir\$archivedir"
November 13, 2015 at 8:40 pm #31961ParticipantPoints: 3Rank: Member
Thank you Curtis. I guess maybe I was overthinking the param block. Basically in this case the same script needs to be set up as 2 separate scheduled tasks, with a common source directory and 2 different source files (it's a monitoring script). I thought maybe I could make the path dynamic, but it sounds as if it's not very practical to do it that way. To use Mr. Jones' nomenclature, to me this script is more of a "controller" than a "tool" since it has a very specific use case, so I did not mind setting default values for the first and third param. The idea originally was to have the second param accept a file name only (not a complete path), and derive a complete path from that name.
November 13, 2015 at 8:59 pm #31962ParticipantPoints: 0Rank: Member
Based on that description, it sounds as though the only thing you want to really be definable at the time of run is the name of the source file to process. As such, the only input parameter your script needs to accept is the source file. The rest of the variables can be part of the main code, The only reason to define a parameter is if you want to be able to change its value when you call the code.
[cmdletbinding()] Param ( [string]$sourcefile ) $sourcedir = "E:\desired\source\directory" $sourcefilepath = "$sourcedir\$sourcefile" $archivedir = "$sourcedir\Archived" $To = "firstname.lastname@example.org" $From = "email@example.com"
The topic ‘Using dynamic paths as parameters’ is closed to new replies.