PowerShell function to create new computers and add member groups

Posted on 2014-08-13
Last Modified: 2014-08-14
It seems like the problem with my function is the $C variable
I don't know how to properly call my $Computer variable that I first assigned into the Get-AdComputer syntax

Function Create-Account {

    # Link WSUS OU Computers
    $OUC = "OU=folder,DC=domain,DC=com"

    # Link OU Member Groups
    $OUM = "OU=folder,DC=domain,DC=com"

    # Link Member Groups into variable
    $LaptopSecGrp = Get-ADGroup -Filter {Name -like 'MemGroup1' -or Name -like 'MemGroup2' -or Name -like 'MemGroup3'} -SearchBase $OUM

    # Computer name into variable
    $Computer = 'ATEST1'

    # Add new computer to WSUS OU
    New-ADComputer -Name $Computer -Path $OUC

    $C = Get-ADComputer -Filter 'Name -like "$Computer"' -SearchBase $OUC
    # Add multiple member groups to multiple computers
    $LaptopSecGrp | ForEach-Object {Add-ADGroupMember $_.Name -Members $C}


Open in new window

If I remove the $Computer from the $C variable line and replace it with my computer name ATEST1 then it work properly. But obviously I want it to work as a variable. What am I doing wrong here? Thanks
Question by:ryanmaves
    LVL 40

    Accepted Solution

    Try with..
    $C = Get-ADComputer -Filter 'Name -like $Computer' -SearchBase $OUC

    Open in new window


    Author Comment


    Hey thanks, I think that worked but I can't be sure because what I found out is that every time I was running the entire function, I had assumed all my variables were being updated with the changes I had made.

    What I just found out was that my variables, specifically the $Computer var was saving previously assigned values. It would seem when I run the entire function it is not actually defining my variables in the process.

    If I highlight only the body of the function then all my variables update properly and after making the change you suggested it actually works!

    I guess now, or has always been, my issue here is why if I start a new PowerShell instance and run my function, expecting my variables to be created...they are not actually being created at all. Unless I highlight them and run selection??
    LVL 40

    Assisted Solution

    The variables inside the function live inside the function (it wont be available outside the function). So if you need to check the value you need to check it inside the function using Write-host or echo commands..

    Here is a good reference..

    Author Comment

    Hey, well no I don't need to check the value.

    What I'm thinking is that once I save this function and want to use it as a tool, I am going to be loading it into a new powershell instance before using it right? So that means none of my variables will exist until they have been set.

    Well, how come executing the function does not set all my variables, I don't get it? How else will my variables get set so that I can actually use the function; it seems like a lot of extra work to have to highlight my variables one at a time and run them so that they are set so that my function works properly right?

    Hope I make sense, I am very new to this so I'm sure it's something I just don't understand.
    LVL 40

    Expert Comment

    That's what I mentioned in my previous post.. :-).. The variable inside the function will not work outside the function. If you want to use the same variables outside the function, then you need to change the scope of the variable to "script" or "global". The article which I posted explains it well

    Function Test-test {
    $Global:X = "something"
    $Global:T = "Another thing"

    Open in new window


    Author Closing Comment

    ohh, okay I'm starting to see how that works. I went back and ran my function then used my function and it ran all my variables and commands correctly because I got the end result I wanted.

    What was confusing me was that my variables were not available "globally" (just learned this term). So variables are available within the function themselves by default, makes sense!

    Very exciting, thanks!
    LVL 40

    Expert Comment

    You're welcome!

    Featured Post

    Gigs: Get Your Project Delivered by an Expert

    Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

    Join & Write a Comment

    Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
    Disabling the Directory Sync Service Account in Office 365 will stop directory synchronization from working.
    Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
    Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now