Monday, October 28, 2019

Getting or setting values in O365 with a Powershell function



Pay special attention to the values $ConnectionUri, $AzureADAuthorizationEndpointUri and $ModulePath as you may need to change them. My c:\exonline folder is actually a duplicate of the folder found on my machine (after the O365 install process) C:\Users\[sam account name]\AppData\Local\Apps\2.0\998QKRDT.T3O\KL00A4OZ.N0K\micr...exe_1975b8453054a2b5_0010.0000_none_1e2f2accd43128c3 . I don't know if this is a static path or not, aside from the profile name. You can also try this $Path = "$Env:LOCALAPPDATA\Apps\2.0\*\CreateExoPSSession.ps1"

This function uses c# to grab the email address for auth with o365, so that other Powershell modules and code don't have to be loaded before logging in. If you use something other than pass through ticket auth, this function won't work out of the box and you'll have to add creds to your new-pssession. It will open and close the session for each command sent. This can be optimized if you need to loop through a bunch of things in each session, I did not so I did not add that complication.

Try it!


function getExoCommandReturnValue
{
[CmdletBinding()]
    Param
    (
        [Parameter(Mandatory=$true, Position=0)]
        [string]$commandString,

[Parameter(Mandatory=$false, Position=1)]
        [string]$argumentList
    )

$mailAddr =
@'
using System;
using System.Data;
using System.DirectoryServices;
using System.Collections;

namespace mailNS
{
public class mailCl
{
public string getMailProps()
{
    string domainName = System.DirectoryServices.AccountManagement.UserPrincipal.Current.EmailAddress;

return domainName;
}
}
}

'@

try
{
$mailAddrGet = New-Object mailNS.mailCl;
}
catch
{
$assemblies = ("System", "System.Data", "System.DirectoryServices", "System.DirectoryServices.AccountManagement", "System.Security.Principal");

Add-Type -TypeDefinition $mailAddr -ReferencedAssemblies $assemblies -Language CSharp
$mailAddrGet = New-Object mailNS.mailCl;
}

Get-PSSession | where {$_.ComputerName -eq "outlook.office365.com"} | Remove-PSSession
[string] $ConnectionUri = "https://outlook.office365.com/PowerShell"; # may need different value
[string] $AzureADAuthorizationEndpointUri = 'https://login.windows.net/common'; # may need different value

[System.Management.Automation.Remoting.PSSessionOption] $PSSessionOption = $null;
$mailProperty = $mailAddrGet.getMailProps();
$ExoPowershellModule = "Microsoft.Exchange.Management.ExoPowershellModule.dll";
$ModulePath = "c:\exonline\Microsoft.Exchange.Management.ExoPowershellModule.dll"; # this will have to be customized to the file path these files are housed

Import-Module $ModulePath -WarningAction SilentlyContinue -WarningVariable $war1 -DisableNameChecking;

$PSSession = New-ExoPSSession -ConnectionUri $ConnectionUri -AzureADAuthorizationEndpointUri $AzureADAuthorizationEndpointUri -UserPrincipalName $mailProperty -WarningAction SilentlyContinue -WarningVariable $war2 -Confirm:$false -ErrorAction SilentlyContinue -ErrorVariable $err2 -PipelineVariable $pip2 -OutVariable $out2b;

Import-PSSession $PSSession -WarningAction SilentlyContinue -DisableNameChecking -AllowClobber;
$scriptBlock = [scriptblock]::Create($commandString);
$returnValue = Invoke-Command -ScriptBlock $scriptBlock;

return $returnValue;
}

cls;

$user = "exotestuser";

$properties = getExoCommandReturnValue -commandString 'get-mailbox $user';
$properties | fl Litigation*, *quotadefaults* , retentioncomment ;

$properties = getExoCommandReturnValue -commandString 'Set-Mailbox $user -litigationholdowner "lit_hold_owner" -retentioncomment "reten_comment"';

$properties = getExoCommandReturnValue -commandString 'get-mailbox $user';
$properties | fl LitigationHold*, *quotadefaults*, retentioncomment ;