Powershell


How to show (export) all devices synchronizing with your Office 365

For a MDM project I had to make an dump which users are using which devices to (active)sync with their Office 365 mailboxes. This is how I did that :

Get-Mailbox -ResultSize Unlimited | ForEach {Get-MobileDeviceStatistics -Mailbox:$_.Identity} | Select-Object @{label=”User” ; expression={$_.Identity}},DeviceModel,DeviceOS, lastsuccesssync | Export-csv F:\powershell\activesync.csv

Running the above command showed the following output (in a CSV file format):

As you can see you see the user, the device the user is using, the OS their device uses and last time that devices succesfully synchronized.

I used Excel to import the CSV and sort on LastSuccessSync.

Find out here how to connect to Office 365 using Powershell.


Powershell: Easily export your Windows Drivers

I was looking for a simple solution for me to export (and import later) my Windows driver. I already knew that Windows 2016 had a Powershell command for that but didn’t knew that it also worked for Windows Server 2012R2 and Windows 10!

Use the following command : Export-WindowsDriver -Destination “C:\Drivers\$((Get-WmiObject -Class win32_computersystem).Model)” -Online

export_drivers

Notice the exported drivers:

export_drivers2

Now you can easily import those drivers in (for example) SCCM or MDT.

 


Powershell: Start using PowerShell using PowerShell Command Builder

Microsoft TechNet site has a great site that enables you to build your own cmdlets using a web drag and drop interface.

There is support for :

image

Let’s try Office 365 :

image

You notice the drag and drop interface. First select the verb, for example Get. Click get and the send arrow. Now choose a Noun, for example Msol User:

image

After selecting the Verb and Noun you notice the other extra options. Fill them in when needed. Now hit the Copy to Clipboard button to copy the PowerShell command to your Clipboard and start using PowerShell to manage your Office 365.

You can find the site here.


Powershell: Connect to Azure Active Directory and Microsoft O365

Use the following Powershell script to connect to Azure Active Directory and Microsoft O365. This enables you to use all the O365 Powershell commands.

#Connecting to Exchange Online and Azure Active Directory

#This first command will import the Azure Active Directory module into your PowerShell session.
Import-Module MSOnline

#Capture administrative credential for future connections.
$credential = get-credential

#Establishes Online Services connection to Azure Active Directory  
Connect-MsolService -Credential $credential

#Creates an Exchange Online session
$ExchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $credential -Authentication Basic -AllowRedirection

#Import session commands
Import-PSSession $ExchangeSession

 

image

(Download the Powershell plugin here)


Office 365 : Disable Clutter (onbelangrijke mail) function

In this post I describe how to disable/enable the Clutter function of O365. The clutter feature is self learning and there are not many options you can specify. You can drag messages to and from the Clutter mailbox so that it can learn how to work. But sometimes you want do disable this function for specific mailboxes.

To disable Clutter for the entire organization you can use the following Powershell command:

Get-Mailbox | Set-Clutter -Enable $false

To disable Clutter for a single mailbox use the following command :

Set-Clutter -Identity helpdesk -Enable $false

To enable Clutter replace $false by $true

 

SNAGHTMLc9d7c89


Office 365 Powershell: Delegate Calender rights

Use the following steps to delegate (Calender) rights to a specific user.

First step specify your (administrator) credentials:

$LiveCred = Get-Credential

Create the session:

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic –AllowRedirection

Now import the Office 365 session using the following command:

Import-PSSession $Session

Use the following command to delegate the calender from user1 to specified user2

Add-MailboxFolderPermission -Identity user1@domain.com:\calendar -user user2@domain.com -AccessRights Editor

SNAGHTMLb41a829

Et voilà! The calender rights are set.


Powershell : How to identify (and delete) specific email messages from Office 365 1

I was looking for a way to identify messages in my Outlook Mailbox and easily delete them.

First connect to Office 365 with you (admin) credentials using the LiveCred command.

Set the Execution Policy and import the commandlets.

Using the next command creates an export of all emails from the specified user in the emailbox username and puts that export in the mailbox of user target mailbox and creates an folder called Searchlogs.

$LiveCred = Get-Credential

Set-ExecutionPolicy RemoteSigned (Make sure you open Powershell with Administrator Rights!)
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $LiveCred -Authentication Basic -AllowRedirection
Import-PSSession $Session
$Session Get-Mailbox username | Search-Mailbox -SearchQuery from:"user@emailbox.nl" -TargetMailbox info -TargetFolder SearchLogs -LogOnly -LogLevel Full

 

In that folder you find the logging of the files specified :

Results_powershell

 

Open the zip file containing the CSV export.

 

When you are ready to delete those files use the following command :

Search-Mailbox -Identity username -SearchQuery From:"user@mail.nl" –DeleteContent

Now all the files from the specified user are deleted. You can also use the -subject:’subjectname” switch.

 


Script to check multiple servers which services are using specific accounts

A client wanted to rename a specific account, but they were afraid to change this specific account because it could be used as a service account for several services running on a number of servers. Because I didn’t want to logon to all those servers manually I decided to create a powershell script.

 

Hereby the source code :

$DefineSaveLocation="f:\powershell"
$SaveLocaPath=Test-Path $DefineSaveLocation
if ($SaveLocaPath -eq $False)
    {New-Item -ItemType directory -Path $DefineSaveLocation}
cd $DefineSaveLocation

Foreach ($Server in Get-Content "F:\powershell\inputfile.txt" )
 {
  Write-Host "Retrieving Servers for $Server "    
  Get-WmiObject win32_service -ComputerName $Server  | select Name,
  @{N="Startup Type";E={$_.StartMode}},
  @{N="Service Account";E={$_.StartName}},
  @{N="System Name";E={$_.Systemname}} | Sort-Object "Name" > ".\$Server -Services.txt"
 }

 

This script uses a inputfile containing the servernames. You can use for example use rvtools or an WMI query to create this file.

After running the script the output is as follows :

services_export

(Click the picture for a full view)

I imported this file into excel and from now it’s easy to check for accounts being used etc.

Make sure to run powershell in elevated mode and don’t forget the set-executionpolicy unrestricted option.