Combining scripts together

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 4 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
Ochness
Posts: 6
Last visit: Wed Oct 28, 2020 3:44 pm

Combining scripts together

Post 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:
  1. $NewUsers = Import-csv C:\New_User_Import\CC_New_Hire_Import_Template_Formulas.csv
  2.  
  3. Clear-Host
  4. Write-Host -object "Enter the username of the user whos groups you would like to copy" -foregroundcolor Green -backgroundcolor Black
  5. $SourceUser = Read-Host
  6. "`n"
  7.  
  8. foreach ($User in $NewUsers)
  9. {
  10.    
  11.     $Username = $User.samaccountname
  12.     $Password = $User.password
  13.     $Firstname = $User.firstname
  14.     $Lastname = $User.lastname
  15.     $Displayname = $User.displayname
  16.     $description = $user.description
  17.     $office = $user.office
  18.     $userprincpalname = $user.userprincpalname
  19.     $OU = $user.oupath
  20.     $homeDirectory = $user.homeDirectory
  21.     $homeDrive = $user.homeDrive
  22.     $Email = $user.email
  23.     $Proxy = $User.proxyaddresses
  24.     $title = $user.title
  25.     $StreetAddress = $User.street
  26.     $City = $User.city
  27.     $State = $User.state
  28.     $PostalCode = $User.zip
  29.    
  30.     #Check if the user account already exists in AD
  31.     if (Get-ADUser -F { SamAccountName -eq $Username })
  32.     {
  33.         #If user does exist, output a warning message
  34.         Write-Warning "A user account $Username already exist in Active Directory."
  35.     }
  36.     else
  37.     {
  38.         #If a user does not exist then create a new user account
  39.         #Account will be created in the OU listed in the $OU variable in the CSV file
  40.        
  41.         New-ADUser `
  42.                    -SamAccountName $Username `
  43.                    -UserPrincipalName $userprincpalname `
  44.                    -Name "$Firstname $Lastname" `
  45.                    -GivenName $Firstname `
  46.                    -Surname $Lastname `
  47.                    -Description $description `
  48.                    -Office $office `
  49.                    -Enabled $True `
  50.                    -ChangePasswordAtLogon $True `
  51.                    -DisplayName $Displayname `
  52.                    -HomeDirectory $homeDirectory `
  53.                    -HomeDrive $homeDrive `
  54.                    -EmailAddress $Email `
  55.                    -Path $OU `
  56.                    -Title $title `
  57.                    -StreetAddress $StreetAddress `
  58.                    -City $City `
  59.                    -State $State `
  60.                    -PostalCode $PostalCode `
  61.                    -AccountPassword (convertto-securestring $Password -AsPlainText -Force)
  62.        
  63.     }
  64.     {
  65.         $SourceGroups = Get-ADUser $SourceUser -Property MemberOf | ForEach-Object
  66.         {
  67.             $_.MemberOf | Get-ADGroup | select Name -ExpandProperty Name | sort name
  68.         }
  69.         foreach ($group in $SourceGroups)
  70.         {
  71.             Add-ADGroupMember -Identity $Group -Members $User
  72.         }
  73.     }
  74. }
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?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Combining scripts together

Post 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?
Ochness
Posts: 6
Last visit: Wed Oct 28, 2020 3:44 pm

Re: Combining scripts together

Post 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)
  1. #Enter a path to your import CSV file
  2. $NewUsers = Import-csv C:\New_User_Import\CC_New_Hire_Import_Template_Formulas.csv
  3.  
  4.  
  5.  
  6. foreach ($User in $NewUsers)
  7. {
  8.    
  9.     $Username = $User.samaccountname
  10.     $Password = $User.password
  11.     $Firstname = $User.firstname
  12.     $Lastname = $User.lastname
  13.     $Displayname = $User.displayname
  14.     $description = $user.description
  15.     $office = $user.office
  16.     $userprincpalname = $user.userprincpalname
  17.     $OU = $user.oupath
  18.     $homeDirectory = $user.homeDirectory
  19.     $homeDrive = $user.homeDrive
  20.     $Email = $user.email
  21.     $Proxy = $User.proxyaddresses
  22.     $title = $user.title
  23.     $StreetAddress = $User.street
  24.     $City = $User.city
  25.     $State = $User.state
  26.     $PostalCode = $User.zip
  27.    
  28.     #Check if the user account already exists in AD
  29.     if (Get-ADUser -F { SamAccountName -eq $Username })
  30.     {
  31.         #If user does exist, output a warning message
  32.         Write-Warning "A user account $Username already exist in Active Directory."
  33.     }
  34.     else
  35.     {
  36.         #If a user does not exist then create a new user account
  37.         #Account will be created in the OU listed in the $OU variable in the CSV file
  38.        
  39.         New-ADUser `
  40.                    -SamAccountName $Username `
  41.                    -UserPrincipalName $userprincpalname `
  42.                    -Name "$Firstname $Lastname" `
  43.                    -GivenName $Firstname `
  44.                    -Surname $Lastname `
  45.                    -Description $description `
  46.                    -Office $office `
  47.                    -Enabled $True `
  48.                    -ChangePasswordAtLogon $True `
  49.                    -DisplayName $Displayname `
  50.                    -HomeDirectory $homeDirectory `
  51.                    -HomeDrive $homeDrive `
  52.                    -EmailAddress $Email `
  53.                    -Path $OU `
  54.                    -Title $title `
  55.                    -StreetAddress $StreetAddress `
  56.                    -City $City `
  57.                    -State $State `
  58.                    -PostalCode $PostalCode `
  59.                    -AccountPassword (convertto-securestring $Password -AsPlainText -Force)
  60.        
  61.     }
Script 2 where I'm adding the new AD users to AD Groups (and this works on it's own)
  1. Clear-Host
  2.         Write-Host -object "Enter the username of the user whos groups you would like to copy" -foregroundcolor Green -backgroundcolor Black
  3.         $SourceUser = Read-Host
  4.         "`n"
  5.        
  6.         $SourceGroups = Get-ADUser $SourceUser -Property MemberOf | ForEach-Object
  7.         {
  8.             $_.MemberOf | Get-ADGroup | select Name -ExpandProperty Name | sort name
  9.         }
  10.         foreach ($group in $SourceGroups)
  11.         {
  12.             Add-ADGroupMember -Identity $Group -Members $User
  13.         }
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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Combining scripts together

Post 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
This topic is 4 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked