I currently have a script that is adding licenses to a specific user. It uses an If Else statement to add the appropriate license but for some reason the if else statement does not work. it always picks the first license. here is my script:
function button ($title,$FN, $LN, $EM, $PO, $SO, $EI, $LI, $DP) {
###################Load Assembly for creating form & button######
[void][System.Reflection.Assembly]::LoadWithPartialName( “System.Windows.Forms”)
[void][System.Reflection.Assembly]::LoadWithPartialName( “Microsoft.VisualBasic”)
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
#####Define the form size & placement
$form = New-Object “System.Windows.Forms.Form”;
$form.Width = 500;
$form.Height = 400;
$form.Text = $title;
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen;
##############Define firstNameLabel
$firstNameLabel = New-Object “System.Windows.Forms.Label”;
$firstNameLabel.Left = 25;
$firstNameLabel.Top = 15;
$firstNameLabel.Text = $FN;
##############Define text lastNameLabel
$lastNameLabel = New-Object “System.Windows.Forms.Label”;
$lastNameLabel.Left = 25;
$lastNameLabel.Top = 52;
$lastNameLabel.Text = $LN;
##############Define text eMail
$eMail = New-Object “System.Windows.Forms.Label”;
$eMail.Left = 25;
$eMail.Top = 95;
$eMail.Text = $EM;
##############Define text position
$position = New-Object “System.Windows.Forms.Label”;
$position.Left = 25;
$position.Top = 132;
$position.Text = $PO;
##############Define text store
$store = New-Object “System.Windows.Forms.Label”;
$store.Left = 25;
$store.Top = 175;
$store.Text = $SO;
##############Define employeeID
$employeeID = New-Object “System.Windows.Forms.Label”;
$employeeID.Left = 25;
$employeeID.Top = 215;
$employeeID.Text = $EI;
##############Define text License
$License = New-Object “System.Windows.Forms.Label”;
$License.Left = 25;
$License.Top = 295;
$License.Text = $LI;
##############Define text Department
$Department = New-Object “System.Windows.Forms.Label”;
$Department.Left = 25;
$Department.Top = 255;
$Department.Text = $DP;
############Define text firstNameBox for input
$firstNameBox = New-Object “System.Windows.Forms.TextBox”;
$firstNameBox.Left = 150;
$firstNameBox.Top = 10;
$firstNameBox.width = 200;
############Define text lastNameBox for input
$lastNameBox = New-Object “System.Windows.Forms.TextBox”;
$lastNameBox.Left = 150;
$lastNameBox.Top = 50;
$lastNameBox.width = 200;
############Define text Dropdown(Email) for input
[array]$eMailArray = "#yahoo.com", "#gmail.com", "#live.com"
function Return-DropDown {
$Choice = $DropDown.SelectedItem.ToString()
}
$DropDown = new-object System.Windows.Forms.ComboBox
$DropDown.Location = new-object System.Drawing.Size(150,90)
$DropDown.Size = new-object System.Drawing.Size(200,30)
ForEach ($Item in $eMailArray) {
[void] $DropDown.Items.Add($Item)
}
$DropDownLabel = new-object System.Windows.Forms.Label
$DropDownLabel.Location = new-object System.Drawing.Size(10,10)
$DropDownLabel.size = new-object System.Drawing.Size(100,20)
$DropDownLabel.Text = "Items"
############Define text (position) for input
[array]$positionArray = "Assistant Manager", "Assistant Operations Manager", "N/A"
function Return-DropDown1 {
$Choice1 = $DropDown1.SelectedItem.ToString()
}
$DropDown1 = new-object System.Windows.Forms.ComboBox
$DropDown1.Location = new-object System.Drawing.Size(150,130)
$DropDown1.Size = new-object System.Drawing.Size(200,30)
ForEach ($Item in $positionArray) {
[void] $DropDown1.Items.Add($Item)
}
$DropDownLabel1 = new-object System.Windows.Forms.Label
$DropDownLabel1.Location = new-object System.Drawing.Size(10,10)
$DropDownLabel1.size = new-object System.Drawing.Size(100,20)
$DropDownLabel1.Text = "Items"
############Define text (store) for input
[array]$storeArray = "15 Duluth, GA", "16 Indianapolis, IN", "17 Louisville, KY"
function Return-DropDown2 {
$Choice2 = $DropDown2.SelectedItem.ToString()
}
$DropDown2 = new-object System.Windows.Forms.ComboBox
$DropDown2.Location = new-object System.Drawing.Size(150,170)
$DropDown2.Size = new-object System.Drawing.Size(200,30)
ForEach ($Item in $storeArray) {
[void] $DropDown2.Items.Add($Item)
}
$DropDownLabel2 = new-object System.Windows.Forms.Label
$DropDownLabel2.Location = new-object System.Drawing.Size(10,10)
$DropDownLabel2.size = new-object System.Drawing.Size(100,20)
$DropDownLabel2.Text = "Items"
############Define text employeeIDBox for input
$employeeIDBox = New-Object “System.Windows.Forms.TextBox”;
$employeeIDBox.Left = 150;
$employeeIDBox.Top = 215;
$employeeIDBox.width = 200;
############Define text Dropdown(Department) for input
[array]$DepartmentArray = "Accounting", "Admin", "HR" , "IS", "Store Operations"
function Return-DropDown3 {
$Choice3 = $DropDown3.SelectedItem.ToString()
}
$DropDown3 = new-object System.Windows.Forms.ComboBox
$DropDown3.Location = new-object System.Drawing.Size(150,255)
$DropDown3.Size = new-object System.Drawing.Size(200,30)
ForEach ($Item in $DepartmentArray) {
[void] $DropDown3.Items.Add($Item)
}
$DropDownLabel3 = new-object System.Windows.Forms.Label
$DropDownLabel3.Location = new-object System.Drawing.Size(10,10)
$DropDownLabel3.size = new-object System.Drawing.Size(100,20)
$DropDownLabel3.Text = "Items"
############Define text Dropdown(License) for input
[array]$LicenseArray = "K1" , "E4"
function Return-DropDown4 {
$Choice4 = $DropDown4.SelectedItem.ToString()
}
$DropDown4 = new-object System.Windows.Forms.ComboBox
$DropDown4.Location = new-object System.Drawing.Size(150,295)
$DropDown4.Size = new-object System.Drawing.Size(200,30)
ForEach ($Item in $LicenseArray) {
[void] $DropDown4.Items.Add($Item)
}
$DropDownLabel4 = new-object System.Windows.Forms.Label
$DropDownLabel4.Location = new-object System.Drawing.Size(10,10)
$DropDownLabel4.size = new-object System.Drawing.Size(100,20)
$DropDownLabel4.Text = "Items"
#############Define default values for the input boxes
$defaultValue = “”
$firstNameBox.Text = $defaultValue;
$lastNameBox.Text = $defaultValue;
$DropDownLabel.Text = $defaultValue;
$DropDownLabel1.Text = $defaultValue;
$DropDownLabel2.Text = $defaultValue;
$DropDownLabel2.Text = $defaultValue;
$DropDownLabel3.Text = $defaultValue;
$DropDownLabel4.Text = $defaultValue;
$employeeIDBox.Text = $defaultValue;
#############define OK button
$button = New-Object “System.Windows.Forms.Button”;
$button.Left = 360;
$button.Top = 295;
$button.Width = 100;
$button.Text = “Ok”;
############# This is when you have to close the form after getting values
$eventHandler = [System.EventHandler]{
$firstNameBox.Text;
$lastNameBox.Text;
$Choice.Text;
$script:Choice = $DropDown.SelectedItem.ToString()
$script:Choice1 = $DropDown1.SelectedItem.ToString()
$script:Choice2 = $DropDown2.SelectedItem.ToString()
$script:Choice3 = $DropDown3.SelectedItem.ToString()
$script:Choice4 = $DropDown4.SelectedItem.ToString()
$form.Close();};
$button.Add_Click($eventHandler) ;
$button.Add_Click({Return-DropDown})
$button.Add_Click({Return-DropDown1})
$button.Add_Click({Return-DropDown2})
$button.Add_Click({Return-DropDown3})
$button.Add_Click({Return-DropDown4})
#############Add controls to all the above objects defined
$form.Controls.Add($button);
$form.Controls.Add($firstNameLabel);
$form.Controls.Add($lastNameLabel);
$form.Controls.Add($eMail);
$form.Controls.Add($position);
$form.Controls.Add($store);
$form.Controls.Add($employeeID);
$form.Controls.Add($License);
$form.Controls.Add($Department);
$form.Controls.Add($firstNameBox);
$form.Controls.Add($lastNameBox);
$Form.Controls.Add($DropDownLabel);
$Form.Controls.Add($DropDown);
$Form.Controls.Add($DropDownLabel1);
$Form.Controls.Add($DropDown1);
$Form.Controls.Add($DropDownLabel2);
$Form.Controls.Add($DropDown2);
$Form.Controls.Add($employeeIDBox);
$Form.Controls.Add($DropDownLabel3);
$Form.Controls.Add($DropDown3);
$Form.Controls.Add($DropDownLabel4);
$Form.Controls.Add($DropDown4);
$ret = $form.ShowDialog();
#################return values
return $firstNameBox.Text, $lastNameBox.Text, $script:choice, $script:choice1, $script:choice2, $employeeIDBox.Text, $script:choice3, $script:choice4
}
$return= button “Enter Info” “First Name” “Last Name” “Email Address” "Position" "Store" "Employee ID" "License" "Department"
$name = $return[0] + " " + $return[1]
$identAlias = $return[0] + "." + $return[1]
$eAddress = $return[0] + "." + $return[1] + $return[2]
$fillEmployeeID = "Employee ID: " + $return[5]
$store = $return[4]
$storePosition = $return[3]
$e = $store.Substring(0,2)
$ADdescription = "Store " + $e + ' - ' + $storePosition
$f = $store.Substring(3)
$g = $store.Substring(0,3)
$pathname = $e + " - " + $f
New-RemoteMailBox -Alias $identAlias -Name $name -FirstName $return[0] -LastName $return[1] -UserPrincipalName $eAddress -Password (ConvertTo-SecureString -String 'password' -AsPlainText -Force) -ResetPasswordOnNextLogon $true -OnPremisesOrganizationalUnit ("xxx.com/users/" + $return[6])
}
####LOGS INTO 0365 #####
$User = “xxxxxxxxxxxxx”
$Pass = “xxxxxxxxxxxxx”
$Cred = New-Object System.Management.Automation.PsCredential($User,(ConvertTo-SecureString $Pass -AsPlainText -Force))
Import-Module MSOnline
Connect-MsolService -Credential $Cred
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $Cred -Authentication Basic -AllowRedirection
Import-PSSession $Session
###Holds of till DIRSYNC takes Effect ###
do {
sleep -seconds 1
$mailboxExists = get-msoluser -UserPrincipalName $eAddress -ErrorAction SilentlyContinue
write-host "." -nonewline
}
while (!$mailboxExists)
#### ADDS LICENSE TO USER IN 0365####
write-host $return[7]
if($return[7] = "K1"){
Set-MsolUser -UserPrincipalName $eAddress -UsageLocation US
Set-MsolUserLicense -UserPrincipalName $eAddress -AddLicenses Company:DESKLESSPACK_YAMMER
}
else{
Set-MsolUser -UserPrincipalName $eAddress -UsageLocation US
Set-MsolUserLicense -UserPrincipalName $eAddress -AddLicenses Company:ENTERPRISEWITHSCAL
}
Return[7] is either "K1" or "E4"
I have even done a write-host and it spits out E4 but still selects the K1 part of the statement. any ideas?
Related
I want to prevent my Powershell Form from closing after a submit button is pressed. After the function "search_PC" there is no more code and the form closes. I want to still be able to write stuff in there after the function is finished. I have already tried the pause function and while($true) loops. I would be very happy about an answer.
Import-Module ActiveDirectory
# Assembly for GUI
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
# Create Form
$objForm = New-Object System.Windows.Forms.Form
$objForm.Size = New-Object System.Drawing.Size(400,500)
$objForm.Backcolor="white"
$objForm.StartPosition = "CenterScreen"
$objForm.Text = "Example Software"
$objForm.Icon="C:\Users\...\icon.ico"
# Label
$objLabel1 = New-Object System.Windows.Forms.Label
$objLabel1.Location = New-Object System.Drawing.Size(80,30)
$objLabel1.Size = New-Object System.Drawing.Size(60,20)
$objLabel1.Text = "UserID:"
$objForm.Controls.Add($objLabel1)
# Textbox
$objTextBox1 = New-Object System.Windows.Forms.TextBox
$objTextBox1.Location = New-Object System.Drawing.Size(160,28)
$objTextBox1.Size = New-Object System.Drawing.Size(100,20)
$objForm.Controls.Add($objTextBox1)
# Label
$objLabel2 = New-Object System.Windows.Forms.Label
$objLabel2.Location = New-Object System.Drawing.Size(80,72)
$objLabel2.Size = New-Object System.Drawing.Size(80,20)
$objLabel2.Text = "PC-Name:"
$objForm.Controls.Add($objLabel2)
# Textbox
$objTextBox2 = New-Object System.Windows.Forms.TextBox
$objTextBox2.Location = New-Object System.Drawing.Size(160,70)
$objTextBox2.Size = New-Object System.Drawing.Size(100,20)
$objForm.Controls.Add($objTextBox2)
# Button for closing
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(100,220)
$CancelButton.Size = New-Object System.Drawing.Size(163,25)
$CancelButton.Text = "Exit"
$CancelButton.Name = "Exit"
$CancelButton.DialogResult = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)
# Checkbox
$objTypeCheckbox1 = New-Object System.Windows.Forms.Checkbox
$objTypeCheckbox1.Location = New-Object System.Drawing.Size(80,120)
$objTypeCheckbox1.Size = New-Object System.Drawing.Size(500,20)
$objTypeCheckbox1.Text = "Internal Session Support"
$objTypeCheckbox1.TabIndex = 1
$objTypeCheckbox1.Add_Click({$objTypeCheckbox2.Checked = $false})
$objForm.Controls.Add($objTypeCheckbox1)
# Checkbox
$objTypeCheckbox2 = New-Object System.Windows.Forms.Checkbox
$objTypeCheckbox2.Location = New-Object System.Drawing.Size(80,140)
$objTypeCheckbox2.Size = New-Object System.Drawing.Size(500,20)
$objTypeCheckbox2.Text = "Session Support Internal & external"
$objTypeCheckbox2.TabIndex = 2
$objTypeCheckbox2.Add_Click({$objTypeCheckbox1.Checked = $false})
$objForm.Controls.Add($objTypeCheckbox2)
# Button fo checking the User Input
$SubmitButton = New-Object System.Windows.Forms.Button
$SubmitButton.Location = New-Object System.Drawing.Size(100,190)
$SubmitButton.Size = New-Object System.Drawing.Size(163,25)
$SubmitButton.Text = "Submit"
$SubmitButton.Name = "Submit"
$SubmitButton.DialogResult = "OK"
$objForm.AcceptButton = $SubmitButton
$SubmitButton.Add_Click({
$User = $objTextBox1.Text
$PC = $objTextBox2.Text
# Check if all User Inputs are filled out
if ( !($User.trim()) -or !($PC.trim()) ) {
[void] [Windows.Forms.MessageBox]::Show("Please fill out every value!", "Error", [Windows.Forms.MessageBoxButtons]::ok, [Windows.Forms.MessageBoxIcon]::Warning)}
if($objTypeCheckbox1.checked -eq $true) {$Software = "Internal"}
elseif($objTypeCheckbox2.checked -eq $true) {$Software = "Internal&External"}
else {[void] [Windows.Forms.MessageBox]::Show("Please fill out every value!", "Error", [Windows.Forms.MessageBoxButtons]::ok, [Windows.Forms.MessageBoxIcon]::Warning)}
search_user
})
$objForm.Controls.Add($SubmitButton)
$statusBar1 = New-Object System.Windows.Forms.StatusBar
$objForm.controls.add($statusBar1)
function createFile
{
$Date = Get-Date -Format d.M.yyyy
$Time = (Get-Date).ToString(„HH:mm:ss“)
$Time2 = (Get-Date).ToString(„HH-mm-ss“)
$Date2 = Get-Date -Format yyyy-M-d;
$FileName = $Time2 + " " + $Date2
$wrapper = New-Object PSObject -Property #{ 1 = $Date; 2 = $Time; 3 = $User; 4 = $PC; 5 = $Software }
Export-Csv -InputObject $wrapper -Path C:\Users\...\$FileName.csv -NoTypeInformation
[void] [Windows.Forms.MessageBox]::Show("Successful!")
}
function search_user
{
if (#(Get-ADUser -Filter {SamAccountName -eq $User}).Count -eq 0) {
$objTextBox1.BackColor = "Red";
$statusBar1.Text = "Couldn't find user!"
$objForm.Dispose()
}
else{$objTextBox1.BackColor = "Green"
search_PC}
}
function search_PC
{
$Client = $PC
if (#(Get-ADComputer -Filter {DNSHostName -eq $Client}).Count -eq 0) {
$objTextBox2.BackColor = "Red";
$statusBar1.Text = "Couldn't find PC!"
$objForm.Dispose()
}
else{$objTextBox2.BackColor = "Green"
createFile}
}
$objForm.ShowDialog() #Showing Form and elements
Screenshot of the form:
This line is your problem:
$SubmitButton.DialogResult = "OK"
It should be:
$SubmitButton.DialogResult = [System.Windows.Forms.DialogResult]::None
Or just be removed.
DialogResult
I have a GUI created in powershell, which contains some checkboxes. Later in the script I use the values from the checkboxes to set parameters of a user account, but it seems the script doesn't take the values as intended. I mean, sometimes the account is created with correct values, sometimes not. I wasn't able to discover any pattern when it works and when not.
$Orig_exec_policy = Get-ExecutionPolicy
Set-ExecutionPolicy Bypass -Force
<# This form was created using POSHGUI.com a free online gui designer for PowerShell
.NAME
Untitled
#>
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
#region Window properties
$AD_user_creation = New-Object system.Windows.Forms.Form
$AD_user_creation.ClientSize = '480,740'
$AD_user_creation.text = "AD user creation - WG Mustang"
$AD_user_creation.TopMost = $false
#endregion
[void]$AD_user_creation.SuspendLayout()
#region Real name of the user
$Display_name_lbl = New-Object system.Windows.Forms.Label
$Display_name_lbl.text = "User`'s real name"
$Display_name_lbl.AutoSize = $true
$Display_name_lbl.width = 25
$Display_name_lbl.height = 10
$Display_name_lbl.location = New-Object System.Drawing.Point(10,10)
$First_name_val = New-Object system.Windows.Forms.TextBox
$First_name_val.multiline = $false
$First_name_val.width = 120
$First_name_val.height = 20
$First_name_val.location = New-Object System.Drawing.Point(200,10)
$Second_name_val = New-Object system.Windows.Forms.TextBox
$Second_name_val.multiline = $false
$Second_name_val.width = 120
$Second_name_val.height = 20
$Second_name_val.location = New-Object System.Drawing.Point(330,10)
$Display_name_val = New-Object system.Windows.Forms.Label
$Display_name_val.Text = ""
$Display_name_val.width = 250
$Display_name_val.height = 20
$Display_name_val.location = New-Object System.Drawing.Point(200,40)
#endregion
#region User name of the user
$User_name_lbl = New-Object system.Windows.Forms.Label
$User_name_lbl.text = "User logon name"
$User_name_lbl.AutoSize = $true
$User_name_lbl.width = 25
$User_name_lbl.height = 10
$User_name_lbl.location = New-Object System.Drawing.Point(10,70)
$User_name_val = New-Object system.Windows.Forms.TextBox
$User_name_val.multiline = $false
$User_name_val.width = 250
$User_name_val.height = 20
$User_name_val.location = New-Object System.Drawing.Point(200,70)
#endregion
#region Account password
$Password_lbl = New-Object system.Windows.Forms.Label
$Password_lbl.text = "Password"
$Password_lbl.AutoSize = $true
$Password_lbl.width = 25
$Password_lbl.height = 10
$Password_lbl.location = New-Object System.Drawing.Point(10,100)
$Password_ini_val = New-Object system.Windows.Forms.MaskedTextBox
$Password_ini_val.multiline = $false
$Password_ini_val.width = 250
$Password_ini_val.height = 20
$Password_ini_val.UseSystemPasswordChar = $true
$Password_ini_val.location = New-Object System.Drawing.Point(200,100)
$Password_conf_val = New-Object system.Windows.Forms.MaskedTextBox
$Password_conf_val.multiline = $false
$Password_conf_val.width = 250
$Password_conf_val.height = 20
$Password_conf_val.UseSystemPasswordChar = $true
$Password_conf_val.location = New-Object System.Drawing.Point(200,130)
#endregion
#region Location of the user
$Location_lbl = New-Object system.Windows.Forms.Label
$Location_lbl.text = "Location"
$Location_lbl.AutoSize = $true
$Location_lbl.width = 25
$Location_lbl.height = 10
$Location_lbl.location = New-Object System.Drawing.Point(10,160)
$Location_val = New-Object system.Windows.Forms.TextBox
$Location_val.multiline = $false
$Location_val.text = "OU=Users,OU=ADM,DC=Mustangeng,DC=com"
$Location_val.width = 250
$Location_val.height = 20
$Location_val.location = New-Object System.Drawing.Point(200,160)
#endregion
#region Checkboxes
$Must_chg_pass = New-Object system.Windows.Forms.CheckBox
$Must_chg_pass.text = "User must change password at next logon"
$Must_chg_pass.AutoSize = $false
$Must_chg_pass.width = 290
$Must_chg_pass.height = 20
$Must_chg_pass.location = New-Object System.Drawing.Point(200,190)
$Cannot_chg_pass = New-Object system.Windows.Forms.CheckBox
$Cannot_chg_pass.text = "User cannot change password"
$Cannot_chg_pass.AutoSize = $false
$Cannot_chg_pass.width = 250
$Cannot_chg_pass.height = 20
$Cannot_chg_pass.location = New-Object System.Drawing.Point(200,220)
$Cannot_chg_pass.Checked = $true
$Pass_not_expires = New-Object system.Windows.Forms.CheckBox
$Pass_not_expires.text = "Password never expires"
$Pass_not_expires.AutoSize = $false
$Pass_not_expires.width = 250
$Pass_not_expires.height = 20
$Pass_not_expires.location = New-Object System.Drawing.Point(200,250)
$Pass_not_expires.Checked = $true
$Account_disabled_val = New-Object system.Windows.Forms.CheckBox
$Account_disabled_val.text = "Account is active"
$Account_disabled_val.AutoSize = $false
$Account_disabled_val.width = 250
$Account_disabled_val.height = 20
$Account_disabled_val.location = New-Object System.Drawing.Point(200,280)
$Account_disabled_val.Checked = $false
#endregion
#region Description
$GECOS_lbl = New-Object system.Windows.Forms.Label
$GECOS_lbl.text = "Description"
$GECOS_lbl.AutoSize = $true
$GECOS_lbl.width = 25
$GECOS_lbl.height = 10
$GECOS_lbl.location = New-Object System.Drawing.Point(10,310)
$GECOS_val = New-Object system.Windows.Forms.TextBox
$GECOS_val.multiline = $false
$GECOS_val.width = 250
$GECOS_val.height = 20
$GECOS_val.location = New-Object System.Drawing.Point(200,310)
#endregion
#region Group membership
$ADGroups_lbl = New-Object system.Windows.Forms.Label
$ADGroups_lbl.text = "AD Groups"
$ADGroups_lbl.AutoSize = $true
$ADGroups_lbl.width = 25
$ADGroups_lbl.height = 10
$ADGroups_lbl.location = New-Object System.Drawing.Point(10,340)
$ADGroups_val = New-Object system.Windows.Forms.TextBox
$ADGroups_val.multiline = $true
$ADGroups_val.width = 250
$ADGroups_val.height = 160
$ADGroups_val.location = New-Object System.Drawing.Point(200,340)
#endregion
#region Additional attributes
$Ext_Attribute5_lbl = New-Object System.Windows.Forms.Label
$Ext_Attribute5_lbl.Text = "Extension Attribute5"
$Ext_Attribute5_lbl.AutoSize = $true
$Ext_Attribute5_lbl.Width = 25
$Ext_Attribute5_lbl.Height = 10
$Ext_Attribute5_lbl.Location = New-Object System.Drawing.Point(10,510)
$Ext_Attribute5_val = New-Object System.Windows.Forms.TextBox
$Ext_Attribute5_val.Text = "Company name"
$Ext_Attribute5_val.Multiline = $false
$Ext_Attribute5_val.Width = 250
$Ext_Attribute5_val.Height = 20
$Ext_Attribute5_val.Location = New-Object System.Drawing.Point(200,510)
$Ext_Attribute10_lbl = New-Object System.Windows.Forms.Label
$Ext_Attribute10_lbl.Text = "Extension Attribute10"
$Ext_Attribute10_lbl.AutoSize = $true
$Ext_Attribute10_lbl.Width = 25
$Ext_Attribute10_lbl.Height = 10
$Ext_Attribute10_lbl.Location = New-Object System.Drawing.Point(10,540)
$Ext_Attribute10_val = New-Object System.Windows.Forms.TextBox
$Ext_Attribute10_val.Text = "Region"
$Ext_Attribute10_val.Multiline = $false
$Ext_Attribute10_val.Width = 250
$Ext_Attribute10_val.Height = 20
$Ext_Attribute10_val.Location = New-Object System.Drawing.Point(200,540)
$Ext_Attribute15_lbl = New-Object System.Windows.Forms.Label
$Ext_Attribute15_lbl.Text = "Extension Attribute15"
$Ext_Attribute15_lbl.AutoSize = $true
$Ext_Attribute15_lbl.Width = 25
$Ext_Attribute15_lbl.Height = 10
$Ext_Attribute15_lbl.Location = New-Object System.Drawing.Point(10,570)
$Ext_Attribute15_val = New-Object System.Windows.Forms.TextBox
$Ext_Attribute15_val.Text = "EH/WH"
$Ext_Attribute15_val.Multiline = $false
$Ext_Attribute15_val.Width = 250
$Ext_Attribute15_val.Height = 20
$Ext_Attribute15_val.Location = New-Object System.Drawing.Point(200,570)
$Job_Title_lbl = New-Object System.Windows.Forms.Label
$Job_Title_lbl.Text = "Job title"
$Job_Title_lbl.AutoSize = $true
$Job_Title_lbl.Width = 25
$Job_Title_lbl.Height = 10
$Job_Title_lbl.Location = New-Object System.Drawing.Point(10,600)
$Job_Title_val = New-Object System.Windows.Forms.TextBox
$Job_Title_val.Text = "NA"
$Job_Title_val.Multiline = $false
$Job_Title_val.Width = 250
$Job_Title_val.Height = 20
$Job_Title_val.Location = New-Object System.Drawing.Point(200,600)
$Department_lbl = New-Object System.Windows.Forms.Label
$Department_lbl.Text = "Department"
$Department_lbl.AutoSize = $true
$Department_lbl.Width = 25
$Department_lbl.Height = 10
$Department_lbl.Location = New-Object System.Drawing.Point(10,630)
$Department_val = New-Object System.Windows.Forms.TextBox
$Department_val.Text = "NA"
$Department_val.Multiline = $false
$Department_val.Width = 250
$Department_val.Height = 20
$Department_val.Location = New-Object System.Drawing.Point(200,630)
$Company_lbl = New-Object System.Windows.Forms.Label
$Company_lbl.Text = "Company"
$Company_lbl.AutoSize = $true
$Company_lbl.Width = 25
$Company_lbl.Height = 10
$Company_lbl.Location = New-Object System.Drawing.Point(10,660)
$Company_val = New-Object System.Windows.Forms.TextBox
$Company_val.Text = "IBM"
$Company_val.Multiline = $false
$Company_val.Width = 250
$Company_val.Height = 20
$Company_val.Location = New-Object System.Drawing.Point(200,660)
#endregion
#region Buttons
$Confirm_Button = New-Object system.Windows.Forms.Button
$Confirm_Button.BackColor = "#00ff00"
$Confirm_Button.text = "OK"
$Confirm_Button.width = 100
$Confirm_Button.height = 30
$Confirm_Button.location = New-Object System.Drawing.Point(200,690)
$Confirm_Button.Font = 'Microsoft Sans Serif,10,style=Bold'
$Create_ADuser = {
if ($Password_ini_val.Text -cne $Password_conf_val.Text)
{
[System.Windows.MessageBox]::Show("Passwords don't match")
} elseif ($Password_ini_val.Text.Length -lt 8)
{
[System.Windows.MessageBox]::Show("Password is too short")
} else {
$password = $Password_ini_val.Text | ConvertTo-SecureString -AsPlainText -Force
$Display_name = $Display_name_val.Text + " [ADM]"
New-ADUser -GivenName $First_name_val.Text -Surname $Second_name_val.Text -DisplayName $Display_name -AccountPassword $password -Path $Location_val.Text -Name $User_name_val.Text`
-CannotChangePassword $Cannot_chg_pass.Checked -PasswordNeverExpires $Pass_not_expires.Checked -ChangePasswordAtLogon $Must_chg_pass.Checked -Enabled $Account_disabled_val.Checked`
-Description $GECOS_val.Text -OtherAttributes #{'ExtensionAttribute5' = $Ext_Attribute5_val.Text;'ExtensionAttribute9' = "People";'ExtensionAttribute10' = $Ext_Attribute10_val.Text;`
'ExtensionAttribute11' = "Other";'ExtensionAttribute12' = "No";'ExtensionAttribute14' = "NA";'ExtensionAttribute15' = $Ext_Attribute15_val.Text;'Division' = "WG Mustang"}`
-Office "NA" -OfficePhone "NA" -Title $Job_Title_val.Text -Department $Department_val.Text -Company $Company_val.Text -SamAccountName $User_name_val.Text -PassThru | `
Add-ADPrincipalGroupMembership -MemberOf $ADGroups_val.Text
$AD_user_creation.Close()
}
}
$Confirm_Button.add_Click($Create_ADuser)
$Cancel_button = New-Object system.Windows.Forms.Button
$Cancel_button.BackColor = "#ff0000"
$Cancel_button.text = "Cancel"
$Cancel_button.width = 100
$Cancel_button.height = 30
$Cancel_button.location = New-Object System.Drawing.Point(350,690)
$Cancel_button.Font = 'Microsoft Sans Serif,10,style=Bold'
<#$Cancel = {
$AD_user_creation.Close()
exit
}#>
$Cancel_button.add_Click({
$AD_user_creation.Close()
exit
})
$AD_user_creation.AcceptButton = $Confirm_Button
$AD_user_creation.CancelButton = $Cancel_button
#endregion
$AD_user_creation.controls.AddRange(#($Display_name_lbl,$First_name_val,$Second_name_val,$User_name_lbl,$Display_name_val,$User_name_val,$Password_lbl,$Password_ini_val,$Password_conf_val,$Location_lbl,`
$Location_val,$Must_chg_pass,$Cannot_chg_pass,$Pass_not_expires,$Account_disabled_val,$GECOS_lbl,$GECOS_val,$ADGroups_lbl,$ADGroups_val,$Ext_Attribute5_lbl,$Ext_Attribute5_val,$Ext_Attribute10_lbl,`
$Ext_Attribute10_val,$Ext_Attribute15_lbl,$Ext_Attribute15_val,$Job_Title_lbl,$Job_Title_val,$Department_lbl,$Department_val,$Company_lbl,$Company_val,$Confirm_Button,$Cancel_button))
$showFullName = { $Display_name_val.Text = ($First_name_val.Text + " " + $Second_name_val.Text) }
[void]$Second_name_val.Add_Leave( { & $showFullName } )
[void]$First_name_val.Add_Leave( { & $showFullName } )
[void]$AD_user_creation.ResumeLayout()
$result = $AD_user_creation.ShowDialog()
[void]$AD_user_creation.Dispose()
Set-ExecutionPolicy $Orig_exec_policy -Force
Definitions of checkboxes start at the row 101 and user gets created at the rows 268-272.
The user sometimes gets created as active, sometimes as disabled. And once I notice that even the "user cannot change password" doesn't get checked in the account, even though the value in the form is set to True. I didn't notice any problems with the other checkboxes. Any idea what's happening?
I believe the error stems from the way you use the New-User cmdlet. That piece of code looks complicated enough to not see that there is at least one backtick in the wrong place.
Better use Splatting for this:
$params = #{
'GivenName' = $First_name_val.Text
'Surname' = $Second_name_val.Text
'DisplayName' = $Display_name
'AccountPassword' = $password
'Path' = $Location_val.Text
'Name' = $User_name_val.Text
'CannotChangePassword' = $Cannot_chg_pass.Checked
'PasswordNeverExpires' = $Pass_not_expires.Checked
'ChangePasswordAtLogon' = $Must_chg_pass.Checked
'Enabled' = $Account_disabled_val.Checked
'Description' = $GECOS_val.Text
'Office' = "NA"
'OfficePhone' = "NA"
'Title' = $Job_Title_val.Text
'Department' = $Department_val.Text
'Company' = $Company_val.Text
'SamAccountName' = $User_name_val.Text
'OtherAttributes' = #{'ExtensionAttribute5' = $Ext_Attribute5_val.Text
'ExtensionAttribute9' = "People"
'ExtensionAttribute10' = $Ext_Attribute10_val.Text
'ExtensionAttribute11' = "Other"
'ExtensionAttribute12' = "No"
'ExtensionAttribute14' = "NA"
'ExtensionAttribute15' = $Ext_Attribute15_val.Text
'Division' = "WG Mustang"}
'PassThru' = $true
}
$newUser = New-ADUser #params
if ($newUser) {
$newUser | Add-ADPrincipalGroupMembership -MemberOf $ADGroups_val.Text
}
else {
[System.Windows.MessageBox]::Show("Error creating new user")
}
So I create an app for my company when I went yesterday all work correctly and now I see my script block by the Anti-Virus and when I use my personal PC for work he passe my loop for
Get-ChildItem -Path $folderpath -Include $filetype -recurse |
ForEach-Object `
{
$Result ++
}
So this is a part of my code and this is bypassed
And the other loop it's for create an .xls file wher I add information from other .xls
Sorry for my English I'm Belgian
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
#region begin GUI{
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '400,400'
$Form.text = "Fichier Provision CP"
$Form.TopMost = $false
$Icon = New-Object system.drawing.icon ("C:\Users\KILLIAN\Desktop\SAMPLE\ico.ico")
$Form.Icon = $Icon
$ProgressBar1 = New-Object system.Windows.Forms.ProgressBar
$ProgressBar1.width = 230
$ProgressBar1.height = 10
$ProgressBar1.location = New-Object System.Drawing.Point(80,360)
$Label1 = New-Object system.Windows.Forms.Label
$Label1.text = ""
$Label1.AutoSize = $true
$Label1.width = 80
$Label1.height = 10
$Label1.location = New-Object System.Drawing.Point(230,330)
$Label1.Font = 'Microsoft Sans Serif,10'
$Button1 = New-Object system.Windows.Forms.Button
$Button1.text = "Entrer"
$Button1.width = 90
$Button1.height = 29
$Button1.location = New-Object System.Drawing.Point(151,282)
$Button1.Font = 'Microsoft Sans Serif,10'
$Label2 = New-Object system.Windows.Forms.Label
$Label2.text = "Mois de fin"
$Label2.AutoSize = $true
$Label2.width = 25
$Label2.height = 10
$Label2.location = New-Object System.Drawing.Point(15,100)
$Label2.Font = 'Microsoft Sans Serif,10'
$Label3 = New-Object system.Windows.Forms.Label
$Label3.text = "Nom du fichier"
$Label3.AutoSize = $true
$Label3.width = 25
$Label3.height = 10
$Label3.location = New-Object System.Drawing.Point(15,60)
$Label3.Font = 'Microsoft Sans Serif,10'
$Label4 = New-Object system.Windows.Forms.Label
$Label4.text = "Jour férié"
$Label4.AutoSize = $true
$Label4.width = 25
$Label4.height = 10
$Label4.location = New-Object System.Drawing.Point(15,140)
$Label4.Font = 'Microsoft Sans Serif,10'
$TextBox2 = New-Object system.Windows.Forms.TextBox
$TextBox2.multiline = $false
$TextBox2.width = 200
$TextBox2.height = 20
$TextBox2.location = New-Object System.Drawing.Point(145,100)
$TextBox2.Font = 'Microsoft Sans Serif,10'
$TextBox1 = New-Object system.Windows.Forms.TextBox
$TextBox1.multiline = $false
$TextBox1.width = 200
$TextBox1.height = 20
$TextBox1.location = New-Object System.Drawing.Point(145,60)
$TextBox1.Font = 'Microsoft Sans Serif,10'
$TextBox3 = New-Object system.Windows.Forms.TextBox
$TextBox3.multiline = $false
$TextBox3.width = 200
$TextBox3.height = 20
$TextBox3.location = New-Object System.Drawing.Point(145,140)
$TextBox3.Font = 'Microsoft Sans Serif,10'
$Form.controls.AddRange(#($ProgressBar1,$Label1,$Button1,$Label2,$Label3,$TextBox1,$TextBox2,$Label4,$TextBox3))
#Var of the code{
#C:\Users\student\Desktop\SAMPLE\*\Congé
#G:\LUXDIVISION\SDE\COLLABORATEURS\*\Congé
$folderpath = "C:\Users\KILLIAN\Desktop\SAMPLE\test\*\Congé"
$Exl = New-Object -ComObject "Excel.Application"
$Exl.Visible = $false
$Exl.DisplayAlerts = $false
$objExcel = new-object -comobject excel.application
$objExcel.Visible = $false
$filetype ="*xls"
$Result = 0
$Counter = 0
$xlContinuous = 1
$xlThick = 4
$xlInsideVertical = 11
$xlEdgeBottom = 9
$a = 1
#Calling function}
function monthnamev (){
$boolean = $false
if($month -eq 3){
$boolean = $True
}
elseif($month -eq 6){
$boolean = $true
}
elseif($month -eq 9){
$boolean = $true
}
elseif($month -eq 12){
$boolean = $true
}
return $boolean
}
function monthname (){
$boolean = $false
if($month -eq 3){
$namemonth = "Mars"
}
elseif($month -eq 6){
$namemonth = "Juin"
}
elseif($month -eq 9){
$namemonth = "Septembre"
}
elseif($month -eq 12){
$namemonth = "Décembre"
}
return $namemonth
}
function isNumeric ($x) {
try {
0 + $x | Out-Null
return $true
} catch {
return $false
}
}
#Write your logic code here
$Form.Add_Shown({
$Year = Get-Date -UFormat "%Y"
$date = Get-Date -UFormat "%m %Y"
$TextBox1.Text = "Provision CP FIN $date"
})
Get-ChildItem -Path $folderpath -Include $filetype -recurse |
ForEach-Object `
{
$Result ++
}
$Button1.Add_Click({
#Var for the textbox
$namews = $TextBox1.Text
$month = $TextBox2.Text
$pholiday = $TextBox3.Text
#Public holiday
if (isNumeric($pholiday) -eq $true){
monthname
if (-not (monthnamev)){
[System.Windows.MessageBox]::Show('Erreur vous avez entrer une valeur incorrecte!', 'Error !', 0, 16)
}
else{
#Create a new .xls
$objWorkbook = $objExcel.Workbooks.Add()
$objWorksheet = $objWorkbook.Worksheets.Item(1)
Get-ChildItem -Path $folderpath -Include $filetype -recurse |
ForEach-Object `
{
## -- Calculate The Percentage Completed
$Counter++
[Int]$Percentage = ($Counter/$Result)*100
$ProgressBar1.Value = $Percentage
$Form.Refresh()
$Label1.Text = "Encodage : $Counter/$Result"
#Open the Collab holiday .xls
$workbook = $Exl.workbooks.open($_.fullname)
#Copy the name of the employee
$workbook.sheets.item(1).activate()
$WorkbookTotal=$workbook.Worksheets.item(1)
$value = $WorkbookTotal.Cells.Item(2, 2)
$value.Text
$objWorksheet.Activate()
$objWorksheet.Cells.Item($a, 1) = $value
Write-Warning $value.text
#Search Solde of previous year
$lastsheet = $workbook.Worksheets|Select -Last 1
$lastsheet.Activate()
$FoundAS = $lastsheet.Cells.Find('Solde')
$RowAS =$FoundAS.Row
$ColumnAS = $FoundAS.Column
[int]$RowAS2 = $RowAS
[int]$ColumnAS2 = $ColumnAS
$ColumnAS2 ++
$value = $lastsheet.cells.item($RowAS2,$ColumnAS2)
if (isNumeric($value) -eq $true){
$objWorksheet.Activate()
$objWorksheet.Cells.Item($a, 2) = $value
}
else{
$value = 0
$objWorksheet.Activate()
$objWorksheet.Cells.Item($a, 2) = $value
}
#Search Holyday
$Found = $lastsheet.Cells.Find('Congés $Year')
$Row =$Found.Row
$Column = $Found.Column
[int]$Row2 = $Row
[int]$Column2 = $Column
$Column2 ++
#Search Total
$Foundto = $lastsheet.Cells.Find('Total')
$Rowto =$Foundto.Row
$Columnto = $Foundto.Column
[int]$Rowto2 = $Rowto
[int]$Columnto2 = $Columnto
$Columnto2 ++
#Search month
$FoundSolde = $lastsheet.Cells.Find((monthname))
$RowSolde =$FoundSolde.Row
$ColumnSolde = $FoundSolde.Column
[int]$RowSolde2 = $RowSolde
[int]$ColumnSolde2 = $ColumnSolde
$ColumnSolde2 = $ColumnSolde2 + 5
$Soldemois = $lastsheet.cells.item($RowSolde2,$ColumnSolde2)
[double]$Soldemois2 = $Soldemois.text
if (isNumeric($Soldemois2) -eq $true){
$objWorksheet.Activate()
}
else{
[double]$Soldemois2 = 0
$objWorksheet.Activate()
}
#Search the correct value
$SoldePre = $lastsheet.cells.item($RowAS2,$ColumnAS2)
$total = $lastsheet.cells.item($Rowto2,$Columnto2)
$Holiday = $lastsheet.cells.item($Row2,$Column2)
[double]$total2 = $total.text
[double]$SoldePre2 = $SoldePre.text
[double]$Holiday2 = $Holiday.text
Write-Warning "Solde Précedent : $SoldePre2"
Write-Warning "Solde du fin de mois : $Soldemois2"
Write-Warning "le total de congé : $total2"
Write-Warning ""
$objWorksheet.Activate()
$objWorksheet.Cells.Item($a, 2) = $SoldePre2
$objWorksheet.Cells.Item($a, 3) = "=($Holiday2/12) * $month"
$objWorksheet.Cells.Item($a, 4) = "=$total2 - $Soldemois2"
$cell2 = $objWorksheet.Cells.Item($a, 3)
$cell3 = $objWorksheet.Cells.Item($a, 4)
[double]$celld2 = $cell2.text
[double]$celld3 = $cell3.text
$objWorksheet.Cells.Item($a, 5) = "=($SoldePre2 + $celld2 - $celld3) + $pholiday"
$workbook.save()
$workbook.close()
$a ++
}
$objWorkbook.saveas("C:\Users\KILLIAN\Desktop\SAMPLE\$namews.xls")
$objWorkbook.close()
$objExcel.Quit()
$objExcel = $null
$Exl.Quit()
$Exl = $null
}
}
else{
[System.Windows.MessageBox]::Show('Le Jour Férié doit être un chiffre !!!', 'Jour Férié incorrect', 0, 16)
}
})
$Form.ShowDialog()
It's all of my code
Here is when I launch the debbuger
And this is when I execute the second step (I tap F10)
When I launch your line
And the line on the cmd
I have a Powershell scripts that creates 12 unique check boxes. Later in the script, it creates an email and enter information. I am trying to get the email to include the value of the boxes that are checked, and not the others. The variable name for each box is $CB1 up through $CB12.
Team,
Per ticket $SCTicket, $User is requesting access to GetPaid.
Details from RITM ticket $RITMTicket are below:
Access Type: $AccessType
Roll: $Roll
Responsibilities:
Thank You
Doing something such as $CB1,$CB2,$CB3, ect. does not look right, as it leaves a bunch of random commas. Putting each checkbox value on it's own line can work, but then there are potentially several blank lines for no reason.
If the user checks off, say, boxes 4, 7, and 10, how can I get just those values written??
Full code is below:
if ($startupvariables) { try {Remove-Variable -Name startupvariables -Scope Global -ErrorAction SilentlyContinue } catch { } }
New-Variable -force -name startupVariables -value ( Get-Variable | ForEach-Object { $_.Name } )
Write-Output "Don't close this window!"
Add-Type -AssemblyName PresentationCore,PresentationFramework
$ButtonType = [System.Windows.MessageBoxButton]::OKCancel
$MessageIcon = [System.Windows.MessageBoxImage]::Warning
$MessageTitle = "GetPaid - Access Request"
$MessageBody = "This script sends a GetPaid access request setup to xxxxxxxx and xxxxxx.`n`nTo use it, enter the below information:`n`n`n`tTicket Number`n`n`tUser's Email Address`n`n`tAccess Requested`n`n`nIf this is the script you want to use, click OK.`nIf not, click Cancel."
$Result = [System.Windows.MessageBox]::Show($MessageBody,$MessageTitle,$ButtonType,$MessageIcon)
if ($Result -eq "Cancel")
{
Exit-PSSession
}
else
{
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
$Separator = ".", "#"
$SCTicket = [Microsoft.VisualBasic.Interaction]::InputBox("Enter the SCTask ticket number" , "Ticket Number")
$RITMTicket = [Microsoft.VisualBasic.Interaction]::InputBox("Enter the RITM ticket number" , "Ticket Number")
$UserID = [Microsoft.VisualBasic.Interaction]::InputBox("Enter the user's email address" , "User Email Address")
$User = $UserID.split($Separator)
$Firstname = $User[0].substring(0,1).toupper()+$User[0].substring(1).tolower()
$Lastname = $User[1].substring(0,1).toupper()+$User[1].substring(1).tolower()
$User = $Firstname, $Lastname
function Access_Type{
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$Form = New-Object System.Windows.Forms.Form
$Form.width = 225
$Form.height = 215
$Form.Text = ”Select Type of Access"
$Font = New-Object System.Drawing.Font("Verdana",11)
$Form.Font = $Font
$MyGroupBox = New-Object System.Windows.Forms.GroupBox
$MyGroupBox.Location = '5,5'
$MyGroupBox.size = '190,125'
$RadioButton1 = New-Object System.Windows.Forms.RadioButton
$RadioButton1.Location = '20,20'
$RadioButton1.size = '120,30'
$RadioButton1.Checked = $false
$RadioButton1.Text = "Grant"
$RB1 = "Grant"
$RadioButton2 = New-Object System.Windows.Forms.RadioButton
$RadioButton2.Location = '20,50'
$RadioButton2.size = '120,30'
$RadioButton2.Checked = $false
$RadioButton2.Text = "Change"
$RB2 = "Change"
$RadioButton3 = New-Object System.Windows.Forms.RadioButton
$RadioButton3.Location = '20,80'
$RadioButton3.size = '120,30'
$RadioButton3.Checked = $false
$RadioButton3.Text = "Revoke"
$RB3 = "Revoke"
$OKButton = new-object System.Windows.Forms.Button
$OKButton.Location = '5,135'
$OKButton.Size = '90,35'
$OKButton.Text = 'OK'
$OKButton.DialogResult=[System.Windows.Forms.DialogResult]::OK
$CancelButton = new-object System.Windows.Forms.Button
$CancelButton.Location = '105,135'
$CancelButton.Size = '90,35'
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$CancelButton.DialogResult=[System.Windows.Forms.DialogResult]::Cancel
$form.Controls.AddRange(#($MyGroupBox,$OKButton,$CancelButton))
$MyGroupBox.Controls.AddRange(#($Radiobutton1,$RadioButton2,$RadioButton3))
$form.AcceptButton = $OKButton
$form.CancelButton = $CancelButton
$form.Add_Shown({$form.Activate()})
$dialogResult = $form.ShowDialog()
if ($DialogResult -eq "OK")
{
if ($RadioButton1.Checked){$global:AccessType = $RB1}
elseif ($RadioButton2.Checked){$global:AccessType = $RB2}
elseif ($RadioButton3.Checked){$global:AccessType = $RB3}
}
elseif ($DialogResult -eq "Cancel")
{
break
}
}
Access_Type
function Access_Roll{
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$Form = New-Object System.Windows.Forms.Form
$Form.width = 225
$Form.height = 180
$Form.Text = ”Select Type of Access"
$Font = New-Object System.Drawing.Font("Verdana",11)
$Form.Font = $Font
$MyGroupBox = New-Object System.Windows.Forms.GroupBox
$MyGroupBox.Location = '5,5'
$MyGroupBox.size = '190,90'
$RadioButton1 = New-Object System.Windows.Forms.RadioButton
$RadioButton1.Location = '20,20'
$RadioButton1.size = '130,30'
$RadioButton1.Checked = $false
$RadioButton1.Text = "User"
$RB11 = "User"
$RadioButton2 = New-Object System.Windows.Forms.RadioButton
$RadioButton2.Location = '20,50'
$RadioButton2.size = '130,30'
$RadioButton2.Checked = $false
$RadioButton2.Text = "Administrator"
$RB22 = "Administrator"
$OKButton = new-object System.Windows.Forms.Button
$OKButton.Location = '5,100'
$OKButton.Size = '90,35'
$OKButton.Text = 'OK'
$OKButton.DialogResult=[System.Windows.Forms.DialogResult]::OK
$CancelButton = new-object System.Windows.Forms.Button
$CancelButton.Location = '105,100'
$CancelButton.Size = '90,35'
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$CancelButton.DialogResult=[System.Windows.Forms.DialogResult]::Cancel
$form.Controls.AddRange(#($MyGroupBox,$OKButton,$CancelButton))
$MyGroupBox.Controls.AddRange(#($Radiobutton1,$RadioButton2))
$form.AcceptButton = $OKButton
$form.CancelButton = $CancelButton
$form.Add_Shown({$form.Activate()})
$dialogResult = $form.ShowDialog()
if ($DialogResult -eq "OK")
{
if ($RadioButton1.Checked){$global:Roll = $RB11}
elseif ($RadioButton2.Checked){$global:Roll = $RB22}
}
elseif ($DialogResult -eq "Cancel")
{
break
}
}
Access_Roll
function Access_Responsibilities{
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$Form = New-Object System.Windows.Forms.Form
$Form.width = 265
$Form.height = 510
$Form.Text = ”Select Type of Access"
$Font = New-Object System.Drawing.Font("Verdana",11)
$Form.Font = $Font
$MyGroupBox = New-Object System.Windows.Forms.GroupBox
$MyGroupBox.Location = '5,5'
$MyGroupBox.size = '230,420'
$Checkbox1 = New-Object System.Windows.Forms.Checkbox
$Checkbox1.Location = '20,20'
$Checkbox1.size = '200,25'
$Checkbox1.Checked = $false
$Checkbox1.Text = "Collections"
$CB1 = "Collections"
$Checkbox2 = New-Object System.Windows.Forms.Checkbox
$Checkbox2.Location = '20,45'
$Checkbox2.size = '200,25'
$Checkbox2.Checked = $false
$Checkbox2.Text = "Credit Management"
$CB2 = "Credit Management"
$Checkbox3 = New-Object System.Windows.Forms.Checkbox
$Checkbox3.Location = '20,70'
$Checkbox3.size = '200,25'
$Checkbox3.Checked = $false
$Checkbox3.Text = "Cash Application"
$CB3 = "Cash Application"
$Checkbox4 = New-Object System.Windows.Forms.Checkbox
$Checkbox4.Location = '20,95'
$Checkbox4.size = '200,25'
$Checkbox4.Checked = $false
$Checkbox4.Text = "Sales and Service User"
$CB4 = "Sales and Service User"
$Checkbox5 = New-Object System.Windows.Forms.Checkbox
$Checkbox5.Location = '20,115'
$Checkbox5.size = '200,50'
$Checkbox5.Checked = $false
$Checkbox5.Text = "Controller (specify plant below)"
$CB5 = "Controller (specify plant below)"
$Checkbox6 = New-Object System.Windows.Forms.Checkbox
$Checkbox6.Location = '20,160'
$Checkbox6.size = '200,50'
$Checkbox6.Checked = $false
$Checkbox6.Text = "Plant (specify plant below)"
$CB6 = "Plant (specify plant below)"
$Checkbox7 = New-Object System.Windows.Forms.Checkbox
$Checkbox7.Location = '20,205'
$Checkbox7.size = '200,25'
$Checkbox7.Checked = $false
$Checkbox7.Text = "Sales"
$CB7 = "Sales"
$Checkbox8 = New-Object System.Windows.Forms.Checkbox
$Checkbox8.Location = '20,230'
$Checkbox8.size = '200,25'
$Checkbox8.Checked = $false
$Checkbox8.Text = "Pricing"
$CB8 = "Pricing"
$Checkbox9 = New-Object System.Windows.Forms.Checkbox
$Checkbox9.Location = '20,255'
$Checkbox9.size = '200,25'
$Checkbox9.Checked = $false
$Checkbox9.Text = "Warranty"
$CB9 = "Warranty"
$Checkbox10 = New-Object System.Windows.Forms.Checkbox
$Checkbox10.Location = '20,280'
$Checkbox10.size = '200,25'
$Checkbox10.Checked = $false
$Checkbox10.Text = "GL Accountant"
$CB10 = "GL Accountant"
$Checkbox11 = New-Object System.Windows.Forms.Checkbox
$Checkbox11.Location = '20,300'
$Checkbox11.size = '200,50'
$Checkbox11.Checked = $false
$Checkbox11.Text = "Mexico Billing (specific plant below)"
$CB11 = "Mexico Billing (specific plant below)"
$Checkbox12 = New-Object System.Windows.Forms.Checkbox
$Checkbox12.Location = '20,340'
$Checkbox12.size = '200,75'
$Checkbox12.Checked = $false
$Checkbox12.Text = "Non Problem Owner (To be used for Inquiry Only)"
$CB12 = "Non Problem Owner (To be used for Inquiry Only)"
$OKButton = new-object System.Windows.Forms.Button
$OKButton.Location = '10,430'
$OKButton.Size = '90,35'
$OKButton.Text = 'OK'
$OKButton.DialogResult=[System.Windows.Forms.DialogResult]::OK
$CancelButton = new-object System.Windows.Forms.Button
$CancelButton.Location = '110,430'
$CancelButton.Size = '90,35'
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$CancelButton.DialogResult=[System.Windows.Forms.DialogResult]::Cancel
$form.Controls.AddRange(#($MyGroupBox,$OKButton,$CancelButton))
$MyGroupBox.Controls.AddRange(#($Checkbox1,$Checkbox2,$Checkbox3,$Checkbox4,$Checkbox5,$Checkbox6,$Checkbox7,$Checkbox8,$Checkbox9,$Checkbox10,$Checkbox11,$Checkbox12))
$form.AcceptButton = $OKButton
$form.CancelButton = $CancelButton
$form.Add_Shown({$form.Activate()})
$dialogResult = $form.ShowDialog()
if ($DialogResult -eq "OK")
{
if ($Checkbox1.Checked){$global:AccessResponsibilities = $CB1}
elseif ($Checkbox2.Checked){$global:AccessResponsibilities = $CB2}
elseif ($Checkbox2.Checked){$global:AccessResponsibilities = $CB3}
elseif ($Checkbox2.Checked){$global:AccessResponsibilities = $CB4}
elseif ($Checkbox2.Checked){$global:AccessResponsibilities = $CB5}
elseif ($Checkbox2.Checked){$global:AccessResponsibilities = $CB6}
elseif ($Checkbox2.Checked){$global:AccessResponsibilities = $CB7}
elseif ($Checkbox2.Checked){$global:AccessResponsibilities = $CB8}
elseif ($Checkbox2.Checked){$global:AccessResponsibilities = $CB9}
elseif ($Checkbox2.Checked){$global:AccessResponsibilities = $CB10}
elseif ($Checkbox2.Checked){$global:AccessResponsibilities = $CB11}
elseif ($Checkbox2.Checked){$global:AccessResponsibilities = $CB12}
}
elseif ($DialogResult -eq "Cancel")
{
break
}
}
Access_Responsibilities
function Read-MultiLineInputBoxDialog([string]$Message, [string]$WindowTitle, [string]$DefaultText)
{
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName System.Windows.Forms
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Size(10,10)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.AutoSize = $true
$label.Text = $Message
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Size(10,40)
$textBox.Size = New-Object System.Drawing.Size(575,200)
$textBox.AcceptsReturn = $true
$textBox.AcceptsTab = $false
$textBox.Multiline = $true
$textBox.ScrollBars = 'Both'
$textBox.Text = $DefaultText
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Size(415,250)
$okButton.Size = New-Object System.Drawing.Size(75,25)
$okButton.Text = "OK"
$okButton.Add_Click({ $form.Tag = $textBox.Text; $form.Close() })
$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Size(510,250)
$cancelButton.Size = New-Object System.Drawing.Size(75,25)
$cancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$cancelButton.Add_Click({ $form.Tag = $null; $form.Close() })
$form = New-Object System.Windows.Forms.Form
$form.Text = $WindowTitle
$form.Size = New-Object System.Drawing.Size(610,320)
$form.FormBorderStyle = 'FixedSingle'
$form.StartPosition = "CenterScreen"
$form.AutoSizeMode = 'GrowAndShrink'
$form.Topmost = $True
$form.AcceptButton = $okButton
$form.CancelButton = $cancelButton
$form.ShowInTaskbar = $true
$form.Controls.Add($label)
$form.Controls.Add($textBox)
$form.Controls.Add($okButton)
$form.Controls.Add($cancelButton)
$form.Add_Shown({$form.Activate()})
$form.ShowDialog() > $null
return $form.Tag
}
$global:Comments = Read-MultiLineInputBoxDialog -Message "Enter any comments from the ticket" -WindowTitle "Comments"
$Username = [System.Environment]::UserName
$subject = "Ticket $SCTicket - $User's GetPaid Access Request"
$body = "
Team,
Per ticket $SCTicket, $User is requesting access to GetPaid.
Details from RITM ticket $RITMTicket are below:
Access Type: $AccessType
Roll: $Roll
Responsibilities:
Comments: $Comments
Thank You,
"
$ButtonType = [System.Windows.MessageBoxButton]::YesNo
$MessageIcon = [System.Windows.MessageBoxImage]::Warning
$MessageTitle = "GetPaid - Access Request"
$MessageBody = "The information you have entered is show below:`n`n`nTicket Number: $Ticket`n`nUser's Email Address: $UserID`n`nAccess Type: $AccessType`n`nRoll: $Roll`n`nResponsibilities: `n`nComments: $Comments`n`n`nIf you would like to send the email, click Yes.`nOtherwise, click No."
$Result = [System.Windows.MessageBox]::Show($MessageBody,$MessageTitle,$ButtonType,$MessageIcon)
if ($Result -eq "No")
{
Exit-PSSession
}
else
{
Send-MailMessage -To "<$Username#xxx.com>" -bcc "<$Username#xxx.com>" -from "<itservicedesk#xxx.com>" -Subject $subject -SmtpServer "mailrelay.xxx.com" -body $body
}
}
Function Clean-Memory {
Get-Variable |
Where-Object { $startupVariables -notcontains $_.Name } |
ForEach-Object {
try { Remove-Variable -Name "$($_.Name)" -Force -Scope "global" -ErrorAction SilentlyContinue -WarningAction SilentlyContinue}
catch { }
}
}
Here's a procedural way to retrieve the matching values.
First, we retrieve all variables with $Checkbox in the name, then grab only ones with a CheckState value. Then, for each, we substring out the number and append that to the string CB to retrieve the value of the corresponding $CB# variable.
Then we store that in an array list and join with a comma at the end, for the output you see below.
$properties = New-object System.Collections.ArrayList
$CheckedBoxes = Get-Variable checkbox* | Where-object {$_.Value.CheckState -eq 'Checked'}
ForEach ($Checked in $CheckedBoxes){
$CBNumber = $CheckBox.Name.Split('Checkbox')[-1]
$Properties.Add((Get-variable "CB$CBNumber").Value) | out-null
}
$Properties -join ','
>GL Accountant, Pricing, Credit Management
All of this code should be used where you have this code today:
if ($Checkbox1.Checked){$global:AccessResponsibilities = $CB1}
#Insert new code here <---------------
elseif ($Checkbox2.Checked){$global:AccessResponsibilities = $CB2}
elseif ($Checkbox2.Checked){$global:AccessResponsibilities = $CB3}
Just from a preliminary look through the code and the question that you are asking, this is what I have come up with.
Basically, if the checkbox is checked, it appends the $CB(1-12) string to the $global:AccessResponsibilities and then in your email just reference $global:AccessResponsibilities and it should have all the strings of the checkboxes that were checked
if ($Checkbox1.Checked){$global:AccessResponsibilities += $CB1 + "`r`n"}
if ($Checkbox2.Checked){$global:AccessResponsibilities += $CB2 + "`r`n"}
if ($Checkbox3.Checked){$global:AccessResponsibilities += $CB3 + "`r`n"}
if ($Checkbox4.Checked){$global:AccessResponsibilities += $CB4 + "`r`n"}
if ($Checkbox5.Checked){$global:AccessResponsibilities += $CB5 + "`r`n"}
if ($Checkbox6.Checked){$global:AccessResponsibilities += $CB6 + "`r`n"}
if ($Checkbox7.Checked){$global:AccessResponsibilities += $CB7 + "`r`n"}
if ($Checkbox8.Checked){$global:AccessResponsibilities += $CB8 + "`r`n"}
if ($Checkbox9.Checked){$global:AccessResponsibilities += $CB9 + "`r`n"}
if ($Checkbox10.Checked){$global:AccessResponsibilities += $CB10 + "`r`n"}
if ($Checkbox11.Checked){$global:AccessResponsibilities += $CB11 + "`r`n"}
if ($Checkbox12.Checked){$global:AccessResponsibilities += $CB12 + "`r`n"}
heres a small loop that should work for HeedfulCrayon's answer.
foreach ($i in 1..12)
{
if ((Get-Variable -Name Checkbox$i).Value.Checked)
{
$Global:AccessResponsibilities += ((Get-Variable -Name CB$i).Value + "`r`n")
}
}
I Am trying to create a drop down box in my form that is set up with two text fields. However when i try to add the dropdown to the form it changes the end values, even though I am not changing the type or answer, it changes the value it returns. The original code I wrote that runs through perfect is:
function button ($title,$mailbx, $WF, $TF) {
###################Load Assembly for creating form & button######
[void][System.Reflection.Assembly]::LoadWithPartialName( “System.Windows.Forms”)
[void][System.Reflection.Assembly]::LoadWithPartialName( “Microsoft.VisualBasic”)
#####Define the form size & placement
$form = New-Object “System.Windows.Forms.Form”;
$form.Width = 750;
$form.Height = 500;
$form.Text = $title;
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen;
##############Define text label1
$textLabel1 = New-Object “System.Windows.Forms.Label”;
$textLabel1.Left = 25;
$textLabel1.Top = 15;
$textLabel1.Text = $mailbx;
##############Define text label2
$textLabel2 = New-Object “System.Windows.Forms.Label”;
$textLabel2.Left = 25;
$textLabel2.Top = 50;
$textLabel2.Text = $WF;
##############Define text label3
$textLabel3 = New-Object “System.Windows.Forms.Label”;
$textLabel3.Left = 25;
$textLabel3.Top = 85;
$textLabel3.Text = $TF;
############Define text box1 for input
$textBox1 = New-Object “System.Windows.Forms.TextBox”;
$textBox1.Left = 150;
$textBox1.Top = 10;
$textBox1.width = 200;
############Define text box2 for input
$textBox2 = New-Object “System.Windows.Forms.TextBox”;
$textBox2.Left = 150;
$textBox2.Top = 50;
$textBox2.width = 200;
############Define text box3 for input
$textBox3 = New-Object “System.Windows.Forms.TextBox”;
$textBox3.Left = 150;
$textBox3.Top = 90;
$textBox3.width = 200;
#############Define default values for the input boxes
$defaultValue = “”
$textBox1.Text = $defaultValue;
$textBox2.Text = $defaultValue;
$textBox3.Text = $defaultValue;
#############define OK button
$button = New-Object “System.Windows.Forms.Button”;
$button.Left = 360;
$button.Top = 85;
$button.Width = 100;
$button.Text = “Ok”;
############# This is when you have to close the form after getting values
$eventHandler = [System.EventHandler]{
$textBox1.Text;
$textBox2.Text;
$textBox3.Text;
$form.Close();};
$button.Add_Click($eventHandler) ;
#############Add controls to all the above objects defined
$form.Controls.Add($button);
$form.Controls.Add($textLabel1);
$form.Controls.Add($textLabel2);
$form.Controls.Add($textLabel3);
$form.Controls.Add($textBox1);
$form.Controls.Add($textBox2);
$form.Controls.Add($textBox3);
$ret = $form.ShowDialog();
#################return values
return $textBox1.Text, $textBox2.Text, $textBox3.Text
}
$return= button “Enter Info” “First Name” “Last Name” “Email Address”
$return2 = ($return[0] + " " + $return[1])
$return3 = ($return[0] + "." + $return[1])
$return4 = $return[0] + "." + $return[1] + "$return[2]"
New-Mailbox -Alias $return3 -Name $return2 -FirstName $return[0] -LastName $return[1] -UserPrincipalName $return4 -Password (ConvertTo-SecureString -String 'P#ssw0rd' -AsPlainText -Force) -ResetPasswordOnNextLogon $true
Set-User -Identity $return3 -StreetAddress '1600 Pennsylvania Ave NW' -City 'Washington' -StateOrProvince 'D.C.' -PostalCode '20500' -Phone '202-456-1111' -Fax '202-456-2461'
The drop down code I have is
########################
# Edit This item to change the DropDown Values
[array]$DropDownArray = "#yahoo.com", "#gmail.com", "#lewisJ.com"
# This Function Returns the Selected Value and Closes the Form
function Return-DropDown {
$Choice = $DropDown.SelectedItem.ToString()
}
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$DropDown = new-object System.Windows.Forms.ComboBox
$DropDown.Location = new-object System.Drawing.Size(400,10)
$DropDown.Size = new-object System.Drawing.Size(130,30)
ForEach ($Item in $DropDownArray) {
$DropDown.Items.Add($Item)
}
$Form.Controls.Add($DropDown)
$DropDownLabel = new-object System.Windows.Forms.Label
$DropDownLabel.Location = new-object System.Drawing.Size(10,10)
$DropDownLabel.size = new-object System.Drawing.Size(100,20)
$DropDownLabel.Text = "Items"
$Form.Controls.Add($DropDownLabel)
$Button = new-object System.Windows.Forms.Button
$Button.Location = new-object System.Drawing.Size(100,50)
$Button.Size = new-object System.Drawing.Size(100,20)
$Button.Text = "OK"
$Button.Add_Click({Return-DropDown})
$form.Controls.Add($Button)
I am trying to have it so if I enter the first name as Ben, Last name as Don, and use the drop down feature and select #gmail.com. it will return those values. When I tried to combine the two codes it changed all the values to :
System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d5 0a3a System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089.System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d5 0a3a System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089.System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d5 0a3a0
Anyone know how to get this drop down correct?
It seems once again that you are in need of voiding some return values from the methods in your code. If you look at TechNet for a list box (Yes I know we have a drop down) you will see that on top of voiding
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
You also have to address the Add method as well.
[void]$DropDown.Items.Add($Item)
That should ensure your returns are the ones you want. You might still have an issue with the value from the drop down but this will get you in the right direction.