header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
How to create users across multiple dbs from file.
Last Post 21 Jul 2011 02:07 PM by mr_evans2u. 17 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
mr_evans2uUser is Offline
New Member
New Member
Posts:32
Avatar

--
20 Jul 2011 06:41 AM
    I have a file that has all of our databases (420), I need to read from that file and evenly add users to all of the databases. The bottom part of the script works, I'm just not sure how to plug in the databases and specify how many users to put on each. Any suggestion/help is greatly appreciated.


    $DbList = "D:\file location\ file.txt"

    Get-Content $DbList | ForEach {
    $Db =
     }


    $password = Read-host "Enter password" -AsSecureString
     
    1..4500 | Foreach {New-Mailbox -Name "Vmobile TempUser$($_)" -Alias "Vmobile.TempUser$($_)" -UserPrincipalName "Vmobile.TempUser$($_)@company.com" -Database $Db -OrganizationalUnit 'OU=$Vmobile Temp,OU=Mailboxes,OU=Accounts,DC=test,DC=test,DC=company,DC=com' -password $password}



    Marco ShawUser is Offline
    Veteran Member
    Veteran Member
    Posts:1684
    Avatar

    --
    20 Jul 2011 06:53 AM
    Do the DBs have to be randomly picked or can the first 10 go in DB#1, then the next 10 in DB#2, etc.?


    Marco ShawUser is Offline
    Veteran Member
    Veteran Member
    Posts:1684
    Avatar

    --
    20 Jul 2011 07:14 AM
    **Not tested**

    Can you try this:

    $DbList = "D:\file location\ file.txt"

    $password = Read-host "Enter password" -AsSecureString

    $j=0

    for($i=1;$i++;$i -lt 4501){
    New-Mailbox -Name "Vmobile TempUser$i" -Alias "Vmobile.TempUser$i" -UserPrincipalName "Vmobile.TempUser${i}@company.com" -Database $DbList[$j] -OrganizationalUnit 'OU=$Vmobile Temp,OU=Mailboxes,OU=Accounts,DC=test,DC=test,DC=company,DC=com' -password $password
    $j++
    if($j -ge $DbList.Count){$j=0}
    }


    mr_evans2uUser is Offline
    New Member
    New Member
    Posts:32
    Avatar

    --
    20 Jul 2011 07:39 AM
    Thank you Marco,
    I'll give that a shot. and I was going with "first 10 go in DB#1, then the next 10 in DB#2, etc.?"


    Marco ShawUser is Offline
    Veteran Member
    Veteran Member
    Posts:1684
    Avatar

    --
    20 Jul 2011 07:42 AM
    Well, my script won't do that as-is. The annoying thing with "first 10 in DB1, etc." is that you don't have an even number of DBs and users, and I was too lazy to try to think through the logic of what to do with the remainder.


    Marco ShawUser is Offline
    Veteran Member
    Veteran Member
    Posts:1684
    Avatar

    --
    20 Jul 2011 07:43 AM
    Sorry, it's not about an "even number", but actually that users/DBs isn't an even number.


    mr_evans2uUser is Offline
    New Member
    New Member
    Posts:32
    Avatar

    --
    20 Jul 2011 07:52 AM
    I got this error message back:
    New-Mailbox : Cannot bind parameter 'Database'. Cannot convert value "D" to type "Microsoft.Exchange.Configuration.Tasks.DatabaseIdParameter". Error: "Invalid
    cast from 'System.Char' to 'Microsoft.Exchange.Configuration.Tasks.DatabaseIdParameter'."


    mr_evans2uUser is Offline
    New Member
    New Member
    Posts:32
    Avatar

    --
    20 Jul 2011 09:11 AM
    When I look at the error message it has all of the servers\dbs listed, I only included the last couple.
    I'm getting an error that:
    ..... server73-SG18 server73\SG19\server73-SG19 server73\SG20\
    server73-SG20[5]' is not a valid value for the identity.
    Parameter name: Identity"
    At D:\Scripts\pwrshell\Generic_Mail.ps1:23 char:131
    + New-Mailbox -Name "Vmobile TempUser$i" -Alias "Vmobile.TempUser$i" -UserPrincipalName "Vmobile.TempUser${i}@company.com" -Database <<<< "$DbList[$j]" -OrganizationalUnit "OU=$Vmobile Temp,OU=Mailboxes,OU=Accounts,DC=test,DC=test,DC=company,DC=com" -password $password
    + CategoryInfo : InvalidArgument: (:) [New-Mailbox], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.Exchange.Management.RecipientTasks.NewMailbox


    ******Code*********

    $password = Read-host "Enter password" -AsSecureString

    $j=0

    $DbList = Get-Content "D:\Get-Mailbox\Genericmailboxes.txt"


    for($i=1;$i -lt 30;$i++){
    $j++

    New-Mailbox -Name "Vmobile TempUser$i" -Alias "Vmobile.TempUser$i" -UserPrincipalName "Vmobile.TempUser${i}@comapnay.com" -Database "$DbList[$j]" -OrganizationalUnit "OU=$Vmobile Temp,OU=Mailboxes,OU=Accounts,DC=test,DC=test,DC=company,DC=com" -password $password
    #Write-Host $i

    #$j++
    if($j -ge $DbList.Count){$j=0}
    }


    Marco ShawUser is Offline
    Veteran Member
    Veteran Member
    Posts:1684
    Avatar

    --
    20 Jul 2011 09:13 AM
    Whoops...

    Change this:
    $DbList = "D:\file location\ file.txt"

    To:
    $DbList = get-content "D:\file location\ file.txt"

    Maybe that will do it...


    Marco ShawUser is Offline
    Veteran Member
    Veteran Member
    Posts:1684
    Avatar

    --
    20 Jul 2011 09:15 AM
    What does this file look like: "D:\Get-Mailbox\Genericmailboxes.txt"?

    You would need to have one DB per line.


    mr_evans2uUser is Offline
    New Member
    New Member
    Posts:32
    Avatar

    --
    20 Jul 2011 09:21 AM
    This is how the file looks.

    server11\SG01\server11-SG01
    server11\SG02\server11-SG02
    server11\SG03\server11-SG03
    server11\SG04\server11-SG04


    Marco ShawUser is Offline
    Veteran Member
    Veteran Member
    Posts:1684
    Avatar

    --
    20 Jul 2011 04:52 PM
    Ah... I think your problem is that you have put quotes around $DbList[$j]. Please try without...


    get-jamesUser is Offline
    New Member
    New Member
    Posts:59
    Avatar

    --
    20 Jul 2011 11:11 PM
    Hi,

    I would take a different approach and instead of putting users on each database in groups of 10, I would randomaly put the users accross all DB's until each DB has a certain amount of users in them.  This way, there is no pattern of where each group of users go and balances the load quite nice, so you dont have all your VIPs in one place.


    See attached text file for code.

    Cheers
    James



    002_001_CreateExchUserRandom.txt

    mr_evans2uUser is Offline
    New Member
    New Member
    Posts:32
    Avatar

    --
    21 Jul 2011 06:15 AM
    That worked Marco!
    Thank you. I looked at that code way to long to not see that.
    Thanks again


    mr_evans2uUser is Offline
    New Member
    New Member
    Posts:32
    Avatar

    --
    21 Jul 2011 06:16 AM
    I will look at yours too James.
    Thank you for working on that code.


    mr_evans2uUser is Offline
    New Member
    New Member
    Posts:32
    Avatar

    --
    21 Jul 2011 11:05 AM
    Another question,
    I've just started looking at this but if I were to need to generate a random password or "Password1", "Password2", etc. how do you get past the securestring?
    I tried it with -password $password$i and although that adds the number on the end correctly it throws this error:
    New-Mailbox : Cannot bind parameter 'Password'. Cannot convert the "System.Security.SecureString1" value of type "System.String" to type "System.Security.SecureString".
    New-Mailbox : Cannot bind parameter 'Password'. Cannot convert the "System.Security.SecureString2" value of type "System.String" to type "System.Security.SecureString".


    get-jamesUser is Offline
    New Member
    New Member
    Posts:59
    Avatar

    --
    21 Jul 2011 12:26 PM
    You need to use ConvertTo-SecureString, look at the below example:

       $password = (ConvertTo-SecureString ("Password"+$_) -asplaintext -force)

    So the script block will look like this:


    
    660..670 | Foreach-Object `
    {
     If($StrUsersPerDatabase | where {$_.count -le [string]$StrMaxUsersPerDatabase})
     {
      #Setting variables
      $StrName = "Vmobile TempUser$($_)"
      $StrAlias = "Vmobile.TempUser$($_)"
      $StrUserPrincipalName = "Vmobile.TempUser$($_)@company.com"
      $StrOrganizationalUnit = "OU=$Vmobile Temp,OU=Mailboxes,OU=Accounts,DC=test,DC=test,DC=company,DC=com"
      $StrTargetDB = (get-random -InputObject ($StrUsersPerDatabase | Where-Object {$_.count -le [string]$StrMaxUsersPerDatabase}))  
      $password = (ConvertTo-SecureString ("Password"+$_) -asplaintext -force)
    
      #Creating New-Mailbox
      New-Mailbox -Name $StrName -Alias $StrAlias -UserPrincipalName $StrUserPrincipalName -Database $StrTargetDB.Name -password $password
    
      #Increase count on $StrUsersPerDatabase by 1
      $StrUsersPerDatabase | Where-Object {$_.Name -eq $StrTargetDB.Name} | Add-member -MemberType NoteProperty -Name "Count" -Value ([int]$StrTargetDB.count+1) -force
     }
     Else
     {
      Write-Error "All Databases have max user counts on them."
     }
    }
    


    mr_evans2uUser is Offline
    New Member
    New Member
    Posts:32
    Avatar

    --
    21 Jul 2011 02:07 PM
    Thank you James, got it working


    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer