Assign License to Users with Group Membership


This script can be used to assign users a license who are member of a specific Office365-Security Group.
The password in this script is encrypted!
Please note that this script only works for users who do not have a license assigned yet.
There are a couple of variables used in this script. Please adjust them if necessary:

Variable Description
GName Name of the Office 365 Security Group
LicenseN Name of the license that will be assigned to the users
Make sure you fill in the license in the correct format! For example: a P1 license is: EXCHANGESTANDARD
UsageLocation Set user location
Make sure you use the correct format! For example: Netherlands = NL
AdminUser Username of the admin user in Office 365 to run this script
Use the UID of the user. For example: AssignLicenses@wortelltechready.com

Script

Here’s the code for the script:

#### Set Variable
Set-Variable -name GName -value "AssignLicenses@wortelltechready.com"
Set-Variable -name LicenseN -value EXCHANGESTANDARD
Set-Variable -name UsageLocation -value NL
Set-Variable -name AdminUser -value "admin@wortelltechready.com"


#### Set Encrypted Password
$Password = "01000000d08c9ddf0115d1118c7a00c04fc297eb010000000ec5c2fd088ac741a6882556fafff2bd0000000002000000000003660000c00000001000000040e00f5a263e04689f6499394c5c6bbe0000000004800000a000000010000000bd5dea3f12458d5030966c7c2cbd0f5528000000474330610cdcc62da9e80a3f19a1eb3b144b1c819ee6d6457906ddbad33baa9cd0944e904bf50000140000003974ffe84f0b9fa5df07c79eaeb3b2b84e5d0023"
$PasswordSecure = ConvertTo-SecureString -String $Password
$cred = New-Object system.Management.Automation.PSCredential($AdminUser, $PasswordSecure)

#### Create Function Logon to Office365
function Logon {
    Import-Module MSOnline
    Connect-MsolService -Credential $cred
               }


############################################################################################################################
############################################################################################################################

#### Logon to Office 365
Logon

#### Create Log File + Start Logging
$Log = "AssignLicenseByGroup.ps1" + ".log"
$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = "Continue"
Start-Transcript -path $Log -append


#### Get DistributionGroupMembers
$GUIDT = Get-MsolGroup -SearchString $GName
$GUID = $GUIDT.ObjectId

#### Set License
$CompanyInfo=Get-MsolAccountSKU
$CompanyName=$CompanyInfo.AccountName
$LicenseName=$CompanyName+":"+$LicenseN.ToUpper()

#Get-MsolGroupMember -GroupObjectId $GUID -All | Set-MsolUser -UsageLocation $UsageLocation
Get-MsolGroupMember -GroupObjectId $GUID -All | ForEach-Object {
    Set-MsolUser -ObjectId $_.ObjectId -UsageLocation $UsageLocation
	Set-MsolUserLicense -UserPrincipalName $_.EmailAddress -AddLicenses $LicenseName
    }
#### Stop Logging
Stop-Transcript

Copy and paste the code in notepad (for example) and save it as “AssignLicenseByGroup.ps1”. Go through the following steps to use the script.

Steps


This is the final part of a series of posts about some PowerShell scripts I created or used and modified for some Office 365/Exchange Online migrations.
An overview of the series can be found here.

Assign License to Users with Group Membership

Create External Contacts with CSV and PowerShell


To create External Contacts in Office 365/Exchange Online I’ve created a script which uses a CSV file for input. The file must be saved as “Unicode, Semicolon separated” (;) and must contain the following fields:

Name Description
ExternalEmailAddress Email Address for the contact (this filed cannot contain spaces!)
Name Full / Display Name
FirstName First name
LastName Last name
StreetAddress Street Address
City City
StateorProvince State or Province
PostalCode ZIP or Postal Code
Phone Office Phone
MobilePhone Mobile Phone
Pager Fax
HomePhone Address
Company City
Title State or Province
Department ZIP or Postal Code
CountryOrRegion Country or Region
Fax Fax
Initials Initials
Notes Notes
Office Office
Manager Manager
Hidden Hide or show the contact in the Global Address List with this field.It can contain only two values: TRUE or FALSE. When using TRUE the contact is hidden. If this field is left blank, the contact is made visible (=TRUE).

Script

Here’s the code for the script:

#### Create Function Logon to Office365 - Exchange Online
function Logon {
    #### Pop-up a dialog for username and request your password
    $cred = Get-Credential
    #### Import the Local Microsoft Online PowerShell Module Cmdlets and Connect to O365 Online
    Import-Module MSOnline
    Connect-MsolService -Credential $cred
    #### Establish an Remote PowerShell Session to Exchange Online
    $msoExchangeURL = “https://ps.outlook.com/powershell/”
    $session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $msoExchangeURL -Credential $cred -Authentication Basic -AllowRedirection
    Import-PSSession $session
                }

#### Create Function Logoff Office365 & Exchange Online
function Logoff {
    #### Remove the Remote PowerShell Session to Exchange Online ----
    Get-PsSession | Remove-PsSession
    #Remove-PsSession $session
                }

############################################################################################################################
############################################################################################################################

#### Logon to Office 365 & Exchange Online
Logon

#### Ask the user for input CSV File
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$ofd = New-Object System.Windows.Forms.OpenFileDialog
#$ofd.InitialDirectory = "d:\scripts"
$ofd.ShowHelp=$true
if($ofd.ShowDialog() -eq "OK") { $ofd.FileName }
$File = $ofd.Filename

#### Create Log File + Start Logging
if ($File -ne $Null) {
$Log = $File + ".log"
$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = "Continue"
Start-Transcript -path $Log -append
   }

#### Import CSV
Import-csv -Delimiter ";" $File | ForEach {

#### Create Contacts
New-MailContact -Name $_.Name -DisplayName $_.Name -ExternalEmailAddress $_.ExternalEmailAddress -FirstName $_.FirstName -LastName $_.LastName

#### Add Extra Information depending on Manager Field
if (!$_.Manager) {
    Set-Contact -Identity $_.Name -StreetAddress $_.StreetAddress -City $_.City -StateOrProvince $_.StateOrProvince -PostalCode $_.PostalCode -Phone $_.Phone -MobilePhone $_.MobilePhone -Pager $_.Pager -HomePhone $_.HomePhone -Company $_.Company -CountryOrRegion $_.CountryOrRegion -Title $_.Title -Department $_.Department -Fax $_.Fax -Initials $_.Initials -Notes $_.Notes -Office $_.Office
    }
if ($_.Manager) {
    Set-Contact -Identity $_.Name -StreetAddress $_.StreetAddress -City $_.City -StateOrProvince $_.StateOrProvince -PostalCode $_.PostalCode -Phone $_.Phone -MobilePhone $_.MobilePhone -Pager $_.Pager -HomePhone $_.HomePhone -Company $_.Company -CountryOrRegion $_.CountryOrRegion -Title $_.Title -Department $_.Department -Fax $_.Fax -Initials $_.Initials -Notes $_.Notes -Office $_.Office -Manager $_.Manager
    }

#### Hide or show the DistributionGroup in the Global Address List
if ($_.Hidden) {
[boolean] $StoreBool = [System.Convert]::ToBoolean($_.Hidden)
Set-MailContact $_.Name -HiddenFromAddressListsEnabled $StoreBool
    }

}

#### Stop Logging
Stop-Transcript

#### Logoff
Logoff

Copy and paste the code in notepad (for example) and save it as “CreateExternalContacts.ps1”. Go through the following steps to use the script.

Steps


This is part 5 of a series of posts about some PowerShell scripts I created or used and modified for some Office 365/Exchange Online migrations.

In the final part (6) of this series I will share a script which can be used to assign a license to users based on group membership.

An overview of the series can be found here.

Create External Contacts with CSV and PowerShell