PowerShell function to create new computers and add member groups

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
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ryanmavesAuthor Commented:

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??
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..
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

ryanmavesAuthor Commented:
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.
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

ryanmavesAuthor Commented:
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!
You're welcome!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.