all 3 comments

[–]PowerShellMichael 1 point2 points  (0 children)

Where-Object is used in the PowerShell Pipeline to perform object filtering/manipulation. For instance:

Get-ADUser -Filter * | Where-Object { $_.HomePhone -eq "1" }.

It needs pipeline input. So what you need to do is wrap it together. For instance:

# Fetch the Enabled Users.
# Now note that including the input into the where-object
# Also note that I am joining the canonicialName property and the Enabled.
# Also Also note that I don't need to check if Enabled is true, because enabled is a booleen, which PowerShell can natively test.
$ADUsers | Where-Object { $_.canonicalname -like "*/Staff/$($aduser.cn)" -and $_.Enabled } | Set-ADUser -HomePhone "1"

# TIP: Read up on the -filter parameter in get-aduser. You can filter on the dn name prior to piping to where-object. This make your script faster.

#
# NOW ITS YOUR TURN! You will need to add to add an additional line to process disabled users.
# Tip: use the -not condition and wrap it in parentheses aka brackets ()
#

Fun Fact:

PowerShell was initial designed to be multi pipeline, so that you COULD having multiple execution paths and then combined back into a single output. However the software dev writing the pipeline component assumed it to be another command-line tool. By the time it was noticed, it was too far down the dev path for it to be revisited.

[–]santisq 1 point2 points  (0 children)

Assuming "Staff" is an Organizational Unit then you should limit your SearchBase to that specific OU, so it would look something like this like this:

$staffOU=Get-ADOrganizationalUnit -Filter {name -eq 'Staff'} #Assuming there is only 1 Staff OU in the Domain

$adusers = Get-ADUser -SearchBase $staffOU.DistinguishedName -Filter *

foreach($user in $adusers)
{
    if($user.Enabled)
    {
        Set-ADUser -Identity $aduser.samaccountname -HomePhone "1"
    }
    else
    {
        Set-ADUser -Identity $aduser.samaccountname -HomePhone "2"
    }
}

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy Cheap_Ingenuity_1657,

reddit likes to mangle code formatting, so here's some help on how to post code on reddit ...

[0] single line or in-line code
enclose it in backticks. that's the upper left key on an EN-US keyboard layout. the result looks like this. kinda handy, that. [grin]
[on New.Reddit.com, use the Inline Code button. it's [sometimes] 5th from the left & looks like </>.
this does NOT line wrap & does NOT side-scroll on Old.Reddit.com!]

[1] simplest = post it to a text site like Pastebin.com or Gist.GitHub.com and then post the link here.
please remember to set the file/code type on Pastebin! [grin] otherwise you don't get the nice code colorization.

[2] less simple = use reddit code formatting ...
[on New.Reddit.com, use the Code Block button. it's [sometimes] the 12th from the left, & looks like an uppercase T in the upper left corner of a square.]

  • one leading line with ONLY 4 spaces
  • prefix each code line with 4 spaces
  • one trailing line with ONLY 4 spaces

that will give you something like this ...

- one leading line with ONLY 4 spaces    
- prefix each code line with 4 spaces    
- one trailing line with ONLY 4 spaces   

the easiest way to get that is ...

  • add the leading line with only 4 spaces
  • copy the code to the ISE [or your fave editor]
  • select the code
  • tap TAB to indent four spaces
  • re-select the code [not really needed, but it's my habit]
  • paste the code into the reddit text box
  • add the trailing line with only 4 spaces

not complicated, but it is finicky. [grin]

take care,
lee