0

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?

Vincent De Smet
  • 4,859
  • 2
  • 34
  • 41
Tyler S
  • 827
  • 1
  • 14
  • 23
  • Show the rest of your code. How exactly does the `$return` variable get set? What type of object is it? Is it merely an array? If so, what types of objects does it contain? Your statement that `$return[7]` is either "K1" or "E4" is likely the assumption that is causing your failure. If..else statements don't fail; instead, you must understand exactly how the comparison is working. –  May 09 '15 at 03:08
  • On StackOverflow, an answer isn't the appropriate place to put your code. You can edit your question and put the code in there. –  May 09 '15 at 03:24
  • 3
    It looks like you're using wrong comparison sign in "if($return[7] = "K1"){" It should be "if($return[7] -eq "K1"){". This may be helpful too: http://stackoverflow.com/questions/18772063/how-to-compare-the-contents-of-two-string-objects-in-powershell – Grady G Cooper May 09 '15 at 03:27
  • 1
    i forgot you are supposed to use the -eq.... – Tyler S May 09 '15 at 03:34
  • Grady is right. Look up comparison operators vs. assignment operators. Good eye, Grady! –  May 09 '15 at 03:41

0 Answers0