Author Posts

October 16, 2015 at 6:51 am

Hi Guys

I am writing this script that will help us auto license new users in O365. The script is not finished yet and I have a logic problem here and I hope you could help me here because I ran out of ideas.

If the logical operator is -like i get :
username is operators or sales people and gets E2
username is operators or sales people and gets E2
username is operators or sales people and gets E2
username is operators or sales people and gets E2
usernames is operators or sales people and gets E2
username is operators or sales people and gets E2
username is operators or sales people and gets E2
username is operators or sales people and gets E2
username is operators or sales people and gets E2

which is not correct because some people are in Corporate OU which are supposed to get an E3 license.

If I change the operator to -match (does not make any sense but just for testing purposes i did it) i get:

Useraname is corporate/callcentermanager or store manager and gets E3
Useraname is corporate/callcentermanager or store manager and gets E3
Useraname is corporate/callcentermanager or store manager and gets E3
Useraname is corporate/callcentermanager or store manager and gets E3
Useraname is corporate/callcentermanager or store manager and gets E3
Useraname is corporate/callcentermanager or store manager and gets E3
Useraname is corporate/callcentermanager or store manager and gets E3
Useraname is corporate/callcentermanager or store manager and gets E3
Useraname is corporate/callcentermanager or store manager and gets E3

which is not right either.

If this makes sense to you I would appreciate your help

Thank you

$end = get-date
$CurrentDate = $end.ToString('MM-dd-yyyy_hh-mm-ss')
$When = (Get-Date).Date # today
$When1 = ((Get-Date).AddDays(-10)).Date #day before
#$user = Read-Host "name"
$corporate = "OU=Corporate,OU=Users,OU=local,DC=local,DC=local"
$CallCenterManagers = "OU=CallCenter Managers,OU=CallCenter,OU=CFAs,OU=Users,OU=local,DC=local,DC=local"
$Operators = "OU=Operators,OU=CallCenter,OU=CFAs,OU=Users,OU=local,DC=local,DC=local"
$SalesPeople= "OU=Salespeople,OU=Stores,OU=CFAs,OU=Users,OU=local,DC=local,DC=local"
$StoreManagers = "OU=Store Managers,OU=Stores,OU=CFAs,OU=Users,OU=local,DC=local,DC=local"
$path = "C:\New_Users_$currentDate.log"
$When = (Get-Date).Date # today

#$ou = Get-ADUser $user -Properties *| select UserPrincipalName, WhenCreated, Name, distinguishedName # testing purposes

### GET ALL USERS CREATED TODAY ###
$ou = Get-ADUser -SearchBase "ou=Users,ou=glw,dc=globalivewireless,dc=local" -Filter {whenCreated -ge $When1} -Properties * |select UserPrincipalName, Country, name, whencreated, distinguishedname

### FOR EVERY NEW USER SET O365 license ###
foreach($item in $ou){

if(($ou.distinguishedname -match $corporate) -or ($ou.distinguishedname -match $CallCenterManagers) -or ($ou.distinguishedname -match $StoreManagers))
{
#Set-MsolUser -UserPrincipalName $item.UserPrincipalName -UsageLocation $Item.country -Erroraction Stop
#Set-MsolUserLicense -UserPrincipalName $item.UserPrincipalName -AddLicenses $SKUIDE3 -Erroraction Stop
#$WriteLog = "$($item.UserPrincipalName) is licensed with $($SKUIDE3)"
#Add-Content -Path $path -Value $WriteLog
Write-Host $item.Name is corporate/callcentermanager or store manager and gets E3 -ForegroundColor yellow
}

#if(($ou.distinguishedname -like $Operators) -or ($ou.distinguishedname -like $SalesPeople)){
else
{
#Set-MsolUser -UserPrincipalName $item.UserPrincipalName -UsageLocation $Item.country -Erroraction Stop
#Set-MsolUserLicense -UserPrincipalName $item.UserPrincipalName -AddLicenses $SKUIDE2 -Erroraction Stop
#$WriteLog = "$($item.UserPrincipalName) was licensed with $($SKUIDE2)"
#Add-Content -Path $path -Value $WriteLog
Write-Host $item.Name is operators or sales people and gets E2 -ForegroundColor green}
}
#}

October 16, 2015 at 7:36 am

$corporate = "OU=Corporate,OU=Users,OU=local,DC=local,DC=local"

Is that really the OU path?

I would personally try
$corporate = "OU=CORPORATE,OU=USERS"
$ou.distinguishedname -like "*$corporate*"

October 16, 2015 at 7:44 am

Thank you for your suggestion however i can't get it to work.
Yes the path to Corporate is indeed OU=Corporate,OU=Users,OU=local,DC=local,DC=local
I noticed that if I give it single name everything works fine but in bulk it is not happening.
Don't know what to do next.
Thank you again

October 16, 2015 at 8:07 am

Couple of problems, Steven was almost there. First with -like you should use the wildcard as Steven suggested, but also are using $ou instead of $item for your distinguishedname. It should be $item because you are getting the distinguished name of the current item in your loop

### FOR EVERY NEW USER SET O365 license ###
foreach($item in $ou){

if(($item.distinguishedname -like "*$corporate") -or ($item.distinguishedname -like "*$CallCenterManagers") -or ($item.distinguishedname -like "*$StoreManagers"))
{
#Set-MsolUser -UserPrincipalName $item.UserPrincipalName -UsageLocation $Item.country -Erroraction Stop
#Set-MsolUserLicense -UserPrincipalName $item.UserPrincipalName -AddLicenses $SKUIDE3 -Erroraction Stop
#$WriteLog = "$($item.UserPrincipalName) is licensed with $($SKUIDE3)"
#Add-Content -Path $path -Value $WriteLog
Write-Host $item.Name is corporate/callcentermanager or store manager and gets E3 -ForegroundColor yellow
}

#if(($ou.distinguishedname -like $Operators) -or ($ou.distinguishedname -like $SalesPeople)){
else
{
#Set-MsolUser -UserPrincipalName $item.UserPrincipalName -UsageLocation $Item.country -Erroraction Stop
#Set-MsolUserLicense -UserPrincipalName $item.UserPrincipalName -AddLicenses $SKUIDE2 -Erroraction Stop
#$WriteLog = "$($item.UserPrincipalName) was licensed with $($SKUIDE2)"
#Add-Content -Path $path -Value $WriteLog
Write-Host $item.Name is operators or sales people and gets E2 -ForegroundColor green}
}
#}

It does seem odd however that you are basing your get-aduser at "ou=Users,ou=glw,dc=globalivewireless,dc=local", but none of your user OUs are in that same OU structure. I would not expect it to find any matches at all.

October 16, 2015 at 8:15 am

One more note, I just finished building a PowerShell module for doing this based on an attribute stored in AD. You can then use the module for running a sync with a scheduled task. I am currently implemented it in production in my environment working out any minor bugs, then I plan on publishing to github. I can post back on this thread once done. Should be a couple weeks, maybe early November.

October 16, 2015 at 8:22 am

Thank you Curtis.
It is working as it supposed to now.
Have a nice weekend

October 17, 2015 at 12:47 am

I could kick myself, letting that slip by me! Thank you for spotting that Curtis.

I'm really happy to see people working with Office 365 & PowerShell, it's a great combo.

October 17, 2015 at 1:11 am

Adresa, welcome to the community. Please do yourself a favor. In the future, please choose a more descriptive title for the threads you initiate. The generic "Script Help" is not helpful as that tends to apply to almost all the threads in this forum. A good title can draw in the right folks who might otherwise be busy and blow off a generic request.

Again welcome and best of luck on your PowerShell journey.