I have a Powershell (5.0) script that imports a CSV file and creates new Office 365 users. The accounts are being created correctly, however the specified license is not being applied.
My script:
Import-Csv -Path C:\Temp\tmp9DBC.csv | %{ New-MsolUser -DisplayName $_.DisplayName -UserPrincipalName $_.UserPrincipalName -City $_.City -Country $_.Country -Department $_.Department -FirstName $_.FirstName -LastName $_.LastName -ForceChangePassword:$True -UsageLocation $_.Location -Password $_.Password -PasswordNeverExpires:$True -State $_.State -Title $_.Title -LicenseAssignment $_.License}
All other properties are assigned correctly, but all of my users show isLicensed = false.
Assigning the license manually works fine, so I know that the SKU is good. Why is it not being applied?
EDIT: Sample entry from CSV file as requested:
ADULTMAN Vincent,adultmanv#domain.onmicrosoft.com,MyCity,MyCountry,Library and Information Services,Vincent,Adultman,"xxxxxxxxxxxxxx:STANDARDWOFFPACK_IW_FACULTY",Jaom3231,WA,Library Assistant
While using Import-Csv power shell command you must make sure that you are using the same column name which are mentioned in the csv file.
As your csv file contains column AccountType for licensing details, so please replace your existing powershell command with this:
Import-Csv -Path C:\Temp\tmp9DBC.csv | %{ New-MsolUser -DisplayName $_.DisplayName -UserPrincipalName $_.UserPrincipalName -City $_.City -Country $_.Country -Department $_.Department -FirstName $_.FirstName -LastName $_.LastName -ForceChangePassword:$True -UsageLocation $_.Location -Password $_.Password -PasswordNeverExpires:$True -State $_.State -Title $_.Title -LicenseAssignment $_.AccountType}
Hope this will help you.
Related
I am currently working on a script that should do the following features:
Create New-mailboxes, according to CSV File, Which is currently set-up like this:
The script i am using, is as follows.
#. 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'
#Connect-ExchangeServer -auto
<#
# Bulk Mailbox creation for Exchange.
Will create Mailbox's & AD Users.
CSV / script can be expanded to create more attributes.
Steve Lindsey
11/11/2013
#>
#csv format needs to be as follows:Â
#Alias,Firstname, Lastname,Name,UPN,OUpath,MBDB,password
#change the name and path of the csv file as required.
Import-Module ActiveDirectory
$users = Import-CSV C:\Users\user\Desktop\PowerShell\mailboxes2.csv
$users| foreach {
$Password = convertto-securestring $_.password -asplaintext -force
new-mailbox -name $_.name -alias $_.alias -displayname $_.Displayname -FirstName $_.Firstname -LastName $_.Lastname -userPrincipalName $_.userPrincipalName -PrimarySmtpAddress $_.PrimarySmtpAddress -Database $_.database -RetentionPolicy "b3a83dc4-e471-4d05-b357-25535aa027af" -OrganizationalUnit $_.OrganizationalUnit -Password $Password –ResetPasswordOnNextLogon:$false
}
sleep 30
foreach($user in $users){set-mailbox $user.alias -EmailAddressPolicyEnabled:$true}
sleep 30
foreach($user in $users){set-mailbox $user.alias -EmailAddressPolicyEnabled:$false}
sleep 30
foreach($user in $users){set-mailbox $user.PrimarySmtpAddress -PrimarySmtpAddress $user.PrimarySmtpAddress}
foreach($user in $users){Get-ADUser -Filter "UserPrincipalName -eq '$($user.userprincipalname)'" | Set-ADUser -PostalCode "0101010101" -POBox "000"}
In addition to what this script initialy does,
I need it to also be able to do following, after creating the mailboxes.
each of the Active Directory - USER objects , created for each mailbox should contain, ZIP \ PostalCode "01010101" -POBox "000" + Description "INC000002507906". and created under
The address tab.
https://imgur.com/NLR9HEg
For the "description" part i would look to set some additional column if possible in the imported CSV. so i can edit it, and each time the script will pick up the text from there.
I have tried the following , to achieve these, at the end of the script.
# foreach($user in $users){Set-ADUser -Filter "UserPrincipalName -eq '$($_.userprincipalname)'" -PostalCode "0101010101" -POBox "000"}
# foreach($user in $users){Get-ADUser -Filter "UserPrincipalName -eq '$($_.userprincipalname)'" | Set-ADUser -PostalCode "0101010101" -POBox "000"}
# foreach($user in $users){Get-ADUser $user.identity | Set-ADUser -PostalCode "0101010101" -POBox "000"}
# foreach($user in $users){Get-ADUser $users |Set-ADUser -PostalCode "0101010101" -POBox "000"}
# Foreach($user in $users){Set-ADUser -Identity $users -PostalCode "01010101" -POBox "000"}
Each time i tried these, i faced with a filter error:
https://imgur.com/c4BDdci
The second thing i need to do , is to force the script to go over the CSV
Again, and set the default SMTP , for each user/mailbox created, as set in
the "PrimarySmtpAddres" column.
This is due, to when the EmailAddressPolicyEnabled:$true cmdlet takes
takes place in the code, it sets the Primary Smtp back to the
organization`s default one.
I researched this a bit online and from what i could gather, this might be accomplished with something like :
Set-mailbox somealias -PrimarySmtpAddress "some.guy#contoso.com"
Combined with for each command, i believe it might work, i am m just not sure of the syntax i should use to accomplish that.
Your help is greatly appreciated
<<< UPDATE >>>
With help from AdminOFThings - most have been accomplished.
Thank you very much sir.
Now , all i have left needed , is the following:
foreach($user in $users){Get-ADUser -Filter "UserPrincipalName -eq '$($user.userprincipalname)'" | Set-ADUser -PostalCode "0101010101" -POBox "000"}
This is, the last "foreach" i have in the script.
i want also, in addition to add " -Description " to the parameters of the Set-ADuser cmdlet. in addition i would like to add, another column to the CSV file,
which will be title "Description" and for each line\mailbox created , a different description will be put in. the script will run and add the description defined on each mailbox in the file, on the description colum\line.
Please assist accomplishing that.
I have a csv file with datas:
SamAccountName;MobilePhone;surname
abc;2313;abc
bca;;bca
cba;3243>cba
How can I change this code to replace the cells where no data is available. For example I can't change the bca user attributes because no mobilephone available.
Here is the code:
PS C:\Users\Administrator> Import-Csv "C:\jo.csv" -Delimiter ';' |
ForEach-Object{
Set-ADUser -Identity $_.SamAccountName -Surname $_.surname -Givenname $_.givenname -Displayname $_.displayname -Title
$_.Title -Department $_.Department -Manager $_.Manager -OfficePhone $_.HomePhone -MobilePhone $_.MobilePhone
}
You can do something called splatting in PowerShell, where you construct the parameters into an object and feed that object to the cmdlet instead of each individual parameter.
Something like this:
$params = #{Identity = $_.SamAccountName}
if (-not [string]::IsNullOrWhiteSpace($_.surname)) {
$params.Surname = $_.surname
}
if (-not [string]::IsNullOrWhiteSpace($_.givenname)) {
$params.Givenname = $_.givenname
}
#Do the same for all the other parameters
Set-ADUser #params
Because of the null values you can always use a replace command.
In your script:
$Temp = "C:\jo.csv"
(Get-Content $Temp -Raw) -replace '""','"-"' | Set-Content $Temp
#Replace the "" with "-" because the set-aduser can not complete with null values
After that you can simply run the Set-ADUser command.
As you can see from the screenshot, I get an error from running the script but when I take the output from Write-Host and paste it in, it runs successfully, what's going on?
For testing I commented out New-Mailcontact and added the Write-Host at the end.
$CSVpath = Read-Host -Prompt "Enter the path of the contacts .csv"
Import-Csv "$CSVpath" | ForEach-Object {
$name = $_.displayName
$proxies = $_.proxyAddresses
$proxy = $proxies.replace(";",",")
#New-MailContact -ExternalEmailAddress $_.Mail -Name "`"$name`"" -Alias $_.sAMAccountName -DisplayName $name -FirstName $_.givenName -Initials $_.initials -LastName $_.sn
Set-MailContact -Identity $_.sAMAccountName -CustomAttribute1 "CreatedWithScript"
Set-MailContact -Identity $_.sAMAccountName -CustomAttribute3 $_.extensionAttribute3
Set-MailContact -Identity $_.sAMAccountName -EmailAddresses $proxy
Write-Host "New contact created: Name="$_.displayName
Write-Host " UPN="$_.userPrincipalName
Write-Host Set-MailContact -Identity $_.sAMAccountName -EmailAddresses $proxy
}
Got the following which worked from a Spiceworks user.
I think the -EmailAddresses parameter requires an array of strings
rather than a single comma separated string. Give it a try with this:
$proxy = $proxies -split ';'
Set-MailContact -Identity $_.sAMAccountName -EmailAddresses $proxy
I'm trying to use this powershell script to update AD users. Ideally I'll be updating a bunch of attributes, but for now I'm just trying to get it to update the department just so I get tell if it's working.
Import-Module ActiveDirectory
$dataSource=import-csv "c:\ADupdate.csv"
foreach($dataRecord in $datasource) {
$employeeID=$dataRecord.employeeID
# List of attributes to update
$department=$dataRecord.department
Get-ADUser -SearchBase "ou=Test,ou=OurUsers,ou=Logins,dc=domain,dc=com" -Identity $employeeID -Properties department | Set-ADUser -Replace #{department=$department}
}
Figured out my own problem. Here is what I ended up using if anyone else is interested... though I'm using a lot of attributes.
Import-Module ActiveDirectory
$users = Import-Csv -Path c:\update.csv
foreach ($user in $users) {
Get-ADUser -Filter "employeeID -eq '$($user.employeeID)'" -Properties * -SearchBase "ou=Test,ou=OurUsers,ou=Logins,dc=domain,dc=com" |
Set-ADUser -employeeNumber $($user.employeeNumber) -department $($user.department) -title $($user.title) -office $($user.office) -streetAddress $($user.streetAddress) -City $($user.City) -state $($user.state) -postalCode $($user.postalCode) -OfficePhone $($user.OfficePhone) -mobile $($user.mobile) -Fax $($user.Fax) -replace #{"extensionAttribute1"=$user.extensionAttribute1; "extensionAttribute2"=$user.extensionAttribute2; "extensionAttribute3"=$user.extensionAttribute3}
}
I am trying to import users from a csv file, which I exported from a different domain. Unfortunately the manager attribute gives me a hard time.
1. What I have done so far (Export):
I exported User attributes from AD1 with the domain name oldDomain.com into export.csv. In order to generate the export.csv file I useed the following command:
Get-ADUser -Filter * -Property * | Select-Object givenName,sn,name,displayName,sAMaccountName,manager | Export-Csv -Encoding "UTF8" -path \\hostex\Share\export.csv
This will result to the following file:
"givenName","sn","name","displayName","sAMaccountName","manager"
"Test","User1","Test User1","Test User1","test.user1",
"Test","User2","Test User2","Test User2","test.user2","CN=Test User1,OU=Users,DC=oldDomain,DC=com"
2. Problem with Import
Afterwards I try to import/add the users into AD2 which uses the domainname newDomain.org. My command looks like this:
Import-Csv \\hostex\Share\export.csv | ForEach { New-ADUser -AccountPassword (ConvertTo-SecureString Pass321? -AsPlainText -force) -Path "OU=Users,DC=newDomain,DC=org" -GivenName $_.givenName -Name $_.name -Surname $_.sn -DisplayName $_.displayName -SamAccountName $_.sAMAccountName -Manager $_.manager.Replace("DC=oldDomain,DC=com","DC=newDomain,DC=org") }
This leads to an ADIdentityResolutionException. Since the first line of export.csv has no value set for the manager attribute, the command tries to find the user identity "" within AD2. This is impossible to find. Therefore the user is not added to AD2.
In order to resolve this issue I would like to add some kind of If-Statement, which sets the value for the manager attribute only if the equivalent value in export.csv is not null ($_.manager -notlike $null). Any ideas how to achieve this?
You probably attempt to reference a manager account before that account is actually created. Separate account creation from assigning a manager to it. Also, empty fields read from a CSV appear as empty strings, not $null, so you need to compare to '' to filter them out.
Try this:
$users = Import-Csv '\\hostex\Share\export.csv'
$initialPassword = ConvertTo-SecureString 'Pass321?' -AsPlainText -Force
$users | % {
New-ADUser -AccountPassword $initialPassword `
-Path 'OU=Users,DC=newDomain,DC=org' `
-GivenName $_.givenName `
-Name $_.name `
-Surname $_.sn `
-DisplayName $_.displayName `
-SamAccountName $_.sAMAccountName
}
$users | ? { $_.manager -ne '' } | % {
$manager = $_.manager -replace 'DC=oldDomain,DC=com$', 'DC=newDomain,DC=org'
Set-ADUser -Identity $_.sAMAccountName -Manager $manager
}
One way to do this would be to build the complete command as a string with an additional line that adds the manager option to the end of the string if it exists in the data and then use Invoke-Expression to execute the command.
Import-Csv \\hostex\Share\export.csv | ForEach {
$NewUserCommand = "New-ADUser -AccountPassword (ConvertTo-SecureString Pass321? -AsPlainText -force) -Path 'OU=Users,DC=newDomain,DC=org' -GivenName $_.givenName -Name $_.name -Surname $_.sn -DisplayName $_.displayName -SamAccountName $_.sAMAccountName"
If ($_.manager) {$NewUserCommand += " -Manager " + $_.manager.Replace('DC=oldDomain,DC=com','DC=newDomain,DC=org')}
Invoke-Expression $NewUserCommand
}