Combining Powershell Scripts RRS feed

  • General discussion

  • Hi there please can you help i have two powershell scripts that work individually but, when i try running them together I get an error on the first one.  

    the first script creates AD users the second Script creates homedectory and folder.   I new to powershell so i dont fully understand why the work on their own but not together.

    This is the error message:

    param : The term 'param' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the 
    path is correct and try again.
    At C:\shares\demos\setup\New user Test.ps1:78 char:1
    + param([Parameter(Mandatory=$true)][String]$samAccountName)
    + ~~~~~
        + CategoryInfo          : ObjectNotFound: (param:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException

    This is the first script 

    Import-Module activedirectory
    #Store the data from ADUsers.csv in the $ADUsers variable
    $ADUsers = Import-csv C:\shares\demos\setup\NewUser_Accounts.csv

    #Loop through each row containing user details in the CSV file 
    foreach ($User in $ADUsers)
    #Read user data from each field in each row and assign the data to a variable as below

    $Username = $User.username
    $Password = $User.password
    $Firstname = $User.firstname
    $Lastname = $User.lastname
    $OU = "$Use.OU"
        $email      = $User.email
        $streetaddress = $User.streetaddress
        $city       = $User.city
        $zipcode    = $User.zipcode
        $state      = $User.state
        $country    = $User.country
        $telephone  = $User.telephone
        $jobtitle   = $User.jobtitle
        $company    = $User.company
        $department = $User.department
        $l = $user.Office
        $Password = $User.Password

    #Check to see if the user already exists in AD
    if (Get-ADUser -F {SamAccountName -eq $Username})
    #If user does exist, give a warning
    Write-Warning "A user account with username $Username already exist in Active Directory."
    #User does not exist then proceed to create the new user account

            #Account will be created in the OU provided by the $OU variable read from the CSV file
    New-ADUser `
                -SamAccountName $Username `
                -UserPrincipalName "$Username@wiredbrain.priv" `
                -Name "$Firstname $Lastname" `
                -GivenName $Firstname `
                -Surname $Lastname `
                -Enabled $True `
                -DisplayName "$Lastname, $Firstname" `
                -Path $User.ou `
                -City $city `
                -Company $company `
                -State $state `
                -StreetAddress $streetaddress `
                -OfficePhone $telephone `
                -EmailAddress $email `
                -Title $jobtitle `
                -Department $department `
                -office $l `
                -AccountPassword (convertto-securestring $Password -AsPlainText -Force) -ChangePasswordAtLogon $True

    Then I add the create home directory script below it  ( it dosnt like both scrpts running all at once ) 

    #This Script sets creates an home drive folder and then sets the drive letter and folder path

    $fullPath = "c:\shares\demos\setup\Home01\ {0}" -f $samAccountName
    $driveLetter = "M:"

    $User = Get-ADUser -Identity $samAccountName

    if($User -ne $Null) {
        Set-ADUser $User -HomeDrive $driveLetter -HomeDirectory $fullPath -ea Stop
        $homeShare = New-Item -path $fullPath -ItemType Directory -force -ea Stop

        $acl = Get-Acl $homeShare

        $FileSystemRights = [System.Security.AccessControl.FileSystemRights]"Modify"
        $AccessControlType = [System.Security.AccessControl.AccessControlType]::Allow
        $InheritanceFlags = [System.Security.AccessControl.InheritanceFlags]"ContainerInherit, ObjectInherit"
        $PropagationFlags = [System.Security.AccessControl.PropagationFlags]"InheritOnly"

        $AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule ($User.SID, $FileSystemRights, $InheritanceFlags, $PropagationFlags, $AccessControlType)

        Set-Acl -Path $homeShare -AclObject $acl -ea Stop

        Write-Host ("HomeDirectory created at {0}" -f $fullPath)

    thanks for you advice 



    • Changed type Bill_Stewart Wednesday, May 9, 2018 2:42 PM
    • Moved by Bill_Stewart Wednesday, May 9, 2018 2:42 PM This is not "merge scripts for me" forum
    Tuesday, March 20, 2018 5:36 PM

All replies

  • i dont fully understand why the work on their own but not together.

    Because you can't copy-and-paste two scripts together and just expect it to somehow magically work.

    In your case, you are much better off creating a separate script that runs the two working scripts.

    -- Bill Stewart [Bill_Stewart]

    Tuesday, March 20, 2018 6:01 PM
  • To add:  Doing what you ask would require a complete re-write of both scripts.  The issue with running them from a third script has an issue as the second script runs for one user account and the first is a file driven batch.

    I suggest using this as an excuse to sit down and learn PowerShell.  By the time you understand how to do this you will be very good at PowerShell.

    These resources will get you going.

    Script Gallery.

    Script Center

    Learn PowerShell  


    Tuesday, March 20, 2018 11:14 PM
  • Thanks for the advise Bill 

    I will take on board what you said

    Wednesday, March 21, 2018 4:45 PM