How can I set a path for a new AD-User in this Switch example?

Hi,
I am nearing the end of my script that creates a new ad user, adds them to a set of groups, and moves them to their correct ou.
The problem I have is that I cant work out how to move them to the OU.
eg.
Step 1. In my function I have completed the part that creates the user, and I have used the -passthru parameter to return the new user object into a variable called $obj.  
Step 2. I have created a switch that accepts a department variable, and adds the user to the corresponding groups.
Step 3. Finally I want the user to be created in an OU that is relevant to their job role. If possible in the same switch as above.
Here is the switch :
Switch($dept)
Finance {$FinanceGroups = @("Finance","Users");ForEach($group in $FinanceGroups){Add-ADGroupMember -Identity $group -Members $Obj}}
Sales {$SalesGroups = @("Sales","Users");ForEach($group in $SalesGroups){Add-ADGroupMember -Identity $group -Members $Obj}}
Support {$SupportGroups = @("Support","Users");ForEach($group in $SupportGroups){Add-ADGroupMember -Identity $group -Members $Obj}}
I was trying to do it like this.
Finance{$FinanceGroups=@("Finance","VPN_Users");ForEach($groupin$FinanceGroups){Add-ADGroupMember-Identity$group-Members$Obj}{$Obj.Path ="OU=Staff,OU=Accounts,OU=Resources,DC=homenet,DC=com"}}
But it doesn't seem to work, and I think the problem could be that the new user object returned by the -passthru parameter is read only. So I can't set it's parameters.
Can you think of a way to do it?
Thanks

I would re-write this function as follows:
Function New-User {
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='High')]
Param(
[Parameter(Mandatory=$true,
ValueFromPipeLine=$true,
ValueFromPipeLineByPropertyName=$true,
Position=0)]
[String]$fname,
[Parameter(Mandatory=$true,
ValueFromPipeLine=$true,
ValueFromPipeLineByPropertyName=$true,
Position=1)]
[String]$sname,
[Parameter(Mandatory=$true,
ValueFromPipeLine=$true,
ValueFromPipeLineByPropertyName=$true,
Position=2)]
[ValidateSet("Milan","London","tokyo")]
[String]$site,
[Parameter(Mandatory=$true,
ValueFromPipeLine=$true,
ValueFromPipeLineByPropertyName=$true,
Position=3)]
[ValidateSet("Finance","Sales","Support")]
[String]$dept,
[Parameter(Mandatory=$false,
ValueFromPipeLine=$true,
ValueFromPipeLineByPropertyName=$true,
Position=4)]
[String]$baseOU = "OU=Accounts,OU=Resources,DC=homenet,DC=com",
[Parameter(Mandatory=$false,
ValueFromPipeLine=$true,
ValueFromPipeLineByPropertyName=$true,
Position=5)]
[String]$tempPass = "Temp-pass1"
Write-Verbose "Creating user '$fname $sname' in OU 'OU=$dept,$baseOU'"
$objUser = New-ADUser -name "$fname $sname" `
-GivenName $fname `
-Surname $sname `
-DisplayName "$fname $sname" `
-SamAccountName "$fname.$sname" `
-AccountPassword ($tempPass | ConvertTo-SecureString -AsPlainText -Force ) `
-ChangePasswordAtLogon:$true `
-Path "OU=$dept,$baseOU" `
-PassThru
$Groups = @("All_$site","Users","$dept")
Write-Verbose "Adding user '$fname $sname' to groups '$($Groups -join ', ')'"
$Groups | % { Add-ADGroupMember -Identity $_ -Members $objUser.DistinguishedName }
Write-Verbose "Enabling user account '$fname $sname'"
Enable-ADAccount -Identity $objUser.DistinguishedName
<#
Examples
New-User -fname "Sam" -sname "test" -site Milan -dept Finance -Verbose
#>
Notice:
Function name: follows verb-noun format - best practice 
$obj variable renamed to $objUser - using variable name that reflects its content
Use of CmdLetBinding[] and confirm impact as high since you're writing to AD 
Use of ValidationSet to minimize parameter input errors
Parameterized baseOU, and TempPass
Added requirement to change password at first logon
Enabling the account at the end
You should wrap each of the 3 steps in a try-catch block. Check for things like user already exists or not, group exists r not, OU exists or not..
Sam Boutros, Senior Consultant, Software Logic, KOP, PA http://superwidgets.wordpress.com (Please take a moment to Vote as Helpful and/or Mark as Answer, where applicable) _________________________________________________________________________________
Powershell: Learn it before it's an emergency http://technet.microsoft.com/en-us/scriptcenter/powershell.aspx http://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx

Similar Messages

  • How can i set a path for my deployment files in weblogic server 10.3

    Hi
    How can i set the path for my WAR ,JAR files while deploying.i am using the wls10.3 version.
    is there any scripts for this ,please provide me.
    my Application is ADF 11g application.

    By "path", I assume you mean "classpath".
    The simplest way is simply to include the jars you need inside the web application or web module's WEB-INF/lib directory, EJB module's META-INF/lib directory, or EAR lib directory.
    If that's not practical, if you use NodeManager to start your servers, you can go to the "Server Start" tab in the server definition in the WebLogic console and edit the "Classpath" field, which defaults to no value. You can specify a classpath value there. Note that if you specify a value there, it REPLACES the default classpath for the server, it doesn't add to it. If you need to just add to it (a much more likely scenario), if the value references the value "$CLASSPATH" in it, that will reference the original classpath value that the server would have had.
    So, for instance, if you wanted to include the MQ jars in the server classpath, you could set a value like this:
    /usr/java/mq/lib/mq.jar:/usr/java/mq/lib/mqstuff.jar:$CLASSPATH

  • How can I set home page for a new tab? how can i get firefox to open the home page every time i open a new tab in Windows vista home basic??

    How can I set home page for a new tab? how can i get firefox to open the home page every time i open a new tab in Windows vista home basic??
    == This happened ==
    Every time Firefox opened

    Firefox can have multiple home pages if you wish. Each home page that will open when starting Firefox is separated by the "|" character.
    See: http://support.mozilla.com/en-US/kb/How+to+set+the+home+page
    To have new tabs open a specific web site, add one of the following extensions:
    http://sogame.awardspace.com/newtaburl/
    https://addons.mozilla.org/en-US/firefox/addon/777

  • How can I set a password for firefox so that everyone have to enter the password before executing firefox

    My younger sister executed my firefox just at this afternoon(she originally use IE), and she saw something that can't be seen. How can I set a password for firefox so that everyone have to enter the password before executing firefox?

    Also see this for an English version of Profile Password:
    *Profile Password: http://nic-nac-project.de/~kaosmos/profilepassword-en.html#PPF
    It is an extension to protect the profile with a password, but being an extension that protection can easily be bypassed by starting Firefox in [[Safe mode]].<br />
    So do not rely on it.<br />
    If you want to protect content then you have to use methods supported by the OS like a separate user account with a password or an encrypted file system.

  • How can i set a layout for MIGO screen

    Hello all,
    How can i set a layout for MIGO screen. & make that layout default. Like i want to drag Requistioner column next to quantity in MIgo screen. temporarily I can drag but is there a way to save as layout & save it.

    Hi
    First you arrange however you want and then go to table settings which is there in the right corner of the item details and create a variant and save.
    Find a button with blue yellow and white at the top end above the vertical drag bar when put the cursor it will show you as Configuration.
    After creating the variant tick the check box use as standard setting below the variant
    Hope it helps
    Edited by: Girish  Adaviswamy on Mar 3, 2010 1:27 PM

  • How can I set a default for webpages to expand to full screen?

    How can I set a default for webpages to expand to full screen? Every webpage I go to does not expand to a full screen which causes me to have to ctrl+++ each time. How can I set my preference to expand to full screen automatically?
    Thanks.

    The Firefox [https://support.mozilla.com/en-US/kb/Page+Zoom Page Zoom] feature does a domain by domain level of saving the users preferred zoom level settings, there is no default Page Zoom level setting in Firefox, as with some other browsers.
    Try the Default FullZoom Level extension: <br />
    https://addons.mozilla.org/en-US/firefox/addon/6965
    Or the NoSquint extension: <br />
    https://addons.mozilla.org/en-US/firefox/addon/2592/

  • How can I set a password for my pictures on iPad Air ?

    How can I set a password for my pictures on iPad Air ? if anyone have my iPad access mail or something I don't want anyone access my photos so how can I set it up.

    You can't set a password for the Photos nor Mail apps, you can only set a passcode for your iPad as a whole (via Settings > Passcode).
    There are third-party photo management apps such as Photo Manager Pro which you can copy your photos into from the Photos app (and then delete them from Photos), that app allows you (via its settings within the app) to set a password to access it.
    For email you could access your email via a browser (and not have your password stored in the browser), or see if there are any email apps in your country's app store which have a password option

  • How can i set class path in ubuntu JAVA_HOME ?

    how can i set class path in ubuntu <JAVA_HOME>?

    Note that on *nix
    environment variable names are case sensitive.
    Also, you are again being vague; is it [this it|http://java.sun.com/j2se/1.5.0/docs/tooldocs/solaris/javac.html] or [that it|http://java.sun.com/j2se/1.5.0/docs/tooldocs/solaris/java.html]

  • How can I set default values for Allocate Mode in AO config?

    Hi, How can I set default values for allocate mode in AO config. To be specific, in the attached vi, I need to set the Allocate Mode in AO Config to 'Use FIFO Memory (6)' if the value inside my case structure is false and to 'no change (0)' if the value inside the case strusture is true.
    Solved!
    Go to Solution.
    Attachments:
    generateWaveformFIFO.vi ‏15 KB

    Create two constants for the Allocate Mode input (right click > create > constant). Place one in the true case of the case structure, and place the other one in the false case. Wire them to the same tunnel (border of the case structure), then wire the tunnel to the Allocate Mode terminal of the AO Config. I don't have Traditional DAQ installed, but that should do it.
    Misha

  • How can you set a ringtone for unknown numbers

    How can you set a ringtone for unknown numbers?

    You can set up each one of your contacts with a ringtone and use the default for all other unknown numbers. There is no way to set up a ringtone for unknown numbers.

  • HI,How can i set delta option for costumised Generic data source

    Hi Sir/Madam,
          How can i set delta option for costumised Generic data source.
    Regards,
    Vishali.R
    Please search the forum before posting a thread
    Edited by: Pravender on Aug 15, 2011 1:26 PM

    Hi,
    Go to RSO2 transaction and click on change then on the tool bar click on Generic Delta button. In the next popup you can set your delta.
    Regards,
    Durgesh.

  • How can i set the orientation for a photo book (horizontal or vertical)

    how can i set the orientation for a photo book (horizontal or vertical)
    I like a photo book vertical format and i cant findet!!

    That was quick! Although my earlier search turned up nothing, when I posted my question the system immediately gave me a list of previous questions among which I found the following answer:
    This solved my question by LarryHN on Aug 18, 2010 6:01 PM
    select the photos and change the date/time - photo menu ==> adjust time and date to make a relative adjustment to a number of photos ==> batch change ==> time and date to set an absolute time date for them
    LN

  • HT204053 Hi, how can I set up icloud for my ipad and iphone5? I missed tout he initial setup step and now want to have both the devices under icloud. Please help...thank you!

    Hi, how can I set up icloud for my ipad and iphone5? I missed tout he initial setup step and now want to have both the devices under icloud. Please help...thank you!

    Go to Settings>iCloud and sign in with your Apple ID to create the account.  Then turn on the data you wish to sync with iCloud.  See http://www.apple.com/icloud/setup/ios.html.

  • HT1918 i want to change the country from USA to EGYPT in the itunes but i get a message saying, u have remaining balance which is 0.44 USD, how can i find some thing for 0.44 USD to spend this sum so i can switch my account to the right store??

    i want to change the country from USA to EGYPT in the itunes but i get a message saying, u have remaining balance which is 0.44 USD, how can i find some thing for 0.44 USD to spend this sum so i can switch my account to the right store??

    Buy something for more than the balance and charge the remainder to a credit card, or write Apple and ask them to keep the balance.
    iTunes Customer Service Contact - http://www.apple.com/support/itunes/contact.html > Get iTunes support via Express Lane > iTunes > iTunes Store

  • How can i change the path for uploadin videos??

    i m using javax.servlet.servletcontext for setting the path for storin upload videos...
    by using the getrealpath able to store videos in web-inf file...
    how can i change the location...

    this is the part of code in which i want to change my storage location:
    ** * The base path corresponds to the base directory of the
    * application context's WEB-INF directory. This means that
    * this directory is NOT visible to the end user.
    public static String getBasePath(javax.servlet.ServletContext context)
    { return context.getRealPath("/") + "WEB-INF" + directorySep;}
    /** * The temporary directory used to store files while they are
    * being uploaded. */
    public static String getTempPath(javax.servlet.ServletContext context)
    { return getBasePath(context); }
    /** * Gets the upload directory - used to indicate the local file
    * system storage area.
    can u tell me now how to change the context value???
    and how to change the storage loction??

Maybe you are looking for

  • Calculated field in a query

    Hi, To begin with I am just learning EJB and JAVA so please bear with me. I want to create a calculated field in a query from two fields in the table being queried, using SQL I would write the query like this: SELECT ledgerdate, description, quantity

  • External displays and projectors

    I've been having problems getting my MacbookAir (Mid 2012) running OSX 10.8.2 to use external displays and video projectors via VGA and DVI adapters. The problem is that if I am connected to my external monitor and disconnect the magsafe power adapte

  • XML Datatypes in Oracle 9i

    My database architect would like to leverage the XML datatype support that is new in Oracle 9i. How can I configure TopLink to directly map object attributes to elements in an XML structure that is stored in an XML-type column?

  • Support of ADF 11gR2 for Internet Explorer 10

    Following this link:http://www.oracle.com/technetwork/developer-tools/jdev/jdev11gr2-cert-405181.html there currently is no certification of Internet Explorer 10 for ADF 11.1.2.3.0. Are there any plans to certify Internet Explorer 10 in the near futu

  • Contacts on share on ovi

    been trying to add a contact it should be a simple matter of just clicking the persons pic and adding thm no matter how i try......it just wont click!!!! they can see my photos but i cant see theirs this person also uses ovi........ while im at it ho