Scripts work on Windows 7 and not on Windows 10


ok 

here how goes

a simple scriptlet ask selection drop down. 

the 1 can see here:

script:

####################################################################

[void] [system.reflection.assembly]::loadwithpartialname("system.windows.forms")  [void] [system.reflection.assembly]::loadwithpartialname("system.drawing")     $objform = new-object system.windows.forms.form   $objform.text = "select computer"  $objform.size = new-object system.drawing.size(300,200)   $objform.startposition = "centerscreen"    $objform.keypreview = $true  $objform.add_keydown({if ($_.keycode -eq "enter")       {$x=$objlistbox.selecteditem;$objform.close()}})  $objform.add_keydown({if ($_.keycode -eq "escape")       {$objform.close()}})    $okbutton = new-object system.windows.forms.button  $okbutton.location = new-object system.drawing.size(75,120)  $okbutton.size = new-object system.drawing.size(75,23)  $okbutton.text = "ok"  $okbutton.add_click({$x=$objlistbox.selecteditem;$objform.close()})  $objform.controls.add($okbutton)    $cancelbutton = new-object system.windows.forms.button  $cancelbutton.location = new-object system.drawing.size(150,120)  $cancelbutton.size = new-object system.drawing.size(75,23)  $cancelbutton.text = "cancel"  $cancelbutton.add_click({$objform.close()})  $objform.controls.add($cancelbutton)    $objlabel = new-object system.windows.forms.label  $objlabel.location = new-object system.drawing.size(10,20)   $objlabel.size = new-object system.drawing.size(280,20)   $objlabel.text = "please select computer:"  $objform.controls.add($objlabel)     $objlistbox = new-object system.windows.forms.listbox   $objlistbox.location = new-object system.drawing.size(10,40)   $objlistbox.size = new-object system.drawing.size(260,20)   $objlistbox.height = 80    [void] $objlistbox.items.add("atl-dc-001")  [void] $objlistbox.items.add("atl-dc-002")  [void] $objlistbox.items.add("atl-dc-003")  [void] $objlistbox.items.add("atl-dc-004")  [void] $objlistbox.items.add("atl-dc-005")  [void] $objlistbox.items.add("atl-dc-006")  [void] $objlistbox.items.add("atl-dc-007")    $objform.controls.add($objlistbox)     $objform.topmost = $true    $objform.add_shown({$objform.activate()})  [void] $objform.showdialog()    $x

####################################################################

does not work on windows 10. 

any thing need install/call/make reference in scriptlet make work on windows10 ?


####################################################################
####################################################################

mcsa, mcp, microsoft specialist - microsoft azure

hi,

ah, see mean. it's scope issue: on ps2, code triggered events executed on own scope. in ps5, it's executed in own scope. can work around this, forcing x in global scope:

# old $x=$objlistbox.selecteditem  # new $global:x = $objlistbox.selecteditem

note: not need use keydown events - can assign values after user closes form.

cheers,
fred


there's no place 127.0.0.1



Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

some help on Event 540

WMI Repository 4GB limit - Win 2003 Ent Question

Event ID 1302 (error 1307) DFS replication service encountered an error while writing to the debug log file