Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Checkbox selection not being selective

I am playing around with WPF and powershell project for a tool and some learning. I have built a screen which allows you to display user information. I have added checkboxes next to each textbox so that if you edit a field then click the button it will change that section.

The issue i have is i cannot get it to work based on the selection. I have the below which works in the way it changes all fields regardless of the selection. I have played around with a few combinations but none seem to give the desired effect.

function UpdateUserInfoClick
{
    param($sender, $e)

# prepare table
$userUpdateData = @{
  Identity = $UserSearchName.text
  Server   = $UserSearchDomainSelect.text
  Credential = $ADMCreds
  GivenName = $USFN.Text
  SurName = $USLN.Text
  DisplayName = $USDN.Text
  Description = $USDesc.Text
  Confirm = $false

}

# populate entry values associated with checked checkboxes
if ($USFNCB.IsChecked = $true) {
  $userUpdateData['GivenName'] = $USFN.Text
}
if ($USLNCB.IsChecked = $true) {
  $userUpdateData['SurName'] = $USLN.Text
}
if ($USDNCB.IsChecked = $true) {
  $userUpdateData['DisplayName'] = $USDN.Text
}
if ($USDescCB.IsChecked = $true) {
  $userUpdateData['Description'] = $USDesc.Text
}
# decide on target server based on selected domain
if ($UserSearchDomainSelect.text -eq "Domain1") {
  $userUpdateData['Server'] = 'Domain1.com'
}
elseif ($UserSearchDomainSelect.text -eq "Domain2") {
  $userUpdateData['Server'] = 'Domain2.com'
}
elseif ($UserSearchDomainSelect.text -eq "Domain3.com") {
  $userUpdateData['Server'] = 'Domain3.com'
}
else {
  throw 'unexpected domain selection, cannot map to domain controller'
}
if ($userUpdateData.Count -gt 1) {
  # at least 1 updated property was checked, let's update the backend
  Set-ADUser @userUpdateData
}

}

The xaml code for one of the checkboxes

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<CheckBox x:Name="USFNCB" Content="" HorizontalAlignment="Left" Margin="461,148,0,0" VerticalAlignment="Top" Height="20" Width="20" IsChecked="False"/>

Not sure if there is some more definition need for the states in the Xaml or the the set-eventhandlers?

Thanks

>Solution :

In PowerShell, = is never used as a comparison operator – it’s only used for assignment.

Like in most other C-inspired languages, referencing an assignment operation as a value expression evaluates to a reference to the assignment target, which after the assignment contains the value $true.

In plain English, that means that this:

if ($USFNCB.IsChecked = $true) {
  $userUpdateData['GivenName'] = $USFN.Text
}

… is equivalent to

if ($true) {
  $userUpdateData['GivenName'] = $USFN.Text
}

Remove the assignment operation and your if statements will be evaluated as you expect them to:

function UpdateUserInfoClick {
  param($sender, $e)

  # prepare table
  $userUpdateData = @{
    Identity    = $UserSearchName.text
    Server      = $UserSearchDomainSelect.text
    Credential  = $ADMCreds
    GivenName   = $USFN.Text
    SurName     = $USLN.Text
    DisplayName = $USDN.Text
    Description = $USDesc.Text
    Confirm     = $false
  }

  # populate entry values associated with checked checkboxes
  if ($USFNCB.IsChecked) {
    $userUpdateData['GivenName'] = $USFN.Text
  }
  if ($USLNCB.IsChecked) {
    $userUpdateData['SurName'] = $USLN.Text
  }
  if ($USDNCB.IsChecked) {
    $userUpdateData['DisplayName'] = $USDN.Text
  }
  if ($USDescCB.IsChecked) {
    $userUpdateData['Description'] = $USDesc.Text
  }

  # decide on target server based on selected domain
  if ($UserSearchDomainSelect.Text -in "Domain1","Domain2","Domain3") {
    $userUpdateData['Server'] = $UserSearchDomainSelect.Text
  }
  else {
    throw 'unexpected domain selection, cannot map to domain controller'
  }

  if ($userUpdateData.Count -gt 1) {
    # at least 1 updated property was checked, let's update the backend
    Set-ADUser @userUpdateData
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading