Combining scripts together

Ask your Windows 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.
Locked
Ochness
Posts: 4
Joined: Fri Feb 15, 2019 7:12 pm

Combining scripts together

Post by Ochness » Thu May 16, 2019 2:54 pm

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.  
  4.  
  5. Clear-Host
  6.  
  7. Write-Host -object "Enter the username of the user whos groups you would like to copy" -foregroundcolor Green -backgroundcolor Black
  8.  
  9. $SourceUser = Read-Host
  10.  
  11. "`n"
  12.  
  13.  
  14.  
  15. foreach ($User in $NewUsers)
  16.  
  17. {
  18.  
  19.    
  20.  
  21.     $Username = $User.samaccountname
  22.  
  23.     $Password = $User.password
  24.  
  25.     $Firstname = $User.firstname
  26.  
  27.     $Lastname = $User.lastname
  28.  
  29.     $Displayname = $User.displayname
  30.  
  31.     $description = $user.description
  32.  
  33.     $office = $user.office
  34.  
  35.     $userprincpalname = $user.userprincpalname
  36.  
  37.     $OU = $user.oupath
  38.  
  39.     $homeDirectory = $user.homeDirectory
  40.  
  41.     $homeDrive = $user.homeDrive
  42.  
  43.     $Email = $user.email
  44.  
  45.     $Proxy = $User.proxyaddresses
  46.  
  47.     $title = $user.title
  48.  
  49.     $StreetAddress = $User.street
  50.  
  51.     $City = $User.city
  52.  
  53.     $State = $User.state
  54.  
  55.     $PostalCode = $User.zip
  56.  
  57.    
  58.  
  59.     #Check if the user account already exists in AD
  60.  
  61.     if (Get-ADUser -F { SamAccountName -eq $Username })
  62.  
  63.     {
  64.  
  65.         #If user does exist, output a warning message
  66.  
  67.         Write-Warning "A user account $Username already exist in Active Directory."
  68.  
  69.     }
  70.  
  71.     else
  72.  
  73.     {
  74.  
  75.         #If a user does not exist then create a new user account
  76.  
  77.         #Account will be created in the OU listed in the $OU variable in the CSV file
  78.  
  79.        
  80.  
  81.         New-ADUser `
  82.  
  83.                    -SamAccountName $Username `
  84.  
  85.                    -UserPrincipalName $userprincpalname `
  86.  
  87.                    -Name "$Firstname $Lastname" `
  88.  
  89.                    -GivenName $Firstname `
  90.  
  91.                    -Surname $Lastname `
  92.  
  93.                    -Description $description `
  94.  
  95.                    -Office $office `
  96.  
  97.                    -Enabled $True `
  98.  
  99.                    -ChangePasswordAtLogon $True `
  100.  
  101.                    -DisplayName $Displayname `
  102.  
  103.                    -HomeDirectory $homeDirectory `
  104.  
  105.                    -HomeDrive $homeDrive `
  106.  
  107.                    -EmailAddress $Email `
  108.  
  109.                    -Path $OU `
  110.  
  111.                    -Title $title `
  112.  
  113.                    -StreetAddress $StreetAddress `
  114.  
  115.                    -City $City `
  116.  
  117.                    -State $State `
  118.  
  119.                    -PostalCode $PostalCode `
  120.  
  121.                    -AccountPassword (convertto-securestring $Password -AsPlainText -Force)
  122.  
  123.        
  124.  
  125.     }
  126.  
  127.     {
  128.  
  129.         $SourceGroups = Get-ADUser $SourceUser -Property MemberOf | ForEach-Object
  130.  
  131.         {
  132.  
  133.             $_.MemberOf | Get-ADGroup | select Name -ExpandProperty Name | sort name
  134.  
  135.         }
  136.  
  137.         foreach ($group in $SourceGroups)
  138.  
  139.         {
  140.  
  141.             Add-ADGroupMember -Identity $Group -Members $User
  142.  
  143.         }
  144.  
  145.     }
  146.  
  147. }
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: 14018
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Combining scripts together

Post by jvierra » Thu May 16, 2019 3:17 pm

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: 4
Joined: Fri Feb 15, 2019 7:12 pm

Re: Combining scripts together

Post by Ochness » Fri May 17, 2019 8:26 am

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.  
  3. $NewUsers = Import-csv C:\New_User_Import\CC_New_Hire_Import_Template_Formulas.csv
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11. foreach ($User in $NewUsers)
  12.  
  13. {
  14.  
  15.    
  16.  
  17.     $Username = $User.samaccountname
  18.  
  19.     $Password = $User.password
  20.  
  21.     $Firstname = $User.firstname
  22.  
  23.     $Lastname = $User.lastname
  24.  
  25.     $Displayname = $User.displayname
  26.  
  27.     $description = $user.description
  28.  
  29.     $office = $user.office
  30.  
  31.     $userprincpalname = $user.userprincpalname
  32.  
  33.     $OU = $user.oupath
  34.  
  35.     $homeDirectory = $user.homeDirectory
  36.  
  37.     $homeDrive = $user.homeDrive
  38.  
  39.     $Email = $user.email
  40.  
  41.     $Proxy = $User.proxyaddresses
  42.  
  43.     $title = $user.title
  44.  
  45.     $StreetAddress = $User.street
  46.  
  47.     $City = $User.city
  48.  
  49.     $State = $User.state
  50.  
  51.     $PostalCode = $User.zip
  52.  
  53.    
  54.  
  55.     #Check if the user account already exists in AD
  56.  
  57.     if (Get-ADUser -F { SamAccountName -eq $Username })
  58.  
  59.     {
  60.  
  61.         #If user does exist, output a warning message
  62.  
  63.         Write-Warning "A user account $Username already exist in Active Directory."
  64.  
  65.     }
  66.  
  67.     else
  68.  
  69.     {
  70.  
  71.         #If a user does not exist then create a new user account
  72.  
  73.         #Account will be created in the OU listed in the $OU variable in the CSV file
  74.  
  75.        
  76.  
  77.         New-ADUser `
  78.  
  79.                    -SamAccountName $Username `
  80.  
  81.                    -UserPrincipalName $userprincpalname `
  82.  
  83.                    -Name "$Firstname $Lastname" `
  84.  
  85.                    -GivenName $Firstname `
  86.  
  87.                    -Surname $Lastname `
  88.  
  89.                    -Description $description `
  90.  
  91.                    -Office $office `
  92.  
  93.                    -Enabled $True `
  94.  
  95.                    -ChangePasswordAtLogon $True `
  96.  
  97.                    -DisplayName $Displayname `
  98.  
  99.                    -HomeDirectory $homeDirectory `
  100.  
  101.                    -HomeDrive $homeDrive `
  102.  
  103.                    -EmailAddress $Email `
  104.  
  105.                    -Path $OU `
  106.  
  107.                    -Title $title `
  108.  
  109.                    -StreetAddress $StreetAddress `
  110.  
  111.                    -City $City `
  112.  
  113.                    -State $State `
  114.  
  115.                    -PostalCode $PostalCode `
  116.  
  117.                    -AccountPassword (convertto-securestring $Password -AsPlainText -Force)
  118.  
  119.        
  120.  
  121.     }
Script 2 where I'm adding the new AD users to AD Groups (and this works on it's own)
  1. Clear-Host
  2.  
  3.         Write-Host -object "Enter the username of the user whos groups you would like to copy" -foregroundcolor Green -backgroundcolor Black
  4.  
  5.         $SourceUser = Read-Host
  6.  
  7.         "`n"
  8.  
  9.        
  10.  
  11.         $SourceGroups = Get-ADUser $SourceUser -Property MemberOf | ForEach-Object
  12.  
  13.         {
  14.  
  15.             $_.MemberOf | Get-ADGroup | select Name -ExpandProperty Name | sort name
  16.  
  17.         }
  18.  
  19.         foreach ($group in $SourceGroups)
  20.  
  21.         {
  22.  
  23.             Add-ADGroupMember -Identity $Group -Members $User
  24.  
  25.         }
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: 14018
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Combining scripts together

Post by jvierra » Fri May 17, 2019 11:20 am

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

Locked