Export , Import Powershell

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 5 months ago.

  • Author
    Posts
  • #6321

    by deliyurek007 at 2013-04-13 13:20:54

    Hi Guys,

    I have a maybe simple question maybe not.

    I have a simple job that Export users form that Specific OU from Active Directory like:
    Get-QADUser -SearchRoot 'ExternUsers' -SizeLimit 0 | Select-Object -Property Name,SamAccountName,Description,EmailAddress,Manager,Title,Department,Company, | Sort-Object -Property Name | Export-Csv c:\ADUsers.csv

    After Modify The CSV file, Just want to Import in a different OU. But how can I make a script to import the same file what I modify the settings.

    I Foun a Powershell script to import CSV file like:
    ###########################################################
    # AUTHOR : Marius / Hican – http://www.hican.nl – @hicannl
    # DATE : 26-04-2012
    # COMMENT : This script creates new Active Directory users
    # including different kind of properties based
    # on an input_create_ad_users.csv.
    ###########################################################
    Import-Module ActiveDirectory
    # Get current directory and set import file in variable
    $path = Split-Path -parent $MyInvocation.MyCommand.Definition
    $newpath = $path + "C:\ADUsers.csv"
    # Define variables
    $log = $path + "C:\ADUsers.log"
    $date = Get-Date
    $i = 0
    # Change this to the location you want the users to be created in your AD
    $location = "OU=Gebruikers,DC=Extern,DC=Intra"
    # FUNCTIONS
    Function createUsers
    {
    "Created following users (on " + $date + "): " | Out-File $log -append
    "——————————————–" | Out-File $log -append
    Import-CSV $newpath | ForEach-Object {
    # A check for the country, because those were full names and need
    # to be landcodes in order for AD to accept them. I used Netherlands
    # as example
    If($_.CO -eq "Netherlands")
    {
    $_.CO = "NL"
    }
    # Replace dots / points (.) in names, because AD will error when a
    # name ends with a dot (and it looks cleaner as well)
    $replace = $_.CN.Replace(".","")
    If($replace.length -lt 4)
    {
    $lastname = $replace
    }
    Else
    {
    $lastname = $replace.substring(0,4)
    }
    # Create sAMAccountName according to this 'naming convention':
    # for example
    # hhica
    $sam = $_.Initials.substring(0,1).ToLower() + $lastname.ToLower()
    Try { $exists = Get-ADUser -LDAPFilter "(sAMAccountName=$sam)" }
    Catch { }
    If(!$exists)
    {
    $i++
    # Set all variables according to the table names in the Excel
    # sheet / import CSV. The names can differ in every project, but
    # if the names change, make sure to change it below as well.
    $setpass = ConvertTo-SecureString -AsPlainText $_.Password -force
    New-ADUser $sam -GivenName $_.GivenName -Initials $_.Initials `
    -Surname $_.SN -DisplayName $_.DisplayName -Office $_.OfficeName `
    -Description $_.Description -EmailAddress $_.Mail `
    -StreetAddress $_.StreetAddress -City $_.L `
    -PostalCode $_.PostalCode -Country $_.CO -UserPrincipalName $_.UPN `
    -Company $_.Company -Department $_.Department -EmployeeID $_.ID `
    -Title $_.Title -OfficePhone $_.Phone -AccountPassword $setpass

    # Set an ExtensionAttribute
    $dn = (Get-ADUser $sam).DistinguishedName
    $ext = [ADSI]"LDAP://$dn"
    $ext.Put("extensionAttribute1", $_.ExtensionAttribute1)
    $ext.SetInfo()

    # Move the user to the OU you set above. If you don't want to
    # move the user(s) and just create them in the global Users
    # OU, comment the string below
    Move-ADObject -Identity $dn -TargetPath $location

    # Rename the object to a good looking name (otherwise you see
    # the 'ugly' shortened sAMAccountNames as a name in AD. This
    # can't be set right away (as sAMAccountName) due to the 20
    # character restriction
    $newdn = (Get-ADUser $sam).DistinguishedName
    Rename-ADObject -Identity $newdn -NewName $_.CN

    $output = $i.ToString() + ") Name: " + $_.CN + " sAMAccountName: "
    $output += $sam + " Pass: " + $_.Password
    $output | Out-File $log -append
    }
    Else
    {
    "SKIPPED – ALREADY EXISTS OR ERROR: " + $_.CN | Out-File $log -append
    }
    }
    "—————————————-" + "`n" | Out-File $log -append
    }
    # RUN SCRIPT
    createUsers
    #Finished

    But When I run the script I get the following Error:
    Missing expression after unary operator '-'.
    At C:\Jobs\Import.ps1:56 char:8
    + – < <<< Surname $_.SN -DisplayName $_.DisplayName -Office $_.OfficeName `
    + CategoryInfo : ParserError: (-:String) [], ParseException
    + FullyQualifiedErrorId : MissingExpressionAfterOperator

    Does somebody have a advice for me what is the best way to do this to Export and Import the same file from a specific OU (organization Unit). I am still learning but sometime I don't have any clue on it.

    Kind regards,
    deliyurek

    by ps_gregg at 2013-04-13 19:37:52

    Hi Deliyurek,

    Your export looks good but before you import you will need to make sure that the samAccountName is unique to successfully import the object. So "after you modify the CSV file" your import should be successful if you use the New-QADUser cmdlet.

    #Your original import
    Get-QADUser -SearchRoot 'ExternUsers' -SizeLimit 0 | Select-Object -Property Name,SamAccountName,Description,EmailAddress,Manager,Title,Department,Company, |
    Sort-Object -Property Name | Export-Csv c:\ADUsers.csv

    #

    # Import the CSV and use foreach-object to run the New-QADUser cmdlet, specifying the properties from the CSV and a new default ParentContainer.
    # added UserPassword in case your AD requires it
    Import-Csv c:\ADUsers.csv | ForEach-Object { New-QADUser -Name $_.name -SamAccountName $_.SamAccountName -Description $_.Description -Email $_.Email -Manager $_.Manager `
    -Title $_.Title -Department $_.Department -Company $_.Company -ParentContainer 'OU=companyOU,DC=company,DC=com' -UserPassword 'P@ssword' }
    #

    Hope that helps.

    -Gregg

    by deliyurek007 at 2013-04-21 05:50:38

    Sorry for delay I was sick.
    I try to run the job after modify settings like department or phone number example.

    Then I try to import on a different OU (Test domain) with no Users are created (empty OU) then I get tis like:

    New-QADUser : A positional parameter cannot be found that accepts argument ' -Title'.
    At line:1 char:58
    + Import-Csv c:\ADUsers.csv | ForEach-Object { New-QADUser < <<< -Name $_.name -SamAccountName $_.SamAccountName -Desc
    ription $_.Description -Email $_.Email -Manager $_.Manager ` -Title $_.Title -Department $_.Department -Company $_.Comp
    any -ParentContainer 'OU=Gebruiker,DC=Extern,DC=Intra' -UserPassword 'P@ssword' }
    + CategoryInfo : InvalidArgument: (:) [New-QADUser], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Quest.ActiveRoles.ArsPowerShellSnapIn.Powershell.Cmdlets.New
    UserCmdlet

    New-QADUser : A positional parameter cannot be found that accepts argument ' -Title'.
    At line:1 char:58
    + Import-Csv c:\ADUsers.csv | ForEach-Object { New-QADUser < <<< -Name $_.name -SamAccountName $_.SamAccountName -Desc
    ription $_.Description -Email $_.Email -Manager $_.Manager ` -Title $_.Title -Department $_.Department -Company $_.Comp
    any -ParentContainer 'OU=Gebruiker,DC=Extern,DC=Intra' -UserPassword 'P@ssword' }
    + CategoryInfo : InvalidArgument: (:) [New-QADUser], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Quest.ActiveRoles.ArsPowerShellSnapIn.Powershell.Cmdlets.New
    UserCmdlet

    Any Idea I try to modify but still hangs on that part.

    When I try like:
    Import-Csv c:\ADUsers.csv | ForEach-Object { New-QADUser -Name $_.name -SamAccountName $_.SamAccountName -ParentContainer 'OU=Gebruiker,DC=Extern,DC=Intra' -UserPassword 'P@ssword' }

    That works user are created but no any information are created.

    Kind regards,
    Deliyurek007

    by Slashj at 2013-04-22 02:02:46

    Hello,

    Try to run the New-Qaduser command in one line, instead of breaking it into 2.

    Br

You must be logged in to reply to this topic.