Set subscription with Powershell

When users have been synchronized to Office 365, they lack subscription, which means they cannot access Exchange, Lync or SharePoint. Microsoft has documentation on how to activate synced users with the GUI. http://technet.microsoft.com/en-us/library/hh967617.aspx This can be automated using Powershell.

 # Connect to service $Username = "admin@mydomain.onmicrosoft.com" $Password = ConvertTo-SecureString P@ssword" -AsPlainText -Force $Credentials = New-Object System.Management.Automation.PSCredential $Username,$Password Connect-MsolService -Credential $Credentials # Set location Get-MSOLUser -UnlicensedUsersOnly | Set-MSOLUser -UsageLocation "SE" # Set subscription Get-MSOLUser -UnlicensedUsersOnly | where{$_.Title -eq "Student"} | Set-MsolUserLicense -AddLicenses "contoso:STUDENTPACK" Get-MSOLUser -UnlicensedUsersOnly | where{$_.Title -eq "Teacher"} | Set-MsolUserLicense -AddLicenses "contoso:FACULTYPACK" 

Script to set new password in Office 365

The following script can be used to set new password in Office 365. Replace username and password of service account and save file as Set-Password.ps1.

Set-Password.ps1 -UserPrincipalName [UserPrincipalName] -NewPassword [NewPassord]

param
(
   [parameter(Mandatory = $true)][string]$UserPrincipalName,
   [parameter(Mandatory = $true)][string]$NewPassword
)

function Set-Password()
{
   # Connect to service
   $Username = "admin@mydomain.onmicrosoft.com"
   $Password = ConvertTo-SecureString "P@ssword" -AsPlainText -Force
   $Credentials = New-Object System.Management.Automation.PSCredential $Username,$Password
   Connect-MsolService -Credential $Credentials

   # Reset password
   $pwd = ConvertTo-SecureString $NewPassword -AsPlainText -Force
   Set-MsolUserPassword -UserPrincipalName $UserPrincipalName -NewPassword $pwd -ForceChangePassword $false
}

Set-Password -UserPrincipalName $UserPrincipalName -NewPassword $NewPassword

Create Office 365 accounts using Powershell

When Office 365 was released, provisioning accounts with DirSync was the only available method. There was no support for Powershell which was unfortunate. Since then Microsoft has released Powershell support and readily documentation. This article describes a quick way to create an account in Office 365 using Powershell.

To begin using the Office 365 cmdlets, you first need to install them. This is described under section Install the Office 365 cmdlets. Use x64 and make sure you set ExecutionPolicy to RemoteSigned.

Set-ExecutionPolicy RemoteSigned

Then you need to import Powershell module for Office 365.

Import-Module MSOnline

Create a session.

$Username = "admin@mydomain.onmicrosoft.com"
$Password = ConvertTo-SecureString "P@ssword" -AsPlainText -Force
$Credentials = New-Object System.Management.Automation.PSCredential $Username,$Password
Connect-MsolService -Credential $Credentials

Create a new User.

New-MsolUser -UserPrincipalName johndoe@mydomain.onmicrosoft.com -DisplayName "John Doe" -FirstName "John" -LastName "Doe"

New-MsolUser will create a random password and return a MsolUser object which contains the new password. However, when logging in to the portal, user will face an error message.

This is beacuse a user needs to be assigned to a subscription. To do this UsageLocation nedds to be set and second a license added:

Set-MsolUser -UserPrincipalName johndoe@mydomain.onmicrosoft.com -UsageLocation "US"
Set-MsolUserLicense -UserPrincipalName johndoe@mydomain.onmicrosoft.com -AddLicenses "tengil:ENTERPRISEPACK"

It is possible (and recommended) to create a user with a license in a single call.

New-MsolUser -UserPrincipalName johndoe@mydomain.onmicrosoft.com -DisplayName "John Doe" -FirstName "John" -LastName "Doe" -UsageLocation "US" -LicenseAssignment "Contoso:BPOS_Standard"

A simple script to summarize this:


param(
[parameter(Mandatory = $true)][string]$AdminUsername,
[parameter(Mandatory = $true)][string]$AdminPassword,
[parameter(Mandatory = $true)][string]$UserPrincipalName,
[parameter(Mandatory = $true)][string]$DisplayName
[parameter(Mandatory = $true)][string]$FirstName,
[parameter(Mandatory = $true)][string]$LastName,
[parameter(Mandatory = $true)][string]$UsageLocation,
[parameter(Mandatory = $true)][string]$LicenseAssignment
)

function New-O365User()
{
if((Get-Module MSOnline) -eq $null)
{
Import-Module MSOnline
}

$pwd = ConvertTo-SecureString $AdminPassword -AsPlainText -Force
$Credentials = New-Object System.Management.Automation.PSCredential $AdminUsername,$pwd
Connect-MsolService -Credential $Credentials

New-MsolUser -UserPrincipalName $UserPrincipalName -DisplayName $DisplayName -FirstName $FirstName -LastName $LastName -UsageLocation $UsageLocation -LicenseAssignment $LicenseAssignment

}

New-O365User -AdminUsername $AdminUsername -AdminPassword $AdminPassword -UserPrincipalName $UserPrincipalName -DisplayName $DisplayName -FirstName $FirstName -LastName $LastName -UsageLocation $UsageLocation -LicenseAssignment $LicenseAssignment

Modify QuickLaunch of SharePoint web with Powershell

I had to modify the quicklauch for multiple webs. In this case Powershell is my friend. The action needed to be done was to show subwebs in QuickLaunch. If publishing feature is activated on web, this property can be set on /_layouts/AreaNavigationSettings.aspx.

AreaNavigationSettings

With Powershell the property is a bit tricky to find. First, web need to create a PublishingWeb-object.

$pubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)

Second, the CurrentIncludeSubSites property of the Navigation property must be set to true.

$pubWeb.Navigation.CurrentIncludeSubSites = $true

The complete function looks something like this.


param(
[parameter(Mandatory = $true)][string]$Url
)

function Set-CurrentIncludeSubSites()
{
#Load assemblies
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
{
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}

$web = Get-SPWeb $Url

if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($web) -eq $true)
{
$pubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
$pubWeb.Navigation.CurrentIncludeSubSites = $true
$pubWeb.Update()
}
}

Set-CurrentIncludeSubSites -Url $Url

Save this as Set-CurrentIncludeSubSites.ps1 and run it with .\Set-CurrentIncludeSubSites.ps1 -Url http://myurl