Hi guys.<\/p>\n
I am almost new in PowerShell script and I wrote a script<\/p>\n
but variable won’t work can anyone help me ?<\/p>\n
here is the part of the script not working<\/p>\n
Add-Type -AssemblyName System.Windows.Forms\n\tAdd-Type -AssemblyName System.Drawing\n\tFunction AddEm\n\t{\n\t\t$TextBox4.Text = $TextBox1.Text + \" \" + $TextBox2.Text\n\t\t$Textbox3.Visible = $true\n\t\t$Textbox3.Text = $Textbox1.Text[0] + \"\" + $Textbox2.Text + \"@email.com\"\n\t\t$Textbox3.Visible = $true\n\t}\nFunction MakeForm\n{\n\t$script:Form1 = New-Object system.Windows.Forms.Form\n\t$Form1.Text = \"Add Mail box\"\n\t$Font = New-Object System.Drawing.Font(\"Times New Roman\", 12, [System.Drawing.FontStyle]::Bold)\n\t$Form1.Font = $Font\n\t$label = New-Object Windows.Forms.Label\n\t$label.Location = New-Object Drawing.Point 150, 80\n\t$label.Size = New-Object Drawing.Point 200, 25\n\t$label.text = \"First Name\"\n\t$Form1.Controls.Add($label)\n\t$script:TextBox1 = New-Object System.Windows.Forms.Textbox\n\t$TextBox1.Location = New-Object System.Drawing.Size(5, 80)\n\t$TextBox1.Size = New-Object System.Drawing.Size(100, 40)\n\t$label = New-Object Windows.Forms.Label\n\t$label.Location = New-Object Drawing.Point 150, 113\n\t$label.Size = New-Object Drawing.Point 200, 25\n\t$label.text = \"Name\"\n\t$Form1.Controls.Add($label)\t\n\t$script:TextBox2 = New-Object System.Windows.Forms.Textbox\n\t$TextBox2.Location = New-Object System.Drawing.Size(5, 110)\n\t$TextBox2.Size = New-Object System.Drawing.Size(100, 40)\n\t$label = New-Object Windows.Forms.Label\n\t$label.Location = New-Object Drawing.Point 150, 143\n\t$label.Size = New-Object Drawing.Point 200, 25\n\t$label.text = \"Full Name\"\n\t$Form1.Controls.Add($label)\t\n\t$script:TextBox4 = New-Object System.Windows.Forms.Textbox\n\t$TextBox4.Location = New-Object System.Drawing.Size(5, 140)\n\t$TextBox4.Size = New-Object System.Drawing.Size(120, 40)\n\t$script:TextBox3 = New-Object System.Windows.Forms.Textbox\n\t$TextBox3.Location = New-Object System.Drawing.Size(5, 210)\n\t$TextBox3.Size = New-Object System.Drawing.Size(200, 40)\n\t$TextBox3.Visible = $false\n\t$Button1 = New-Object System.Windows.Forms.Button\n\t$Button1.Location = New-Object System.Drawing.Size(95, 20)\n\t$Button1.Size = New-Object System.Drawing.Size(80, 50)\n\t$Button1.Text = \"Create\"\n\t$Button1.Add_Click({ \t\t\t\t\n\t\t\t\tSet-ExecutionPolicy Unrestricted -Scope Process -Force\n\t\t\t\t$Username = \"[email protected]\"\n\t\t\t\t$Password = ConvertTo-SecureString 'Password' -AsPlainText -Force\n\t\t\t\t$LiveCred = New-Object System.Management.Automation.PSCredential $Username, $Password\n $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $LiveCred -Authentication Basic -AllowRedirection\n\t\t\t\tImport-PSSession $Session\n\t\t\t\tConnect-MsolService -Credential $LiveCred\n\t\t\t\tImport-Module MSOnline\n\t\t\t\tGet-MsolUser\n\t\t\t\tNew-MsolUser -UserPrincipalName \"$textbox3\" -FirstName \"$textbox1\" -LastName \"$textbox2\" -DisplayName \"$textbox4\" -UsageLocation CA\n\t\t\t\tSet-MsolUserPassword -UserPrincipalname \"$textbox3\" -NewPassword \"$password1\"\n\t\t\t\t$forcechange = Read-Host \"force a changer Y/N\"\n\t\t\t\tif ($forcechange -eq \"n\")\n\t\t\t\t{\n\t\t\t\t\tSet-MsolUserPassword -UserPrincipalName \"$textbox3\" -NewPassWord \"$nouveaupass\" -ForceChangePassword $false\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tSet-MsolUserPassword -UserPrincipalName \"$textbox3\" -NewPassWord \"$nouveaupass\" -ForceChangePassword $true\n\t\t\t\t}\n\t\t\t\tGet-MsolAccountSku\n\t\t\t\t$LicenseSKU = Get-MsolAccountSku | Out-GridView -Title 'Select a license plan to assign to users' -OutputMode Single | Select-Object -ExpandProperty AccountSkuId\n\t\t\t\t$userdisplayname = Get-MsolUser -All -UnlicensedUsersOnly | Out-GridView -Title 'Select users to assign license plan to' -OutputMode Multiple\n\t\t\t\t$userdisplayname | Set-MsolUser -UsageLocation CA\n\t\t\t\t$userdisplayname | Set-MsolUserLicense -AddLicenses $LicenseSKU\n\t\t\t})\n\t\t\t\t\t$Button2 = New-Object System.Windows.Forms.Button\n\t\t$Button2.Location = New-Object System.Drawing.Size(5, 20)\n\t\t$Button2.Size = New-Object System.Drawing.Size(80, 50)\n\t\t$Button2.Text = \"Creation email\"\n\t\t$Button2.Add_Click({ AddEm })\n\t\t$Button3 = New-Object System.Windows.Forms.Button\n\t\t$Button3.Location = New-Object System.Drawing.Size(185, 20)\n\t\t$Button3.Size = New-Object System.Drawing.Size(80, 50)\n\t\t$Button3.Text = \"Exit\"\n\t\t$Button3.Add_Click({ $Form1.Close() })\n\t\t$Form1.Controls.Add($Button1)\n\t\t$Form1.Controls.Add($Button2)\n\t\t$Form1.Controls.Add($Button3)\n\t\t$Form1.Controls.Add($TextBox1)\n\t\t$Form1.Controls.Add($TextBox2)\n\t\t$Form1.Controls.Add($TextBox3)\n\t\t$Form1.Controls.Add($TextBox4)\n\t\t$Form1.ShowDialog()\n\t\t}\n\t\t\t$Button6 = New-Object System.Windows.Forms.Button\n\t$Button6.Location = New-Object System.Drawing.Size(25, 15)\n\t$Button6.Size = New-Object System.Drawing.Size(110, 23)\n\t$Button6.Text = \"Créé Usager\"\n\t$Button6.Add_Click({ MakeForm -form1 })\n\tAdd-Type -AssemblyName System.Windows.Forms\n\tAdd-Type -AssemblyName System.Drawing\n\t# Build Form\n\t$Form = New-Object System.Windows.Forms.Form\n\t$Form.Text = \"O365 Manager\"\n\t$Form.Size = New-Object System.Drawing.Size(800, 800)\n\t$Form.StartPosition = \"CenterScreen\"\n\t$Form.Topmost = $false\n\t\t$form.Controls.Add($Button6)\n\t$form.Controls.Add($Button7)\n\t#Add Button event \n\t$Button6.Add_Click($Button6)\n\t$Button7.Add_Click($Button7)\n\t$form.ShowDialog()\n<\/code><\/pre>","upvoteCount":6,"answerCount":7,"datePublished":"2019-05-19T20:06:49.000Z","author":{"@type":"Person","name":"martyboy05","url":"https://community.spiceworks.com/u/martyboy05"},"acceptedAnswer":{"@type":"Answer","text":"You need to make $TextBox1.Text either a global or script variable My prefered method of doing this is in the button click<\/p>\n
\t$Button2.Add_Click({ \n\t\t$Global:Text1 = $TextBox1.Text\n\t\t$Global:Text2 = $TextBox2.Text\n\t\tAddEm \n\t})\n<\/code><\/pre>\nThen in the AddEm Function<\/p>\n
Function AddEm\n{\n\t$TextBox4.Text = $Global:Text1 + \" \" + $Global:Text2\n\t$Textbox3.Visible = $true\n\t$Textbox3.Text = $Global:Text1[0] + \"\" + $Global:Text2 + \"@email.com\"\n\t$Textbox3.Visible = $true\n}\n<\/code><\/pre>","upvoteCount":2,"datePublished":"2019-05-20T20:29:23.000Z","url":"https://community.spiceworks.com/t/variable-in-powershell-help/712646/7","author":{"@type":"Person","name":"krisgross","url":"https://community.spiceworks.com/u/krisgross"}},"suggestedAnswer":[{"@type":"Answer","text":"Hi guys.<\/p>\n
I am almost new in PowerShell script and I wrote a script<\/p>\n
but variable won’t work can anyone help me ?<\/p>\n
here is the part of the script not working<\/p>\n
Add-Type -AssemblyName System.Windows.Forms\n\tAdd-Type -AssemblyName System.Drawing\n\tFunction AddEm\n\t{\n\t\t$TextBox4.Text = $TextBox1.Text + \" \" + $TextBox2.Text\n\t\t$Textbox3.Visible = $true\n\t\t$Textbox3.Text = $Textbox1.Text[0] + \"\" + $Textbox2.Text + \"@email.com\"\n\t\t$Textbox3.Visible = $true\n\t}\nFunction MakeForm\n{\n\t$script:Form1 = New-Object system.Windows.Forms.Form\n\t$Form1.Text = \"Add Mail box\"\n\t$Font = New-Object System.Drawing.Font(\"Times New Roman\", 12, [System.Drawing.FontStyle]::Bold)\n\t$Form1.Font = $Font\n\t$label = New-Object Windows.Forms.Label\n\t$label.Location = New-Object Drawing.Point 150, 80\n\t$label.Size = New-Object Drawing.Point 200, 25\n\t$label.text = \"First Name\"\n\t$Form1.Controls.Add($label)\n\t$script:TextBox1 = New-Object System.Windows.Forms.Textbox\n\t$TextBox1.Location = New-Object System.Drawing.Size(5, 80)\n\t$TextBox1.Size = New-Object System.Drawing.Size(100, 40)\n\t$label = New-Object Windows.Forms.Label\n\t$label.Location = New-Object Drawing.Point 150, 113\n\t$label.Size = New-Object Drawing.Point 200, 25\n\t$label.text = \"Name\"\n\t$Form1.Controls.Add($label)\t\n\t$script:TextBox2 = New-Object System.Windows.Forms.Textbox\n\t$TextBox2.Location = New-Object System.Drawing.Size(5, 110)\n\t$TextBox2.Size = New-Object System.Drawing.Size(100, 40)\n\t$label = New-Object Windows.Forms.Label\n\t$label.Location = New-Object Drawing.Point 150, 143\n\t$label.Size = New-Object Drawing.Point 200, 25\n\t$label.text = \"Full Name\"\n\t$Form1.Controls.Add($label)\t\n\t$script:TextBox4 = New-Object System.Windows.Forms.Textbox\n\t$TextBox4.Location = New-Object System.Drawing.Size(5, 140)\n\t$TextBox4.Size = New-Object System.Drawing.Size(120, 40)\n\t$script:TextBox3 = New-Object System.Windows.Forms.Textbox\n\t$TextBox3.Location = New-Object System.Drawing.Size(5, 210)\n\t$TextBox3.Size = New-Object System.Drawing.Size(200, 40)\n\t$TextBox3.Visible = $false\n\t$Button1 = New-Object System.Windows.Forms.Button\n\t$Button1.Location = New-Object System.Drawing.Size(95, 20)\n\t$Button1.Size = New-Object System.Drawing.Size(80, 50)\n\t$Button1.Text = \"Create\"\n\t$Button1.Add_Click({ \t\t\t\t\n\t\t\t\tSet-ExecutionPolicy Unrestricted -Scope Process -Force\n\t\t\t\t$Username = \"[email protected]\"\n\t\t\t\t$Password = ConvertTo-SecureString 'Password' -AsPlainText -Force\n\t\t\t\t$LiveCred = New-Object System.Management.Automation.PSCredential $Username, $Password\n $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $LiveCred -Authentication Basic -AllowRedirection\n\t\t\t\tImport-PSSession $Session\n\t\t\t\tConnect-MsolService -Credential $LiveCred\n\t\t\t\tImport-Module MSOnline\n\t\t\t\tGet-MsolUser\n\t\t\t\tNew-MsolUser -UserPrincipalName \"$textbox3\" -FirstName \"$textbox1\" -LastName \"$textbox2\" -DisplayName \"$textbox4\" -UsageLocation CA\n\t\t\t\tSet-MsolUserPassword -UserPrincipalname \"$textbox3\" -NewPassword \"$password1\"\n\t\t\t\t$forcechange = Read-Host \"force a changer Y/N\"\n\t\t\t\tif ($forcechange -eq \"n\")\n\t\t\t\t{\n\t\t\t\t\tSet-MsolUserPassword -UserPrincipalName \"$textbox3\" -NewPassWord \"$nouveaupass\" -ForceChangePassword $false\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tSet-MsolUserPassword -UserPrincipalName \"$textbox3\" -NewPassWord \"$nouveaupass\" -ForceChangePassword $true\n\t\t\t\t}\n\t\t\t\tGet-MsolAccountSku\n\t\t\t\t$LicenseSKU = Get-MsolAccountSku | Out-GridView -Title 'Select a license plan to assign to users' -OutputMode Single | Select-Object -ExpandProperty AccountSkuId\n\t\t\t\t$userdisplayname = Get-MsolUser -All -UnlicensedUsersOnly | Out-GridView -Title 'Select users to assign license plan to' -OutputMode Multiple\n\t\t\t\t$userdisplayname | Set-MsolUser -UsageLocation CA\n\t\t\t\t$userdisplayname | Set-MsolUserLicense -AddLicenses $LicenseSKU\n\t\t\t})\n\t\t\t\t\t$Button2 = New-Object System.Windows.Forms.Button\n\t\t$Button2.Location = New-Object System.Drawing.Size(5, 20)\n\t\t$Button2.Size = New-Object System.Drawing.Size(80, 50)\n\t\t$Button2.Text = \"Creation email\"\n\t\t$Button2.Add_Click({ AddEm })\n\t\t$Button3 = New-Object System.Windows.Forms.Button\n\t\t$Button3.Location = New-Object System.Drawing.Size(185, 20)\n\t\t$Button3.Size = New-Object System.Drawing.Size(80, 50)\n\t\t$Button3.Text = \"Exit\"\n\t\t$Button3.Add_Click({ $Form1.Close() })\n\t\t$Form1.Controls.Add($Button1)\n\t\t$Form1.Controls.Add($Button2)\n\t\t$Form1.Controls.Add($Button3)\n\t\t$Form1.Controls.Add($TextBox1)\n\t\t$Form1.Controls.Add($TextBox2)\n\t\t$Form1.Controls.Add($TextBox3)\n\t\t$Form1.Controls.Add($TextBox4)\n\t\t$Form1.ShowDialog()\n\t\t}\n\t\t\t$Button6 = New-Object System.Windows.Forms.Button\n\t$Button6.Location = New-Object System.Drawing.Size(25, 15)\n\t$Button6.Size = New-Object System.Drawing.Size(110, 23)\n\t$Button6.Text = \"Créé Usager\"\n\t$Button6.Add_Click({ MakeForm -form1 })\n\tAdd-Type -AssemblyName System.Windows.Forms\n\tAdd-Type -AssemblyName System.Drawing\n\t# Build Form\n\t$Form = New-Object System.Windows.Forms.Form\n\t$Form.Text = \"O365 Manager\"\n\t$Form.Size = New-Object System.Drawing.Size(800, 800)\n\t$Form.StartPosition = \"CenterScreen\"\n\t$Form.Topmost = $false\n\t\t$form.Controls.Add($Button6)\n\t$form.Controls.Add($Button7)\n\t#Add Button event \n\t$Button6.Add_Click($Button6)\n\t$Button7.Add_Click($Button7)\n\t$form.ShowDialog()\n<\/code><\/pre>","upvoteCount":6,"datePublished":"2019-05-19T20:06:49.000Z","url":"https://community.spiceworks.com/t/variable-in-powershell-help/712646/1","author":{"@type":"Person","name":"martyboy05","url":"https://community.spiceworks.com/u/martyboy05"}},{"@type":"Answer","text":"That’s a pretty ambitious script or part of one from someone new to “write”. There are also many variables in it. Which one(s) are going you issues and what is the error<\/p>","upvoteCount":3,"datePublished":"2019-05-20T13:19:27.000Z","url":"https://community.spiceworks.com/t/variable-in-powershell-help/712646/2","author":{"@type":"Person","name":"brad","url":"https://community.spiceworks.com/u/brad"}},{"@type":"Answer","text":"
I am going to guess that your call to your function AddEm is not working in line:<\/p>\n
$Button2.Add_Click({ AddEm })<\/p>\n
I think this is a scoping issue for $TextBox4. In function AddEm, I would guess its creating a new local variable $TextBox4 and assigning it values, rather than the $script:TextBox4 in the MakeForm script.<\/p>","upvoteCount":0,"datePublished":"2019-05-20T15:07:39.000Z","url":"https://community.spiceworks.com/t/variable-in-powershell-help/712646/3","author":{"@type":"Person","name":"johnclarke","url":"https://community.spiceworks.com/u/johnclarke"}},{"@type":"Answer","text":"
\n- \n
Please post this again as powershell code<\/p>\n<\/li>\n
- \n
but variable won’t work? Can you elaborate?<\/p>\n<\/li>\n<\/ol>\n
If you post code, please use the ‘Insert Code’ button. Please and thank you!<\/p>\n