Powershell script calls other powershell script with read-host lines -
i have multiple powershell scripts, each of them has read-host lines, user can provide values script, example name of server or true/false in cases.
ike create powershell script, call other scripts, question: there way main script fill in read-host values?
or best way handle then? prefer not change current existing scripts already.
stop trying re-invent wheel. powershell has ability prompt missing parameters, use read things server names. has ability prompt confirmation before doing dangerous:
ps c:\> function foo-bar >> { >> [cmdletbinding(supportsshouldprocess=$true, >> confirmimpact='high')] >> param >> ( >> # target server >> [parameter(mandatory=$true, >> valuefrompipeline=$true, >> valuefrompipelinebypropertyname=$true, >> valuefromremainingarguments=$false, >> position=0)] >> [validatenotnull()] >> [string[]] >> $servername >> ) >> >> process >> { >> foreach ($srv in $servername) { >> if ($pscmdlet.shouldprocess("$srv", "foo-bar server")) >> { >> write-output "$srv has been foo'ed" >> } >> } >> } >> } >> ps c:\> foo-bar cmdlet foo-bar @ command pipeline position 1 supply values following parameters: servername[0]: first servername[1]: second servername[2]: third servername[3]: confirm sure want perform action? performing operation "foo-bar server" on target "first". [y] yes [a] yes [n] no [l] no [s] suspend [?] (default "y"): y first has been foo'ed confirm sure want perform action? performing operation "foo-bar server" on target "second". [y] yes [a] yes [n] no [l] no [s] suspend [?] (default "y"): second has been foo'ed third has been foo'ed ps c:\> foo-bar alpha,beta -confirm:$false alpha has been foo'ed beta has been foo'ed ps c:\>
put code cmdlets , use shouldprocess
, have full control on when user prompted continue , whether or not prompted missing values.
this gives dry-run support free:
ps c:\> foo-bar alpha,beta -whatif if: performing operation "foo-bar server" on target "alpha". if: performing operation "foo-bar server" on target "beta".
Comments
Post a Comment