I created a script in powergui but it will not work in the powershell console Help Please.


i new powershell , playing around. trying make script gui based add users active directory specific ou.  i created script , runs fine in powergui not in powershell console.  it seems input entered in text boxes not come in script variable. can review script , me figure out wrong?

#generated form function
function generateform {

#region import assemblies
[reflection.assembly]::loadwithpartialname("system.drawing") | out-null
[reflection.assembly]::loadwithpartialname("system.windows.forms") | out-null
#endregion

#region generated form objects
$form1 = new-object system.windows.forms.form
$label4 = new-object system.windows.forms.label
$label3 = new-object system.windows.forms.label
$label2 = new-object system.windows.forms.label
$label1 = new-object system.windows.forms.label
$textbox4 = new-object system.windows.forms.textbox
$textbox3 = new-object system.windows.forms.textbox
$textbox2 = new-object system.windows.forms.textbox
$textbox1 = new-object system.windows.forms.textbox
$button1 = new-object system.windows.forms.button
$initialformwindowstate = new-object system.windows.forms.formwindowstate
#endregion generated form objects

#----------------------------------------------
#generated event script blocks
#----------------------------------------------
#provide custom code events specified in primalforms.
$handler_label4_click= 
{
#todo: place custom script here

}

$handler_button1_click= 
{
#todo: place custom script here

}

$onloadform_statecorrection=
{#correct initial state of form prevent .net maximized form issue
$form1.windowstate = $initialformwindowstate
}

#----------------------------------------------
#region generated form code
$system_drawing_size = new-object system.drawing.size
$system_drawing_size.height = 458
$system_drawing_size.width = 449
$form1.clientsize = $system_drawing_size
$form1.databindings.defaultdatasourceupdatemode = 0
$form1.name = "form1"
$form1.text = "primal form"

$label4.databindings.defaultdatasourceupdatemode = 0

$system_drawing_point = new-object system.drawing.point
$system_drawing_point.x = 40
$system_drawing_point.y = 321
$label4.location = $system_drawing_point
$label4.name = "label4"
$system_drawing_size = new-object system.drawing.size
$system_drawing_size.height = 23
$system_drawing_size.width = 378
$label4.size = $system_drawing_size
$label4.tabindex = 8
$label4.text = 'what users password be? example:"p@ssw0rd"'
$label4.add_click($handler_label4_click)

$form1.controls.add($label4)

$label3.databindings.defaultdatasourceupdatemode = 0

$system_drawing_point = new-object system.drawing.point
$system_drawing_point.x = 40
$system_drawing_point.y = 230
$label3.location = $system_drawing_point
$label3.name = "label3"
$system_drawing_size = new-object system.drawing.size
$system_drawing_size.height = 23
$system_drawing_size.width = 378
$label3.size = $system_drawing_size
$label3.tabindex = 7
$label3.text = 'what ldap path users stored? example: ou="ou"dc="domain",dc="local"'

$form1.controls.add($label3)

$label2.databindings.defaultdatasourceupdatemode = 0

$system_drawing_point = new-object system.drawing.point
$system_drawing_point.x = 40
$system_drawing_point.y = 143
$label2.location = $system_drawing_point
$label2.name = "label2"
$system_drawing_size = new-object system.drawing.size
$system_drawing_size.height = 23
$system_drawing_size.width = 378
$label2.size = $system_drawing_size
$label2.tabindex = 6
$label2.text = "what email domain? example: ctcomp.com"

$form1.controls.add($label2)

$label1.databindings.defaultdatasourceupdatemode = 0

$system_drawing_point = new-object system.drawing.point
$system_drawing_point.x = 40
$system_drawing_point.y = 62
$label1.location = $system_drawing_point
$label1.name = "label1"
$system_drawing_size = new-object system.drawing.size
$system_drawing_size.height = 23
$system_drawing_size.width = 378
$label1.size = $system_drawing_size
$label1.tabindex = 5
$label1.text = "what is location of .csv file? example: c:/newusers.csv"

$form1.controls.add($label1)

$textbox4.databindings.defaultdatasourceupdatemode = 0
$system_drawing_point = new-object system.drawing.point
$system_drawing_point.x = 121
$system_drawing_point.y = 350
$textbox4.location = $system_drawing_point
$textbox4.name = "textbox4"
$system_drawing_size = new-object system.drawing.size
$system_drawing_size.height = 20
$system_drawing_size.width = 297
$textbox4.size = $system_drawing_size
$textbox4.tabindex = 4

$form1.controls.add($textbox4)

$textbox3.databindings.defaultdatasourceupdatemode = 0
$system_drawing_point = new-object system.drawing.point
$system_drawing_point.x = 121
$system_drawing_point.y = 259
$textbox3.location = $system_drawing_point
$textbox3.name = "textbox3"
$system_drawing_size = new-object system.drawing.size
$system_drawing_size.height = 20
$system_drawing_size.width = 297
$textbox3.size = $system_drawing_size
$textbox3.tabindex = 3

$form1.controls.add($textbox3)

$textbox2.databindings.defaultdatasourceupdatemode = 0
$system_drawing_point = new-object system.drawing.point
$system_drawing_point.x = 121
$system_drawing_point.y = 169
$textbox2.location = $system_drawing_point
$textbox2.name = "textbox2"
$system_drawing_size = new-object system.drawing.size
$system_drawing_size.height = 20
$system_drawing_size.width = 297
$textbox2.size = $system_drawing_size
$textbox2.tabindex = 2

$form1.controls.add($textbox2)

$textbox1.databindings.defaultdatasourceupdatemode = 0
$system_drawing_point = new-object system.drawing.point
$system_drawing_point.x = 121
$system_drawing_point.y = 88
$textbox1.location = $system_drawing_point
$textbox1.name = "textbox1"
$system_drawing_size = new-object system.drawing.size
$system_drawing_size.height = 20
$system_drawing_size.width = 297
$textbox1.size = $system_drawing_size
$textbox1.tabindex = 1

$form1.controls.add($textbox1)


$button1.databindings.defaultdatasourceupdatemode = 0

$system_drawing_point = new-object system.drawing.point
$system_drawing_point.x = 376
$system_drawing_point.y = 416
$button1.location = $system_drawing_point
$button1.name = "button1"
$system_drawing_size = new-object system.drawing.size
$system_drawing_size.height = 30
$system_drawing_size.width = 61
$button1.size = $system_drawing_size
$button1.tabindex = 0
$button1.text = "done"
$button1.usevisualstylebackcolor = $true
$button1.add_click({$data = $textbox1.text;$domain = $textbox2.text;$ldap = $textbox3.text;$password = $textbox4.text;$form1.close()})

$form1.controls.add($button1)

#endregion generated form code

#save initial state of form
$initialformwindowstate = $form1.windowstate
#init onload event correct initial state of form
$form1.add_load($onloadform_statecorrection)
#show form
$form1.showdialog()| out-null

$class = “user”
$tu = 0

write-host $data

import-csv $data | foreach {
if($_.first) {
$name = $_.first + " " + $_.last
$adsi = [adsi]“ldap://$ldap"
$cnuser=”cn=”+$name
$pa = “smtp:” + $_.logonname
$mail = $_.logonname + "@" + $domain
write-host -foregroundcolor green “building worker drone:                    ” $name
""
    $user = $adsi.create($class,$cnuser)
$user.put(“samaccountname”, $_.logonname)
$user.put(“mail”, $mail)
$user.put(“proxyaddresses”, $pa)
$user.put(“userprincipalname”, $mail)
$user.put(“givenname”, $_.first)
$user.put(“sn”, $_.last)
if($_.initials) {
$user.put(“initials”, $_.initials)
}
$user.put(“displayname”, $name)
$user.setinfo()
$user.psbase.invokeset(“accountdisabled”, “false”)
$user.setinfo()
$user.pwdlastset = -1
$user.setinfo()
$user.psbase.invoke("setpassword", $password)
$user.psbase.commitchanges()
$tu += 1

}
}

write-host -foregroundcolor green congratulations have created $tu users in ad 

i able resolve issue self sorry wasting thread.  but if falls same issue fix simple.

in section cannot take in textbox , turn variable in same line.

$button1.add_click({$data = $textbox1.text;$domain = $textbox2.text;$ldap = $textbox3.text;$password = $textbox4.text;$form1.close()})

you need this 

$button1.add_click({$textbox1.text;$textbox2.text;$textbox3.text;$textbox4.text;$form1.close()})

$data = $textbox1.text

$domain = $textbox2.text

$ldap = $textbox3.text

$password = $textbox4.text



Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

WIMMount (HSM) causing cluster storage to go redirected (2012r2 DC)

Failed to delete the test record dcdiag-test-record in zone test.com

Azure MFA with Azure AD and RDS