Invoking script file from a custom cmdlet?
i writing custom cmdlet. in processrecord() method of cmdlet, want to execute an external script file. how achieve that?
and also, script file expects have parameters passed (it declaring params() tag @ top). how pass parameters, known @ runtime, script file when execute it?
any pointer appreciated.
what asking easy. let's have script called script1.ps1 in current directory. the contents this:
"these args:" $args
now have function takes argument, , calls script args of own in process block:
function doit { param( [parameter(mandatory=$true,position=0)] [string]$param ) process { & .\script1.ps1 $param, "blah" } } doit "done"
now, that's easy - technique find more useful general way create large sets of custom code. this may not want now, it's know can it:
function doit { param( [parameter(mandatory=$true,position=0)] [string]$param ) $scriptsegments = @() $scriptsegments += { $var1 = "blah" $var1 } $scriptsegments += $executioncontext.invokecommand.newscriptblock("get-childitem '$param'") $finalscript = "" $scriptsegments |foreach { $finalscript += $_.tostring() + "`n" } $finalscript = $executioncontext.invokecommand.newscriptblock($finalscript) & $finalscript } doit $pwd
i blogged in crazy stream of conscious article last winter. you can check out here if want see rabbit hole went down learn way make dynamic code in powershell:
http://powertoe.wordpress.com/2010/02/10/dynamic-code-in-powershell/
write-host ((0..56)|%{if (($_+1)%3 -eq 0){[char][int]("116111101110117102102064103109097105108046099111109"[($_-2)..$_] -join "")}}) -separator ""
Windows Server > Windows PowerShell
Comments
Post a Comment