Link to home
Start Free TrialLog in
Avatar of ryanmaves
ryanmaves

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of SubSun
SubSun
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ryanmaves
ryanmaves

ASKER

@Subsun

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??
Thanks!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
User generated imageThanks!
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

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

Open in new window

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!