export results

Welcome Forums General PowerShell Q&A export results

This topic contains 9 replies, has 5 voices, and was last updated by

 
Participant
1 year, 8 months ago.

  • Author
    Posts
  • #63193

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

    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

      Participant
      Points: 0
      Rank: Member
      $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

      Participant
      Points: 0
      Rank: Member

      This is also a possible way.

      Cheers mate.

  • #63210
    Ron

    Participant
    Points: 0
    Rank: Member

    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

      Participant
      Points: 0
      Rank: Member

      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

    Participant
    Points: 30
    Rank: Member

    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

    Moderator
    Points: 0
    Team Member
    Rank: Member

    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
    Ron

    Participant
    Points: 0
    Rank: Member

    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

      Participant
      Points: 0
      Rank: Member

      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 .

The topic ‘export results’ is closed to new replies.