Thursday, 19 November 2015

Disable Office 365 License Plans with Powershell

While we set a new license to an user, we can enable or disable only a particular set of service plans of the new license. we can use the powershell cmdlet New-MsolLicenseOptions to set license plans or features that we want to disable (or remove) from new license and we have to pass this disabled license options to Set-MsolUserLicense cmdlet with the parameter -LicenseOptions.

Note: Before proceed, Install and Configure Azure AD PowerShell

Disable office 365 service plans while adding new license:

To add a new license or disable service plans, we need to use AccountSkuId for the particular license, you can list all the available licenses and its AccountSkuId by running the following command.
Get-MsolAccountSku | Select AccountSkuId,SkuPartNumber,ActiveUnits,ConsumedUnits
You can get the list of all the available service plans in a particular license by using below command:
Get-MsolAccountSku | Where-Object {$_.AccountSkuId -eq 'mts:O365_BUSINESS_PREMIUM'} | ForEach-Object {$_.ServiceStatus}
#1 Create a new license options that we want to disable from the license that we are going to set to an user.
$options = New-MsolLicenseOptions -AccountSkuId mts:O365_BUSINESS_PREMIUM -DisabledPlans OFFICE_BUSINESS,MCOSTANDARD
#2 Now, we can add new license by passing new license's AccountSkuId to Set-MsolUserLicense cmdlet and set the license options in the paramater -LicenseOptions.
Set-MsolUserLicense -UserPrincipalName 'morgan@mts.com' –AddLicenses mts:O365_BUSINESS_PREMIUM -LicenseOptions $options

Enable or Disable License Plans in existing License:

Instead of disabling license plans while adding new license, you might also come across the need of enable or disable service plans in existing license. You can achieve this need by using the same office 365 powershell cmdlets Set-MsolUserLicense and New-MsolLicenseOptions. You can do it by just ignoring the parameter –AddLicenses in Set-MsolUserLicense cmdlet.

#1 Create the new license options that we want to disable from the existing license applied for the user.
$options = New-MsolLicenseOptions -AccountSkuId mts:O365_BUSINESS_PREMIUM -DisabledPlans OFFICE_BUSINESS,MCOSTANDARD
Note: There is no options like -EnabledPlans, you have use the same paramater -DisabledPlans to enable service plans in existing license by disabling other license plans

#2 Now, we can set this new license options in the Set-MsolUserLicense cmdlet with the paramater -LicenseOptions.
Set-MsolUserLicense -UserPrincipalName 'morgan@mts.com' -LicenseOptions $options

Monday, 16 November 2015

Powershell: Unable to find a default server with Active Directory Web Services running

Problem:

Today I experienced the error “Unable to find a default server with Active Directory Web Services running.” in Powershell with Windows 2012 Server. This message was occurred when I try to create bulk AD users using the New-ADUser command.
New-ADUser : Unable to find a default server with Active Directory Web Services running.
At line:6 char:1
+ New-ADUser `
+ ~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (:) [New-ADUser], ADServerDownException
    + FullyQualifiedErrorId : ActiveDirectoryServer:1355,Microsoft.ActiveDirectory.Management.Commands.NewADUser

Add-ADGroupMember : Unable to find a default server with Active Directory Web Services running.
At line:12 char:2
+  Add-ADGroupMember "Domain Admins" $userName;
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (Domain Admins:ADGroup) [Add-ADGroupMember], ADServerDownException
    + FullyQualifiedErrorId : ActiveDirectoryServer:1355,Microsoft.ActiveDirectory.Management.Commands.AddADGroupMembe
   r

Solution:

From this blog post https://jorgequestforknowledge.wordpress.com/2011/12/12/the-active-directory-web-service-adws/ , I have learned that the AD Powershell Module is depends on the windows service "Active Directory Web Service (ADWS)". So, I have checked the service "Active Directory Web Services" in "services.msc" console and re-started it, now the problem resolved successfully.

Saturday, 14 November 2015

Manage Office 365 Users License using Powershell

We can use the Azure AD Powershell cmdlet Set-MsolUserLicense to manage Office 365 license for a user. You can add a new license, remove an existing license and update existing license features (enable or disable license sub features) using this cmdlet.

Summary:

Add New License to Office 365 User:

To add a new license, we need to get AccountSkuId for the particular license, you can list all the available licenses and its AccountSkuId by running the following command.

Note: Before proceed, Install and Configure Azure AD PowerShell
Get-MsolAccountSku | Select AccountSkuId,ActiveUnits,ConsumedUnits
You can also get the list of all the available service plans in a particular license by using below command:
Get-MsolAccountSku | Where-Object {$_.AccountSkuId -eq 'mts:O365_BUSINESS_PREMIUM'} | ForEach-Object {$_.ServiceStatus}
Next, we need to set Usage Location for the user. This is required for every individual user. If you are re-assigning the license, the usage location is not mandatory. We have to use the Azure AD powershell cmdlet Set-MsolUser to set usage location.
Set-MsolUser -UserPrincipalName 'morgan@mts.com' -UsageLocation "US"
Now, we can assign the new license by using below powershell command.
Set-MsolUserLicense -UserPrincipalName 'morgan@mts.com' -AddLicenses 'mts:O365_BUSINESS_PREMIUM'
To add multiple licenses, you have to pass AccountSkuId of all the licenses as comma (,) separated values.
Set-MsolUserLicense -UserPrincipalName 'morgan@mts.com' -AddLicenses mts:O365_BUSINESS_PREMIUM,mts:AAD_PREMIUM

Add License with certain features:

While adding new license to an user, we can enable or disable only particular set of sub components of a new license. we have to use the powershell cmdlet New-MsolLicenseOptions with the option -DisabledPlans to set license features that we want to disable (or remove) from new license.
$options = New-MsolLicenseOptions -AccountSkuId 'mts:O365_BUSINESS_PREMIUM' -DisabledPlans OFFICE_BUSINESS,MCOSTANDARD
Now, we can pass this license options to Set-MsolUserLicense cmdlet using the paramater -LicenseOptions.
Set-MsolUserLicense -UserPrincipalName 'morgan@mts.com' –AddLicenses 'mts:O365_BUSINESS_PREMIUM' -LicenseOptions $options
Note: There is no option EnabledPlans like DisabledPlans, so we can't set only required features in straightforward way, we can achieve this only by excluding non-required features by using DisabledPlans option.

Update Features in existing License (Enable and Disable sub feature)

If you want to update or disable license features in existing license, you have to set only LicenseOptions in Set-MsolUserLicense cmdlet (exclude the parameter –AddLicenses).
$options = New-MsolLicenseOptions -AccountSkuId 'mts:O365_BUSINESS_PREMIUM' -DisabledPlans OFFICE_BUSINESS,MCOSTANDARD
Set-MsolUserLicense -UserPrincipalName 'morgan@mts.com' -LicenseOptions $options

Re-Assigning License of an Office 365 User:

We can easily re-assign or replace new license by removing old license by using single command. The below powershell command remove the license mts:O365_BUSINESS_PREMIUM from the user morgan and add the license mts:ENTERPRISEPACK.
Set-MsolUserLicense -UserPrincipalName 'morgan@mts.com' -AddLicenses 'mts:ENTERPRISEPACK' -RemoveLicenses 'mts:O365_BUSINESS_PREMIUM'

Remove Licenses from Office 365 User:

We can easily remove an existing license from an user by passing the license's AccountSkuId to the parameter –RemoveLicenses in Set-MsolUserLicense cmdlet.
Set-MsolUserLicense -UserPrincipalName 'morgan@mts.com' –RemoveLicenses 'mts:O365_BUSINESS_PREMIUM'
To remove multiple licenses, you have to give AccountSkuId of all the licenses as comma (,) separated values.

Find and Export User License Details:

We can find and get the applied license details of all the office 365 users by using the Azure AD powershell cmdlet Get-MsolUser. The Get-MsolUser cmdlet returns the user's license details and applied license sub features (enabled license plans).
$users = Get-MsolUser | Where-Object { $_.IsLicensed }
$users | Foreach-Object{ 
  $licenseDetail = '' 
  $licenses='' 
  if($_.licenses -ne $null) {
ForEach ($license in $_.licenses){
  switch -wildcard ($($license.Accountskuid.tostring())) { 
           '*POWER_BI_STANDALONE' { $licName = 'POWER BI STANDALONE' } 
           '*CRMSTANDARD' { $licName = 'CRM Online' }
           '*O365_BUSINESS_PREMIUM' { $licName = 'Office 365 BUSINESS PREMIUM' } 
           '*ENTERPRISEPACK' { $licName = 'Office 365 (Plan E3)' }  
           default { $licName = $license.Accountskuid.tostring() }
        }         

  if($licenses){  $licenses = ($licenses + ',' + $licName) } else { $licenses = $licName}
ForEach ($row in $($license.servicestatus)) {

if($row.ProvisioningStatus -ne 'Disabled') {          
       switch -wildcard ($($row.ServicePlan.servicename)) { 
           'EXC*' { $thisLicence = 'Exchange Online' }  
           'LYN*' { $thisLicence = 'Skype for Business' }
           'SHA*' { $thisLicence = 'Sharepoint Online' }       
           default { $thisLicence = $row.ServicePlan.servicename }  
       }         
 if($licenseDetail){ $licenseDetail = ($licenseDetail + ',' + $thisLicence) }  Else { $licenseDetail = $thisLicence}}
}}}
New-Object -TypeName PSObject -Property @{    
    UserName=$_.DisplayName   
    Licenses=$licenses 
    LicenseDetails=$licenseDetail }
}  | Select UserName,Licenses,LicenseDetails |
Export-CSV "C:\\Office-365-User-License-Report.csv" -NoTypeInformation -Encoding UTF8

Thursday, 12 November 2015

If Statement in Windows PowerShell

All of us should come across the need of If check when we create a powershell script. The If condition might be needed for different cases, i.e, to compare two integer values, compare string values, check a string value contains another string values. etc...In this article, I am going to write different IF condition statements.

If check with Integer value :

$a = 10

If($a -eq 10)  { '$a equals 10' 
} else { '$a not equals 10' }

If with Not Equal check:

$a = 10
If($a -ne 11)  { '$a not equals 10' }

If check with String value :

$str = 'Hello World'

# case insensitive check: This if check ignore the case sensitive.

If($str -eq 'hello world')  { 'True' 
} else { 'False' }
By default, all comparison operators are case-insensitive. To make a comparison operator case-sensitive, precede the operator name with a "c".
$str = 'Hello World'

# case sensitive check: This if statement checks with case sensitive.

If($str -ceq 'hello World')  { 'True' 
} else { 'False' }

If with Not Equal check:

$str = 'Hello World'

If($str -ne 'hello worlddd')  { 'True' }

If with contains (or like ) check:

$str = 'Hello World'

If($str -like '*hello*')  { 'True'
} else { 'False' }

If with not contains (or not like ) check:

$str = 'Hello World'

If($str -notlike '*helloddd*') { 'True' 
} else { 'False' }