Switch statement - issue commands

Guys.

Been trying to get this to work for the last couple of hours. I am trying to run Set-QADUser command to set an AD attribute value from within one of my switch statements. I thought that using $_ would be ok to pass the users logon account.

What am i doing wrong here?

Connect-QADService -Service "ARS-Service-Name" -Proxy

$OUSelected = "OU-NAME"
Get-qaduser -SizeLimit 0 -SearchRoot $OUSelected |
    ForEach-Object {
        switch ($_.physicalDeliveryOfficeName){
            'London' {Set-QADUser -Identity $_ -ObjectAttributes @{'AD ATTRIBUTE HERE'= "AD ATTRIBUTE VALUE" }}
            'Milan'  {2}
            'Padova' {3}
            'Geneva' {4}
            default {'No site'}
        }
     }

July 15th, 2015 9:19am

Hi Wallace,

you may want to describe a bit more about what happens when you run this. Questions:

  • Do the switch cases work as intended?
  • Have you tried to validate the content of $_ inside a switch clause?
  • Have you tried to use $_.SamAccountName instead of $_ for the -Identity parameter?

Cheers,
Fred

Free Windows Admin Tool Kit Click here and download it now
July 15th, 2015 9:26am

Hello Fred. Thanks for the reply.

User JVR provided the Switch statement for me and run on its own then it does work and works great.

If i runthe code below insode the London switch statement then it displays CN=Active Directory

'London' {Write-Host $_.Name }

or

'London' {Write-Host $_.SamAccountName }

I am confused as i thought $_ would have been sufficiant.

July 15th, 2015 9:46am

$users = Get-aduser -properties physicalDeliveryOfficeName -Searchbase $OUSelected -Filter * |select -First 10 


foreach($user in $users) {
switch ($user.physicalDeliveryOfficeName) {

'myoffice' { $user.samaccountname }

}

}
Free Windows Admin Tool Kit Click here and download it now
July 15th, 2015 10:02am

Hi Wallace,

as you are using the Quest AD Cmdlets I can't tell you much about how Get-QADUser works. The examples I could find showed the cmdlet using the distinguished name. This you can probably use like this:

Set-QADUser -Identity $_.DistinguishedName -ObjectAttributes @{ 'AD ATTRIBUTE HERE'= "AD ATTRIBUTE VALUE" }

Cheers,
Fred

July 15th, 2015 10:03am

Always make sure you are returning the correct objects before modifying them.

foreach($user in $users) {
switch ($user.physicalDeliveryOfficeName) {

'Milan'  { $user.samaccountname + " - " + $user.physicalDeliveryOfficeName }
'Padova' { $user.samaccountname + " - " + $user.physicalDeliveryOfficeName }
'Geneva' { $user.samaccountname + " - " + $user.physicalDeliveryOfficeName }

}

}

Free Windows Admin Tool Kit Click here and download it now
July 15th, 2015 10:09am

Why make it so complicated.  Just use the pipeline:

$OUSelected='OU-NAME'
function Get-SiteID{
    param($sitename)

    switch ($sitename) {
        'London' { 1 }
        'Milan'  { 2 }
        'Padova' { 3 }
        'Geneva' { 4 }
        default { 'No site' }
    }
}

Connect-QADService -Service "ARS-Service-Name" -Proxy
Get-qaduser -SizeLimit 0 -SearchRoot $OUSelected |
    Set-QADUser -ObjectAttributes @{ 'AD ATTRIBUTE HERE'=(Get-SiteID $_.physicalDeliveryOfficeName)} 

We really don't even need a function but it makes it a bit easier to understand.

July 15th, 2015 10:31am

If we need to do more things then this would work:

Get-qaduser -SizeLimit 0 -SearchRoot $OUSelected |
    ForEach-Object{
        $siteid=Get-SiteID $_.physicalDeliveryOfficeName
        $_|Set-QADUser -ObjectAttributes @{ 'AD ATTRIBUTE HERE'=$siteID} 
    }

Free Windows Admin Tool Kit Click here and download it now
July 15th, 2015 10:33am

And we can skip the function like this:

Get-qaduser -SizeLimit 0 -SearchRoot $OUSelected |
ForEach-Object{
    $siteid = switch ($_.physicalDeliveryOfficeName) {
        'London' { 1 }
        'Milan'  { 2 }
        'Padova' { 3 }
        'Geneva' { 4 }
        default { 'No site' }
    }
    $_ | Set-QADUser -ObjectAttributes @{ 'AD ATTRIBUTE HERE' = $siteID }
}

July 15th, 2015 10:35am

Thanks everyone for the reply. Just going through them now. I think i have been misunderstood.

The attribute value will be different and not based on the SiteID. For example someone office might be London but i want the AD attribute to be EU. I am just using the SiteID to then apply an update based from that.

I will see if i can pick it apart and get it to work. if not i will reply back and see if someone can guide me.

Free Windows Admin Tool Kit Click here and download it now
July 15th, 2015 1:08pm

The "switch" statement will return anything you place in the {} number, strings - anything; even old slices of pizza.

July 15th, 2015 1:14pm

Thanks everyone for the reply. Just going through them now. I think i have been misunderstood.

The attribute value will be different and not based on the SiteID. For example someone office might be London but i want the AD attribute to be EU. I am just using the SiteID to then apply an update based from that.

I will see if i can pick it apart and get it to work. if not i will reply back and see if someone can guide me.

JRV's answers are great solutions but I thought it'd be easier to see the difference in what you are returning with these two examples..

$users = Get-aduser -properties physicalDeliveryOfficeName -Searchbase $OUSelected -Filter * |select -First 10 

foreach($user in $users) {
switch ($user.physicalDeliveryOfficeName) {

'Milan' { $user }

}

}




Get-aduser -properties physicalDeliveryOfficeName -Searchbase $OUSelected -Filter * |select -First 10 | % {

switch ($_.physicalDeliveryOfficeName) {

'Milan' { $_ }

}

}


btw, happy belated End of support 2003. You should work on 2008r2 now at a minimum:-)

Free Windows Admin Tool Kit Click here and download it now
July 16th, 2015 8:30am

...

    ForEach-Object {
        switch ($_.physicalDeliveryOfficeName){
            'London' {Set-QADUser -Identity $_ -ObjectAttributes @{'AD ATTRIBUTE HERE'=      ...

Your code misuse the automatic variable $_.

The 1st $_ is created by the ForEach-Object cmdlet, while the 2nd $_ is created by the switch statement... Thus, you can't use it as argument for the -identity parameter.

Here's a practical example of how to use the switch statement:

https://social.technet.microsoft.com/Forums/windowsserver/en-US/9a577cce-f507-4247-af27-b68e48d23188/use-user-input-to-stop-a-script?forum=winserverpowershell

July 16th, 2015 11:35pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics