locked
Looking for some help with my new starter script RRS feed

  • Question

  • Hi All,

    Hope you are well.

    I am in the middle off setting up a new start script for the company i work for but i am unable to get the button to work correctly.

    Would someone be able to show where i am going wrong.

    This is what i have so far. https://pastebin.com/g0pnRbEk

    Thanks


    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    Function Mirror_UserFun
    {
    param([string]$MU=$args[0])
    $MirrorUser = Get-ADUser -identity $mu -properties memberof | select memberof
    }
    Function Create_User
    {
    param()
        New-ADUser -SamAccountName $samaccountname -Name $displayname -Office $LC -ChangePasswordAtLogon $true -Title $JT -AccountPassword $password
        ForEach ($group in $MirrorUser)
        {
        Add-ADGroupMember $group -Members $samaccountname
        }
    }
    $textboxsize = New-Object System.Drawing.Size (85,8)
    $labelsize = New-Object System.Drawing.Size(65,15)
     
    $NewStarterForm = New-Object System.Windows.Forms.Form
        $NewStarterForm.Text = "New Start Form"
        $NewStarterForm.Size = New-Object System.Drawing.Size(300,500)
        $NewStarterForm.FormBorderStyle = "FixedDialog"
        $NewStarterForm.TopMost = $true
        $NewStarterForm.MaximizeBox = $false
        $NewStarterForm.MinimizeBox = $false
        $NewStarterForm.ControlBox = $true
        $NewStarterForm.KeyPreview = $True
        $NewStarterForm.StartPosition = "CenterScreen"
        $NewStarterForm.Font = "Segoe UI"
     
    #firstname Label
    $FirstName_NewStarterForm = New-Object System.Windows.Forms.Label
        $FirstName_NewStarterForm.Location = New-Object System.Drawing.Size(8,8)
        $FirstName_NewStarterForm.Size = $labelsize
        $FirstName_NewStarterForm.TextAlign = "TopLeft"
        $FirstName_NewStarterForm.Text = "First Name :"
        $NewStarterForm.Controls.Add($FirstName_NewStarterForm)
    #firstname textbox
        $firstname_textbox = New-Object System.Windows.Forms.TextBox
        $firstname_textbox.Location = New-Object System.Drawing.Size (85,8)
        $firstname_textbox.Size = $textboxsize
        $firstname_textbox.TextAlign = "Left"
        $firstname_textbox.Text
        $NewStarterForm.Controls.Add($firstname_textbox)
     
        #Initial Label
    $Intial_NewStarterForm = New-Object System.Windows.Forms.Label
        $Intial_NewStarterForm.Location = New-Object System.Drawing.Size(175,40)
        $Intial_NewStarterForm.Size = New-Object System.Drawing.Size(25,32)
        $Intial_NewStarterForm.TextAlign = "TopLeft"
        $Intial_NewStarterForm.Text = "In:"
        $NewStarterForm.Controls.Add($Intial_NewStarterForm)
     
    #Initial textbox
    $Intitial_textbox = New-Object System.Windows.Forms.TextBox
        $Intitial_textbox.Location = New-Object System.Drawing.Size (200,40)
        $Intitial_textbox.Size = $textboxsize
        $Intitial_textbox.TextAlign = "Left"
        $Intitial_textbox.Text
        $NewStarterForm.Controls.Add($Intitial_textbox)
     
    #Surname Label
    $Surname_NewStarterForm = New-Object System.Windows.Forms.Label
        $Surname_NewStarterForm.Location = New-Object System.Drawing.Size(10,44)
        $Surname_NewStarterForm.Size = $labelsize
        $Surname_NewStarterForm.TextAlign = "MiddleLeft"
        $Surname_NewStarterForm.Text = "Surname:"
        $NewStarterForm.Controls.Add($Surname_NewStarterForm)
     
    #Surname textbox
    $Surname_textbox = New-Object System.Windows.Forms.TextBox
        $Surname_textbox.Location = New-Object System.Drawing.Size (85,40)
        $Surname_textbox.Size = $textboxsize
        $Surname_textbox.TextAlign = "Left"
        $Surname_textbox.Text
        $NewStarterForm.Controls.Add($Surname_textbox)
     
    #Location Label
    $Location_NewStarterForm = New-Object System.Windows.Forms.Label
        $Location_NewStarterForm.Location = New-Object System.Drawing.Size(10,75)
        $Location_NewStarterForm.Size = $labelsize
        $Location_NewStarterForm.TextAlign = "BottomLeft"
        $Location_NewStarterForm.Text = "Location:"
        $NewStarterForm.Controls.Add($Location_NewStarterForm)
     
    #Location textbox
    $Location_textbox = New-Object System.Windows.Forms.TextBox
        $Location_textbox.Location = New-Object System.Drawing.Size (85,70)
        $Location_textbox.Size = $textboxsize
        $Location_textbox.TextAlign = "Left"
        $Location_textbox.Text
        $NewStarterForm.Controls.Add($Location_textbox)
     
    #Job Title Label
    $JobTitle_NewStarterForm = New-Object System.Windows.Forms.Label
        $JobTitle_NewStarterForm.Location = New-Object System.Drawing.Size(10,105)
        $JobTitle_NewStarterForm.Size = $labelsize
        $JobTitle_NewStarterForm.TextAlign = "BottomLeft"
        $JobTitle_NewStarterForm.Text = "Job Title:"
        $NewStarterForm.Controls.Add($JobTitle_NewStarterForm)
     
    #Job Title textbox
    $JobTitle_textbox = New-Object System.Windows.Forms.TextBox
        $JobTitle_textbox.Location = New-Object System.Drawing.Size (85,100)
        $JobTitle_textbox.Size = $textboxsize
        $JobTitle_textbox.TextAlign = "Left"
        $JobTitle_textbox.Text
        $NewStarterForm.Controls.Add($JobTitle_textbox)
     
    #Mirror Label
    $MirrorUser_NewStarterForm = New-Object System.Windows.Forms.Label
        $MirrorUser_NewStarterForm.Location = New-Object System.Drawing.Size(10,180)
        $MirrorUser_NewStarterForm.Size = $labelsize
        $MirrorUser_NewStarterForm.TextAlign = "BottomLeft"
        $MirrorUser_NewStarterForm.Text = "Mirror User:"
        $NewStarterForm.Controls.Add($MirrorUser_NewStarterForm)
     
    #Mirror textbox
    $MirrorUser_textbox = New-Object System.Windows.Forms.TextBox
        $MirrorUser_textbox.Location = New-Object System.Drawing.Size (85,175)
        $MirrorUser_textbox.Size = $textboxsize
        $MirrorUser_textbox.TextAlign = "Left"
        $MirrorUser_textbox.Text
        $NewStarterForm.Controls.Add($MirrorUser_textbox)
     
    #Mirror User Button
    $MirrorUser_Button = New-Object System.Windows.Forms.Button
        $MirrorUser_Button.Location = New-Object System.Drawing.Size(180,175)
        $MirrorUser_Button.Size = New-Object System.Drawing.Size(80,20)
        $MirrorUser_Button.Text = "Get User"
        $MirrorUser_Button.DialogResult = ([System.Windows.Forms.DialogResult]::retry)
        $NewStarterForm.Controls.Add($MirrorUser_Button)
     
     
    #CreateUser Button
    ##$CreateUser_Button = New-Object System.Windows.Forms.Button
    ##    $CreateUser_Button.Location = New-Object System.Drawing.Size(180,205)
    ##    $CreateUser_Button.Size = New-Object System.Drawing.Size(80,20)
    ##    $CreateUser_Button.Text = "Create User"
    ##    $CreateUser_Button.DialogResult = ([System.Windows.Forms.DialogResult]::retry)
    ##   $NewStarterForm.Controls.Add($CreateUser_Button)
    #HomeDrive Creation
     
    #Return Results + show form
    $NewStarterForm.Add_Shown({$NewStarterForm.Activate()})
    $result = $NewStarterForm.ShowDialog()
    if ($result -eq [System.Windows.Forms.DialogResult]::retry)
    {
      $MU = $mirroruser_textbox.Text
      $FN = $firstname_textbox.Text
      $SN = $Surname_textbox.Text
      $IN = $Intitial_textbox.text
      $LC = $Location_textbox.Text
      $JT = $JobTitle_textbox.Text
     
    }
     
    $samaccountname = $SN + $IN
    $displayname = $FN + $SN
    $password = 'Thursday.1'

    • Edited by Phil1988 Tuesday, May 22, 2018 3:08 PM
    Tuesday, May 22, 2018 2:48 PM

All replies

  • Use the code format tool and post your code to you post

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Tuesday, May 22, 2018 2:51 PM
  • Please only post your code here using the code posting tool provided.

    What is it the button code is supposed to do?


    \_(ツ)_/

    Tuesday, May 22, 2018 2:52 PM
  • $button.add_Click({  your code here })

    \_(ツ)_/

    Tuesday, May 22, 2018 2:54 PM
  • Sorry added that in
    Tuesday, May 22, 2018 3:07 PM
  • Added it in sorry, 

    I am trying to use the button to get AD user information

    Tuesday, May 22, 2018 3:07 PM
  • You should edit your original post and put it there.  People use these topics and need to have the OP presented in a clear way.


    \_(ツ)_/

    Tuesday, May 22, 2018 3:09 PM
  • Updated sorry again
    Tuesday, May 22, 2018 3:21 PM
  • Hi,

    I have add the function but each time i click the button i get this error "cannot find an object with identity: 'system.windows.forms.button"

    Wednesday, May 23, 2018 10:13 AM
  • Did you write this script or did you just copy it?


    \_(ツ)_/

    Wednesday, May 23, 2018 11:34 AM
  • I wrote it, how come ? Watch some video on youtube and tried to figure it out from there. This is the most amount of powershell i have done. I'm just learning and trying to find out how these things work

    • Edited by Phil1988 Wednesday, May 23, 2018 11:38 AM
    Wednesday, May 23, 2018 11:36 AM
  • I wrote it, how come ? Watch some video on youtube and tried to figure it out from there

    Then apply the example to your button. It is just n example.


    \_(ツ)_/

    Wednesday, May 23, 2018 11:38 AM
  • right ok thanks for all your help
    Wednesday, May 23, 2018 11:42 AM