Using expressions in pipeline parameters

Often times you need the second cmdlet in a pipeline to not only accept the collection from the first one but also re-use some of the properties of the members. For example:

  • Renaming objects so the new name is the old one plus some prefix, or
  • Move objects to an OU based on some of objects’ attributes, or
  • Set password for a user to something containing his/her username.

I guess you got the idea. The problem with that is that in PowerShell if you just try to add this as an expression this will not work.

With AD cmdlets 1.0.5 the problem finally got a solution. Although PowerShell still does not allow to use expressions in the parameters you can use scriptblocks and AD cmdlets will handle the rest.

Just remember to use {} brackets instead of ():

[Update] This scriptblock approach works for Move/Rename but not for Set cmdlets:

# Add prefix to a set of user accounts:

Get-QADUser -SearchRoot ps64.local/test | Rename-QADObject -NewName {"Test_" + $_.Name}

# Restructure AD:

Get-QADUser -SearchRoot ps64.local/employees | Move-QADObject -to {"ps64.local/employees/" + $_.City}

For other cases you still need to use ForEach:

# Set password to username (note that your AD password restrictions might prevent you from setting passwords containing usernames)

Get-QADUser Test* | ForEach { Set-QADObject $_ -UserPassword {$_.sAMAccountName} }

#Set description including user properties

Get-QADUser | ForEach { Set-QADObject $_ -Description {$_.Title + " from " + $_.City + " " + $_.Department}}

Good luck with your bulk changes!

Tags: , , , , , ,

4 Responses to “Using expressions in pipeline parameters”


  1. 1 George Kesler October 26, 2007 at 5:43 pm

    Hi Dmitry,

    there are two tabs on the User Property page in AD which I cant get access to from powershell: Session and Remote Control.
    I figured out that the settings are kept in the userParameters property but I can’t decode the string it returns:

    $k = Get-QADUser -lastname abc
    $k.userParameters
    P☺CtxCfgPresent????☺CtxCfgFlags1????☺CtxShadow????☺CtxMaxDisco
    nnectionTime???☺CtxMaxIdleTime????*☻☺CtxMinEncryptionLevel?

    Much less I can think of a way of setting these properties.
    You are my last hope🙂

  2. 2 dmitrysotnikov October 29, 2007 at 1:35 pm

    George,

    It looks like this are not really represented directly by specific attributes in AD.

    If you happen to be a Quest ActiveRoles Server customer you can use the -proxy mode and get access to the virtual attributes which ARS is constructing for these properties, but from your question I assume this is not the case you you are looking for a way to get hold of these attributes without buying extra stuff.

    In this case you might want to post the question to the Active Directory forum at PowerGUI.org: http://powergui.org/forum.jspa?forumID=173 – there are guys there who know the stuff much better than me, so they might be able to help you out.

    Dmitry

  3. 3 George Kesler October 30, 2007 at 11:07 pm

    Thanks Dmitry,
    help they did right away:

    http://powergui.org/thread.jspa?messageID=13544&#13544

  4. 4 halr9000 November 5, 2007 at 7:52 pm

    Dimitry, can’t you can make expressions work with subexpressions? ‘$()’ ? Having support for scriptblocks as an input object type is cool though.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




My Recent Tweets

Legal

The posts on this blog are provided “as is” with no warranties and confer no rights. The opinions expressed on this site are mine and mine alone, and do not necessarily represent those of my employer - WSO2 or anyone else for that matter. All trademarks acknowledged.

© 2007-2014 Dmitry Sotnikov

October 2007
M T W T F S S
« Sep   Nov »
1234567
891011121314
15161718192021
22232425262728
293031  

%d bloggers like this: