Jump to content

[Solved] Powershell script

Pup Shepard
Go to solution Solved by C2dan88,
1 hour ago, Pup Shepard said:

  I was hoping that if the user wrote teacher it would write to $path.  Then I could do something like, $($path)  and that become the desired variable.

Try using get-variable like this

$teacher = "OU=Teachers,OU=Educators,OU=Alpha,DC=REDACTED,DC=REDACTED"
$student = "OU=Students,DC=REDACTED,DC=REDACTED"

$type = Read-Host -Prompt "Type student or Type teacher"
$path = (get-variable $type).value
Write-Output "Path is: $path"

 

But I think it would be much safer to use a simple if statement.

if ($type -eq "student") {
	$path = $student
} else if ($path_type -eq "teacher") {
	$path = $teacher
} else {
	Write-Output "Incorect value entered. Only student or teacher permitted"
}

 

I am writing a powershell script to make new users in our AD.  The workflow I am currently working on.

 

* Prompt asking what the user is, student, teacher, or ta

* based on that selection insert the distinguished name correctly into the script.

 

The code below is such that I have a script for each OU I want the user to go into.  What I was trying to do was prompt for teacher, student, or ta.  Each of those a variable with the corresponding OU ($teacher, $student, $ta).  Then I made another variable ($path).  I was hoping that if the user wrote teacher it would write to $path.  Then I could do something like, $($path)  and that become the desired variable.  But the script just output "$ teacher" rather than treat it like a variable.

 

I also want to do the same with groups.  Where the user gets added to different groups based on who they are.  But I will tackle that after figuring out how to achieve this goal.

$name = Read-Host -Prompt "Enter Username."
$surname = Read-Host -Prompt "Enter Last Name"
$givenname = Read-Host -Prompt "Enter First Name"
#$email = Read-Host -Prompt "Enter Email."
#$teacher = "OU=Teachers,OU=Educators,OU=Alpha,DC=REDACTED,DC=REDACTED"
#$student = "OU=Students,DC=REDACTED,DC=REDACTED"
#$path = Read-Host -Prompt "Type student or Type teacher"
#Write-Output "$""$path"


New-ADUser -Name "$name" -GivenName "$givenname" -Surname "$surname" -EmailAddress $name"@REDACTED.com" –OtherAttributes @{‘ProxyAddresses’='SMTP:' + $name + '@REDACTED.com'} -PasswordNeverExpires 1 -Accountpassword (Read-Host -AsSecureString "Enter Password") -Enabled $true  -Path "OU=Students,DC=REDACTED,DC=REDACTED"

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Pup Shepard said:

  I was hoping that if the user wrote teacher it would write to $path.  Then I could do something like, $($path)  and that become the desired variable.

Try using get-variable like this

$teacher = "OU=Teachers,OU=Educators,OU=Alpha,DC=REDACTED,DC=REDACTED"
$student = "OU=Students,DC=REDACTED,DC=REDACTED"

$type = Read-Host -Prompt "Type student or Type teacher"
$path = (get-variable $type).value
Write-Output "Path is: $path"

 

But I think it would be much safer to use a simple if statement.

if ($type -eq "student") {
	$path = $student
} else if ($path_type -eq "teacher") {
	$path = $teacher
} else {
	Write-Output "Incorect value entered. Only student or teacher permitted"
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

Thank you, I was able to integrate that into the script

 

#Create Var name and prompt user to enter a username
$name = Read-Host -Prompt "Enter Username."

#check to see if the username already exist and exit if yes
$AdUser = Get-ADUser -Filter {SamAccountName -eq $name}
if ($AdUser -eq $null) {Write-Host "Username avalible, please continue on."}
else  {Write-Host "Username already exists. Please Try Again."; exit}

#create var's for first and last name and prompt user to enter them
$surname = Read-Host -Prompt "Enter Last Name"
$givenname = Read-Host -Prompt "Enter First Name"

#create var's for TAs, Teachers, and students.  Assign ther values as the OU locations for each type of user.
$teacher = "OU=Teachers,OU=Educators,OU=Alpha,DC=REDACTED,DC=REDACTED"
$ta = "OU=Teacher Assistants,OU=Educators,OU=Alpha,DC=REDACTED,DC=REDACTED"
$student = "OU=Students,DC=REDACTED,DC=REDACTED"

#take the input of the use and assign the ou location to the var $type.  If the wrong value is typed, exit.
$type = Read-Host -Prompt "Type student, ta, or teacher"
if ($type -eq "student") {$path = $student}
elseIf ($type -eq "teacher") {$path = $teacher}
elseIf ($type -eq "ta") {$path = $ta}
else {$path = $null; Write-Output "Incorect value entered. Only student or teacher permitted"; exit}

#create the new user based on above values.
New-ADUser -Name "$name" -GivenName "$givenname" -Surname "$surname" -DisplayName "$givenname $surname" -EmailAddress $name"@REDACTED.com" –OtherAttributes @{‘ProxyAddresses’='SMTP:' + $name + '@REDACTED.com'} -PasswordNeverExpires 1 -Accountpassword (Read-Host -AsSecureString "Enter Password") -Enabled $true  -Path $path

#Add the user to groups based on if they are a ta, teacher, or student.  Rename their CN name to their first and last name.
if ($type -eq "teacher") {Add-ADGroupMember -Identity Teachers -Members $name; Add-ADGroupMember -Identity SMS_Teacher -Members $name; Add-ADGroupMember -Identity vpnusers -Members $name; Rename-ADObject -Identity "CN=$name,OU=Teachers,OU=Educators,OU=Alpha,DC=REDACTED,DC=REDACTED" "$givenname $surname"}
elseif ($type -eq "ta"){Add-ADGroupMember -Identity 'Teaching Assistants' -Members $name; Add-ADGroupMember -Identity SMS_Aides -Members $name; Add-ADGroupMember -Identity vpnusers -Members $name; Rename-ADObject -Identity "CN=$name,OU=Teacher Assistants,OU=Educators,OU=Alpha,DC=REDACTED,DC=REDACTED" "$givenname $surname"}
elseif ($type -eq "student"){Add-ADGroupMember -Identity Students; Rename-ADObject -Identity "CN=$name,OU=Students,DC=REDACTED,DC=REDACTED" "$givenname $surname"}
else {exit}

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×