[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PowerShell function to create new computers and add member groups

Posted on 2014-08-13
7
Medium Priority
?
366 Views
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
0
Comment
Question by:ryanmaves
  • 4
  • 3
7 Comments
 
LVL 40

Accepted Solution

by:
Subsun earned 2000 total points
ID: 40259948
Try with..
$C = Get-ADComputer -Filter 'Name -like $Computer' -SearchBase $OUC

Open in new window

0
 

Author Comment

by:ryanmaves
ID: 40259965
@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!
0
 
LVL 40

Assisted Solution

by:Subsun
Subsun earned 2000 total points
ID: 40260034
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..
http://blogs.msdn.com/b/powershell/archive/2007/04/14/controlling-the-scope-of-variables.aspx
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 

Author Comment

by:ryanmaves
ID: 40260969
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.
ee-help.gifThanks!
0
 
LVL 40

Expert Comment

by:Subsun
ID: 40261017
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

0
 

Author Closing Comment

by:ryanmaves
ID: 40261139
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!
0
 
LVL 40

Expert Comment

by:Subsun
ID: 40261154
You're welcome!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After seeing many questions for JRNL_WRAP_ERROR for replication failure, I thought it would be useful to write this article.
How to deal with a specific error when using the Enable-RemoteMailbox cmdlet to create a mailbox in the cloud-based service, for an existing user in an on-premises Active Directory.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Loops Section Overview
Suggested Courses
Course of the Month18 days, 13 hours left to enroll

834 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