export results

This topic contains 9 replies, has 5 voices, and was last updated by Profile photo of barbar barbar 5 months, 3 weeks ago.

  • Author
    Posts
  • #63193
    Profile photo of barbar
    barbar
    Participant

    hello there ...
    i have this simple script

    $NewPassword = -join ((97..122) + (48..57) | Get-Random -Count 10 | % {[char]$_})
    $OU = "OU=TEST,OU=TEST,DC=TEST,DC=TEST,DC=TEST,DC=TEST"
    Import-Csv D:\a.csv | foreach { New-ADUser -Name $_.Name -Enabled $true `
    -AccountPassword(ConvertTo-SecureString -AsPlainText $NewPassword -Force )` -SamAccountName $_.SamAccountName` -Path $OU } 

    the csv column contains three columns ... column1 = name ... column2 = samaccountname .. column3 = password
    the password column in empty...
    how can i export the password the generated password into the third column ? if its not possible ... how can i export the results into another csv file with the generated password ?
    thank you

  • #63199
    Profile photo of BlackBoxCoder
    BlackBoxCoder
    Participant

    Hi,

    you just read from the CSV file, but you have to write to the csv file.

    My suggest: Generate the passwords, put them in the CSV-File after this Import the CSV Files and and create the ADuser

    BR,
    BBC

    • #63204
      Profile photo of barbar
      barbar
      Participant
      $NewPassword = -join ((97..122) + (48..57) | Get-Random -Count 10 | % {[char]$_})
      $OU = "OU=TEST,OU=TEST,DC=TEST,DC=TEST,DC=TEST,DC=TEST"
      $csv = Import-Csv d:\a.csv
      $csv | foreach { New-ADUser -Name $_.Name -Enabled $true `
      -AccountPassword(ConvertTo-SecureString -AsPlainText $NewPassword -Force )` -SamAccountName $_.SamAccountName` -Path $OU }
      $csv | Select-Object *,@{'Name'='Password';'Expression'={$NewPassword}} | Export-Csv -Path d:\b.csv -NoTypeInformation -Force

      this did the job perfectly ... it exported the results to a new csv file with the generated password .

    • #63207
      Profile photo of BlackBoxCoder
      BlackBoxCoder
      Participant

      This is also a possible way.

      Cheers mate.

  • #63210
    Profile photo of Ron
    Ron
    Participant

    Something like this should work, but it is untested.

    $OU = "OU=TEST,OU=TEST,DC=TEST,DC=TEST,DC=TEST,DC=TEST"
    $csv = Import-Csv D:\a.csv
    foreach ($u in $csv) {
    	$u.Password = -join ((97 .. 122) + (48 .. 57) | Get-Random -Count 10 | % { [char]$_ })
    	Try {
    		New-ADUser -Name $u.Name -Enabled $true -AccountPassword (ConvertTo-SecureString -AsPlainText $u.Password -Force)` -SamAccountName $u.SamAccountName` -Path $OU
    	} Catch {
    		Write-Error "$u.Name failed."
    		$u.Password = ""
    	}
    	$csv | Export-Csv d:\b.csv -NoTypeInformation
    }

    You can move the export outside of the loop if your input file is huge, I just like to make sure it updates after each loop. If any of the users fail, the password will be blank and you'll be able to restart the script using the updated file after you fix the problem.

    • #63216
      Profile photo of barbar
      barbar
      Participant

      well thats actually a good idea ...
      but i'm getting errors

      Exception setting "Password": "The property 'Password' cannot be found on this object. Verify that the property exists and can be set."

      theres no such thing as

      .Password

      ill work on it !!!

  • #63219
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    Here is an example of using a calculated expression and splatting to make some clean code:

    #Calculated Expression
    $users = Import-CSV D:\a.csv | 
             Select *,
             @{Name='Password';Expression={-join ((97..122) + (48..57) | Get-Random -Count 10 | % {[char]$_})}}
             
    foreach ($user in $users) {
        #splatting
        $userParam = @{
            Name            = $user.Name 
            Enabled         = $true
            AccountPassword = (ConvertTo-SecureString -AsPlainText $user.Password -Force )
            SamAccountName  = $user.SamAccountName
            Path            = "OU=TEST,OU=TEST,DC=TEST,DC=TEST,DC=TEST,DC=TEST"
        }
        
        New-ADUser @userParam
    }   
    
  • #63222
    Profile photo of Daniel Krebs
    Daniel Krebs
    Moderator

    One would need to replace

    $u.Password = -join ((97 .. 122) + (48 .. 57) | Get-Random -Count 10 | % { [char]$_ })
    

    with

    $u | Add-Member -MemberType NoteProperty -Name Password -Value (-join ((97 .. 122) + (48 .. 57) | Get-Random -Count 10 | % { [char]$_ }))
    

    or

    $u | Add-Member -MemberType NoteProperty -Name Password -Value ([System.Web.Security.Membership]::GeneratePassword(10, 2))
    

    because $u is a PSCustomObject not a Hashtable.

    I hope that helps.

  • #63226
    Profile photo of Ron
    Ron
    Participant

    He said the column was there but empty, so shouldn't need to add it, if that's true.

    $csv="a,b,c`r`n1,2,"|convertfrom-csv
    $csv|ft -auto
    
    a b c
    - - -
    1 2
    
    $csv.c='3'
    $csv|ft -auto
    
    a b c
    - - -
    1 2 3
    • #63235
      Profile photo of barbar
      barbar
      Participant

      well Ron ... you're right ... in the source CSV i did not have a column named "PASSWORD" since my first script created it in the exported CSV ... and i tried to create two users with the same name to see what it does if it fails it gave this error

      New-ADUser : The specified account already exists

      and in the exported csv it gave the same password for both rows...
      i tried it with ur twist after i added the column Password in the source CSV and it did exactly what u said it would do ... it gave a blank cell for the password
      well thank you all this is very helpful actually .

You must be logged in to reply to this topic.