powershell - Using a $Variable to Submit -Filepath Argument for Start-Process -
this question has answer here:
- powershell gui not set variable 1 answer
i'm using powershell version 3. have weird problem fix. i've written powershell script reads config csv file (with application paths , names) , creates form application buttons. when try submit start-process filepath
argument variable wont work. when echo variable correct. maybe here knows whats wrong , can me. error
argument null or empty in: + $btn.add_click({start-process -filepath "$buttoncommand"})
i tried fix problem solution of stackoverflow thread:
$buttoncommand = $buttoncommand.replace("\","\\").replace('"',"")
and tried submit $buttoncommand
, without '"'
#search script path, locate config file in same path $fullpathincfilename = $myinvocation.mycommand.definition $currentscriptname = $myinvocation.mycommand.name $currentexecutingpath = $fullpathincfilename.replace($currentscriptname, "") $configfile = $currentexecutingpath + "admintools.conf" #read config file $stringarray = get-content $configfile #count lines in config file $configlinecount = get-content $configfile | measure-object –line # create button function function createbutton ($buttoncommand, $buttonname, $buttonlocx, $buttonlocy, $buttonsizex, $buttonsizey) { $btn = new-object system.windows.forms.button $btn.add_click({start-process -filepath "$buttoncommand"}) $btn.text = $buttonname $btn.location = new-object system.drawing.size($buttonlocx,$buttonlocy) $btn.size = new-object system.drawing.size($buttonsizex,$buttonsizey) $btn.cursor = [system.windows.forms.cursors]::hand $btn.backcolor = [system.drawing.color]::lightgreen $form.controls.add($btn) } # define form add-type -assemblyname system.windows.forms $form = new-object windows.forms.form $form.size = new-object drawing.size @(260,250) $form.text = "tools" $form.startposition = "centerscreen" #for each line in configfile button gets created. foreach ($arrayline in $stringarray) { $ar = [string]$arrayline $ar = $ar.split(";") $userarray += @($ar[1]) createbutton $ar[0] $ar[1] $ar[2] $ar[3] $ar[4] $ar[5] } #show form $drc = $form.showdialog()
you running scope issue. inside small script block $buttoncommand
not initialized. 1 solution declare script block in own variable.
$clickevent = {start-process -filepath "$buttoncommand"} $btn.add_click($clickevent)
there couple of threads 2 worth noting here @ so , on technet
in same vein use variable in global scope around issue.
$btn.add_click({start-process -filepath "$global:buttoncommand"})
Comments
Post a Comment