Tag Archives: cmdlets

YaExOs (Yet another Exchange Online script)

Sup’ PSHomies,

Exchange online… I’ll be honest… In my many years in IT, I avoided Exchange like the plague! I’ve seen many colleagues crumble under it’s weight! (Sounds more dramatic don’t you think?) But with Office 365 being all the rage now, I guess I better get with the program, so here goes…

So there was recently a bit of a commotion on twitter about sharing level 200 stuff. We all were beginners at some point, so if you already know this then I guess this blog wasn’t meant for you… 😉

So how do you go about creating a mailbox for a user in Office 365 Exchange? Using PowerShell of course… Hehe…

Ok there are modules for AzureAD and MSOnline and stuff but where are the Exchange Online cmdlets? That was my first step. Luckily I found an excellent blog on how to go about doing this. The rest after that is pretty straightforward…

Looking at the cmdlet I wanted to figure out if there were any mandatory attributes.

(get-command New-OnlineMailbox).ParameterSets.Parameters | Out-GridView

 

No mandatory parameter(s)… I’ll just stick with the basics on this one:

  • Alias
  • Name
  • FirstName
  • LastName
  • DisplayName

Feel free to add any other parameters you need… 😉

You’ll need Global Admin credentials to create your Mailbox Online so have that at hand…

Once the Mailbox is created you’ll need to assign a license to the user. I’m using the MSOL cmdlet for this just because it’s still around (and way easier to implement). Having said that here’s a link to how it’s done using AzureAD cmdlets. That practical365 site is definitely a good source for anything Office365 related! 😉 Oh and the book is definitely worth having as a reference too!

Ok here’s the script:

<#
Author: I. Strachan
Version: 1.0
Version History:
Purpose: Import Exchange Online cmdlets and create a New Mailbox
#>
param(
[String]
$Alias = 'irwins',
[String]
$Name = 'irwins',
[String]
$FirstName = 'Irwin',
[String]
$LastName = 'Strachan',
[String]
$DisplayName = 'Irwin Strachan'
)
#region Exchange Online Functions
#https://practical365.com/exchange-server/powershell-function-connect-office-365/
function Connect-EXOnline {
Param(
[PSCredential]$EXOnlineCred
)
$Url = 'https://ps.outlook.com/powershell&#39;
$paramEXOSession = @{
ConfigurationName = 'Microsoft.Exchange'
ConnectionUri = $Url
Credential = $EXOnlineCred
Authentication = 'Basic'
AllowRedirection = $true
Name = 'Exchange Online'
}
$EXOSession = New-PSSession @paramEXOSession
Import-PSSession $EXOSession -Prefix 'Online'
}
function Disconnect-EXOnline {
Remove-PSSession -Name 'Exchange Online'
}
#endregion
#region Get Credentials and Connect To Exchange Online
$paramCredential = @{
Message = 'Enter Global O653 Admin account'
UserName = 'o365admin@yourtenant.onmicrosoft.com'
}
$o365Admin = Get-Credential @paramCredential
Connect-EXOnline -EXOnlineCred $o365Admin
Connect-MsolService -Credential $o365Admin
#endregion
#region Create a new Mailbox
$paramEXOMailBox = @{
Alias = $Alias
Name = $Name
FirstName = $FirstName
LastName = $LastName
DisplayName = $DisplayName
MicrosoftOnlineServicesID = '{0}@yourtenant.onmicrosoft.com' -f $Alias
Password = $(ConvertTo-SecureString -String 'P@ssw0rd' -AsPlainText -Force)
ResetPasswordOnNextLogon = $true
}
New-OnlineMailbox @paramEXOMailBox
#endregion
#region Assign new Mailbox a license
#Get AccountSku. In my case I only have one
$SkuId = (Get-MsolAccountSku).AccountSkuId
#Get All unlicensed users
Get-MsolUser -All -UnlicensedUsersOnly
#Set MSOL User License
#Alternatively have a look at https://practical365.com/blog/managing-office-365-licenses-with-azure-ad-v2-powershell-module/
Set-MsolUser -UserPrincipalName $paramEXOMailBox.MicrosoftOnlineServicesID -UsageLocation "NL"
Set-MsolUserLicense -UserPrincipalName $paramEXOMailBox.MicrosoftOnlineServicesID -AddLicenses $SkuId
#endregion
#region Disconnect
Disconnect-EXOnline
#endregion

So that’s how to get started with Exchange Online.

I’ll leave you with a lil’ thought from the Austrian Oak himself…

Beginners

Hope it’s worth something to you,

Ttyl,

Urv

 

 

Custom Intellisense for AD cmdlets with SearchBase parameter

Sup’ PSHomies!

You gotta love the PowerShell community! Found this little gem in my twitter feed (again) :-). Trevor Sullivan demonstrates how we can create custom intellisense for cmdlets if they haven’t been provided as yet. Great video! Trevor really does a great job explaining this.

The first thing that came to mind was Active Directory! I can’t tell you how often I needed the DistinguishedName of an OU. Now imagine having a dynamic list generated for you! No more errors,  just select and you’re good to go! Excited??? I sure am!

Sometimes you need to limit your searchbase depending on you AD size. Let’s say I want to retrieve all users starting from a specific point

Get-ADUser -Filter * -SearchBase 'OU=Users,OU=IT,DC=pshirwin,DC=local'

A simple typo will generate an error. Distinguished names are notorious for being lengthy…

Now the obvious AD cmdlets would be Get-ADUser,Get-ADGroup & Get-ADComputer. So that got me thinking , just how many AD cmdlets have SearchBase as a parameter?

Get-Command -Module ActiveDirectory |
ForEach-Object{
   $psItem.Name |
   Where-Object {
        (Get-Command $psItem).ParameterSets.Parameters.Name -eq 'SearchBase'
   }
}

Turns out there are quite a few using SearchBase

  • Get-ADComputer
  • Get-ADFineGrainedPasswordPolicy
  • Get-ADGroup
  • Get-ADObject
  • Get-ADOptionalFeature
  • Get-ADOrganizationalUnit
  • Get-ADServiceAccount
  • Get-ADUser
  • Search-ADAccount

So I can have Intellisense on all these cmdlets? Awesome!!!


<#
Author: I.C.A. Strachan
Version:
Version History:
Purpose: Custom Intellisense completion for AD cmdlets with SearchBase parameter
ActiveDirectory & TabExpansion++ module is required.
Link to Trevor Sullivan's video demonstration: https://goo.gl/0TdWuv
#>
#region Get AD cmdlets with SearchBase parameter
$ADCmdlestWithSearchBase = Get-Command -Module ActiveDirectory |
ForEach-Object{
$psItem.Name |
Where-Object {
(Get-Command $psItem).ParameterSets.Parameters.Name -eq 'SearchBase'
}
}
#endregion
#region Configure custom intellisense for AD cmdlets with SearchBase
$sbADSearchBase= {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
$ADPaths = ActiveDirectory\Get-ADOrganizationalUnit -filter *
foreach ($ADPath in $ADPaths){
$completionResult =@{
CompletionText = $ADPath.DistinguishedName
ToolTip = ('The organization unit DistinguishedName {0}' -f $ADPath.DistinguishedName)
ListItemText = $ADPath.Name
CompletionResultType = 'ParameterValue'
}
New-CompletionResult @completionResult
}
}
$tabExpansion = @{
CommandName = $ADCmdlestWithSearchBase
ParameterName = 'SearchBase'
ScriptBlock = $sbADSearchBase
}
TabExpansion++\Register-ArgumentCompleter @tabExpansion
#endregion

Intellisense completed the DistinguisedName on -SearchBase for me. No need to type it in, no errors, just select and go!

TabExpansionSearchBase

Here’s the result:

TabExpansionSearchBase-result

I’m sure you guys will find your own use for this… Thanks again Trevor for bring this to our attention! Good looking out for the community! Be sure to watch Trevor’s video for in depth explanation.

Hope it’s worth something to you…

Ttyl,

Urv