Page 1 of 1

Combining scripts together

Posted: Thu May 16, 2019 2:54 pm
by Ochness
I am pretty new to PowerShell. I am trying to combine 2 different scripts that I have to run in one script.

Here is my code:

Code: Select all

$NewUsers = Import-csv C:\New_User_Import\CC_New_Hire_Import_Template_Formulas.csv

Clear-Host
Write-Host -object "Enter the username of the user whos groups you would like to copy" -foregroundcolor Green -backgroundcolor Black
$SourceUser = Read-Host
"`n"

foreach ($User in $NewUsers)
{

$Username = $User.samaccountname
$Password = $User.password
$Firstname = $User.firstname
$Lastname = $User.lastname
$Displayname = $User.displayname
$description = $user.description
$office = $user.office
$userprincpalname = $user.userprincpalname
$OU = $user.oupath
$homeDirectory = $user.homeDirectory
$homeDrive = $user.homeDrive
$Email = $user.email
$Proxy = $User.proxyaddresses
$title = $user.title
$StreetAddress = $User.street
$City = $User.city
$State = $User.state
$PostalCode = $User.zip

#Check if the user account already exists in AD
if (Get-ADUser -F { SamAccountName -eq $Username })
{
#If user does exist, output a warning message
Write-Warning "A user account $Username already exist in Active Directory."
}
else
{
#If a user does not exist then create a new user account
#Account will be created in the OU listed in the $OU variable in the CSV file

New-ADUser `
-SamAccountName $Username `
-UserPrincipalName $userprincpalname `
-Name "$Firstname $Lastname" `
-GivenName $Firstname `
-Surname $Lastname `
-Description $description `
-Office $office `
-Enabled $True `
-ChangePasswordAtLogon $True `
-DisplayName $Displayname `
-HomeDirectory $homeDirectory `
-HomeDrive $homeDrive `
-EmailAddress $Email `
-Path $OU `
-Title $title `
-StreetAddress $StreetAddress `
-City $City `
-State $State `
-PostalCode $PostalCode `
-AccountPassword (convertto-securestring $Password -AsPlainText -Force)

}
{
$SourceGroups = Get-ADUser $SourceUser -Property MemberOf | ForEach-Object
{
$_.MemberOf | Get-ADGroup | select Name -ExpandProperty Name | sort name
}
foreach ($group in $SourceGroups)
{
Add-ADGroupMember -Identity $Group -Members $User
}
}
}
I am trying to create a new AD user and then assign groups based on an existing user in the OU that the new users were created in. Each of these scripts work on their own, but I want to be able to run them as one script. The script shown above asks for the user to copy their "memberof", creates the accounts based off the CSV but then just writes the last bit of code (that is to add the groups) to the output section.

How do I combine these 2 functions?

Re: Combining scripts together

Posted: Thu May 16, 2019 3:17 pm
by jvierra
Sorry but I do not understand your issue. You posted only one script. What is the issue and what are the full errors?

Re: Combining scripts together

Posted: Fri May 17, 2019 8:26 am
by Ochness
I did only post one script...but it's the two scripts I'm trying to combine. Let me separate them...

Script 1 that adds a new user to AD (and this works by itself)

Code: Select all

#Enter a path to your import CSV file
$NewUsers = Import-csv C:\New_User_Import\CC_New_Hire_Import_Template_Formulas.csv



foreach ($User in $NewUsers)
{

$Username = $User.samaccountname
$Password = $User.password
$Firstname = $User.firstname
$Lastname = $User.lastname
$Displayname = $User.displayname
$description = $user.description
$office = $user.office
$userprincpalname = $user.userprincpalname
$OU = $user.oupath
$homeDirectory = $user.homeDirectory
$homeDrive = $user.homeDrive
$Email = $user.email
$Proxy = $User.proxyaddresses
$title = $user.title
$StreetAddress = $User.street
$City = $User.city
$State = $User.state
$PostalCode = $User.zip

#Check if the user account already exists in AD
if (Get-ADUser -F { SamAccountName -eq $Username })
{
#If user does exist, output a warning message
Write-Warning "A user account $Username already exist in Active Directory."
}
else
{
#If a user does not exist then create a new user account
#Account will be created in the OU listed in the $OU variable in the CSV file

New-ADUser `
-SamAccountName $Username `
-UserPrincipalName $userprincpalname `
-Name "$Firstname $Lastname" `
-GivenName $Firstname `
-Surname $Lastname `
-Description $description `
-Office $office `
-Enabled $True `
-ChangePasswordAtLogon $True `
-DisplayName $Displayname `
-HomeDirectory $homeDirectory `
-HomeDrive $homeDrive `
-EmailAddress $Email `
-Path $OU `
-Title $title `
-StreetAddress $StreetAddress `
-City $City `
-State $State `
-PostalCode $PostalCode `
-AccountPassword (convertto-securestring $Password -AsPlainText -Force)

}
Script 2 where I'm adding the new AD users to AD Groups (and this works on it's own)

Code: Select all

Clear-Host
Write-Host -object "Enter the username of the user whos groups you would like to copy" -foregroundcolor Green -backgroundcolor Black
$SourceUser = Read-Host
"`n"

$SourceGroups = Get-ADUser $SourceUser -Property MemberOf | ForEach-Object
{
$_.MemberOf | Get-ADGroup | select Name -ExpandProperty Name | sort name
}
foreach ($group in $SourceGroups)
{
Add-ADGroupMember -Identity $Group -Members $User
}
What I'm trying to do is to combine both of these scripts into 1 script so I only have to have 1 CSV file and it performs the New-ADUser and Add-ADGroupMember all within 1 script.

When I run the scripts together (from the Sapien Studio window) like I have in the first code block on my original post the portion of the script that does the New-ADUser creates the new user(s) but in the Output section of the Sapien Studio it just prints the following:

Clear-Host
Write-Host -object "Enter the username of the user whos groups you would like to copy" -foregroundcolor Green -backgroundcolor Black
$SourceUser = Read-Host
"`n"

$SourceGroups = Get-ADUser $SourceUser -Property MemberOf | ForEach-Object
{
$_.MemberOf | Get-ADGroup | select Name -ExpandProperty Name | sort name
}
foreach ($group in $SourceGroups)
{
Add-ADGroupMember -Identity $Group -Members $User
}

So it doesn't run the second part of the script to add the new user(s) to the groups.

Re: Combining scripts together

Posted: Fri May 17, 2019 11:20 am
by jvierra
You have to add the user to the group inside the loop where you are creating new users.

Take some time to learn a bit about PowerShell before trying to copy and modify complex scripts.

You can get some basic knowledge by watching the YouTube videos here: https://www.youtube.com/user/SAPIENTech ... _polymer=1

Here is a better full tutorial from Microsoft: https://mva.microsoft.com/en-us/trainin ... 2304984382