Create a new user in Azure AD with all licenses assigned

I’ve done some work with a customer recently where there was a requirement to provision new users in Azure Active Directory and assign all available licenses during the provisioning process (e.g. Office 365, Windows Intune).

It’s fairly straightforward to do with PowerShell.  The code below looks for the Azure subscription’s default domain (*.onmicrosoft.com) but you can obviously change that.  It’s also designed to provision one user at a time, prompting the administrator for the first and last name.  Remove the parameters and you can use the rest of the code to create users in bulk.

param (
   [Parameter(Mandatory=$False)]
   [ValidateNotNullOrEmpty()]
   [string]$userFirstName = $(Read-Host -Prompt 'Please Enter the First Name'),
 
   [Parameter(Mandatory=$False)]
   [ValidateNotNullOrEmpty()]
   [string]$userLastName = $(Read-Host -Prompt 'Please Enter the Last Name')
   )
 
$domainName = (Get-MsolDomain | Where-Object {$_.Name -like '*.onmicrosoft.com'}).Name
 
$userDisplayName = $userFirstName + ' ' + $userLastName
$userPrincipalName = $userFirstName + '.' + $userLastName + '@' + $domainName
$userPrincipalName = $userPrincipalName -replace ' ',''
 
$userTempPassword = 'P@ssw0rd'
$userUsageLocation = 'AU'
 
New-MsolUser -FirstName $userFirstName `
  -LastName $userLastName `
  -UserPrincipalName $userPrincipalName `
  -DisplayName $userDisplayName `
  -Password $userTempPassword `
  -UsageLocation $userUsageLocation | Out-Null
 
$licenses = Get-MsolAccountSku
 
foreach ($license in $licenses){
  Set-MsolUserLicense -UserPrincipalName $userPrincipalName -AddLicenses $license.AccountSkuId
}
 
Get-MsolUser -UserPrincipalName $userPrincipalName | Format-Table DisplayName,UserPrincipalName,Licenses -AutoSize

Session recordings from TechEd New Zealand 2014 are live!

The two sessions I delivered at TechEd New Zealand 2014 are now live and available on MSDN Channel 9:

Session: Deep Dive – Deploying and Managing Windows To Go

Session: Automated Compliance, Auditing and Remediation for the Enterprise Datacenter

As always, it was great to attend TechEd New Zealand – the event organisers and track owners did a great job as they always do, and the audiences are always engaged and ready to skewer an unsuspecting presenter with difficult questions :-)

Create an OS Deployment Task Sequence in Configuration Manager 2012 with PowerShell

This code is designed to create the framework for the Windows 8.1 OSD Task Sequence used in Chapter 11 of “Learn ConfigMgr 2012 in a Month of Lunches“.

The PowerShell cmdlet doesn’t perform the full amount of customisation which the reader does in the book’s lab environment, but it sets up the structure of the Task Sequence quite well, leaving very little post-script customisation to perform.

You can download the script directly from http://www.jamesbannanit.com/wp/files/manning/CMTaskSequence.zip.

# Define Configuration Manager variables
 
$CMSiteCode = 'P01'
$CMSiteServer = 'LAB-CM01.mol.sccmlab.net'
$CMNameSpace = "rootSMSsite_$CMSiteCode"
 
# 1.0 Get Deployment Objects
 
$BootImageID = (Get-CMBootImage -Name 'Custom Boot Image (x64)').PackageID
$OSImageID = (Get-CMOperatingSystemImage -Name 'Windows 8.1 x64 Enterprise Gold').PackageID
$ClientPackageID = (Get-CMPackage -Name 'Configuration Manager Client Package').PackageID
 
 
# 1.1 Create Task Sequence
 
New-CMTaskSequence -InstallOperatingSystemImageOption `
  -TaskSequenceName 'Deploy Windows 8.1 x64 Enterprise' `
  -BootImagePackageId $BootImageID `
  -OperatingSystemImagePackageId $OSImageID `
  -OperatingSystemImageIndex '1' `
  -ClientPackagePackageId $ClientPackageID `
  -JoinDomain DomainType `
  -DomainName 'mol.sccmlab.net' `
  -DomainAccount 'MOLCM_JD' `
  -DomainPassword 'P@ssw0rd' `
  -DomainOrganizationUnit 'LDAP://OU=Workstations,OU=SCCMR2Lab,DC=mol,DC=sccmlab,DC=net' `
  -PartitionAndFormatTarget $true `
  -LocalAdminPassword 'P@ssw0rd'

Create a Package in ConfigMgr 2012 R2 with PowerShell

This PowerShell code is designed to create a new Configuration Manager Package for Notepad++ as well as an installation Program for the new Package, as per Chapter 8 in “Learn ConfigMgr 2012 in a Month of Lunches“.

While the code is specific for the lab environment, it will work for any Program in Configuration Manager 2012 R2.

# Define Configuration Manager variables
 
$CMSiteCode = 'P01'
$CMSiteServer = 'LAB-CM01.mol.sccmlab.net'
$CMNameSpace = "rootSMSsite_$CMSiteCode"
$PackageName = 'Notepad++ 6.6.9'
 
# 1.0 Create new Package
 
New-CMPackage -Name $PackageName -Path '\lab-cm01SourcesSoftwareNotepad++6.5.2'
 
# 1.1 Create new Program
 
New-CMProgram -PackageName $PackageName 
  -StandardProgramName 'Install - Notepad++ 6.6.9' 
  -CommandLine 'npp.6.6.9.Installer.exe /S' 
  -RunType Hidden 
  -RunMode RunWithAdministrativeRights 
  -UserInteraction $false 
  -ProgramRunType WhetherOrNotUserIsLoggedOn 
  -DriveMode RenameWithUnc

Set ConfigMgr client Inventory Settings with PowerShell

This PowerShell code is designed to create new custom Device client settings and then enable and configure those settings for Software Inventory, as per Chapter 6 in “Learn ConfigMgr in a Month of Lunches“.

A few things to note about this code:

  1. Similar to the PowerShell code used to create and configure Active Directory Group Discovery, the WMI class which contains the settings objects/instances (SMS_ClientSettings) is not the one you need to work with in order to configure the inventory settings, which in this case is SMS_SoftwareInventoryAgentConfig
  2. Note the Property array which is built using the SMS_SoftwareInventoryAgentConfig class – you need to specify the type of data for each line, otherwise the default is STRING, which will throw an error when you try to write it back to the instance in SMS_ClientSettings

The code is designed for the lab environment in the book, but the process will work in any ConfigMgr 2012 R2 environment.

# Define Configuration Manager variables
 
$CMSiteCode = 'P01'
$CMSiteServer = 'LAB-CM01.mol.sccmlab.net'
$CMNameSpace = "rootSMSsite_$CMSiteCode"
$CustomSettingsName = 'Desktop Software Inventory Settings'
 
# 1.0 Create new Client Settings
 
New-CMClientSetting -Name $CustomSettingsName -Type Device
 
# 1.1 Enable Software Inventory
 
Set-CMClientSetting -Name $CustomSettingsName `
  -SoftwareInventorySettings `
  -EnableSoftwareInventory $true
 
# 1.2 Configure Software Inventory Settings
 
$Settings = Get-CimInstance -ComputerName $CMSiteServer -Namespace $CMNameSpace -ClassName SMS_ClientSettings | Where-Object {$_.Name -eq $CustomSettingsName}
 
$InventorySettings = New-CimInstance -ClientOnly `
  -Namespace $CMNameSpace `
  -ClassName SMS_SoftwareInventoryAgentConfig `
  -Property @{AgentID=[uint32]'2';
            Enabled=[boolean]'True';
            Exclude=[string[]]'True';
            ExcludeWindirAndSubfolders=[string[]]'True';
            InventoriableTypes=[string[]]'*.exe';
            Path=[string[]]'*';
            ReportOptions=[uint32]'7';
            Schedule='0001200000100038';
            Subdirectories=[string[]]'True'}
 
$settings.AgentConfigurations += $InventorySettings
 
Get-CimInstance -ComputerName $CMSiteServer -Namespace $CMNameSpace -ClassName SMS_ClientSettings | Where-Object {$_.Name -eq $CustomSettingsName} | Set-CimInstance -Property @{AgentConfigurations=$Settings.AgentConfigurations}