Create External Contacts with CSV and PowerShell


To create External Contacts in Office 365/Exchange Online I’ve created a script which uses a CSV file for input. The file must be saved as “Unicode, Semicolon separated” (;) and must contain the following fields:

Name Description
ExternalEmailAddress Email Address for the contact (this filed cannot contain spaces!)
Name Full / Display Name
FirstName First name
LastName Last name
StreetAddress Street Address
City City
StateorProvince State or Province
PostalCode ZIP or Postal Code
Phone Office Phone
MobilePhone Mobile Phone
Pager Fax
HomePhone Address
Company City
Title State or Province
Department ZIP or Postal Code
CountryOrRegion Country or Region
Fax Fax
Initials Initials
Notes Notes
Office Office
Manager Manager
Hidden Hide or show the contact in the Global Address List with this field.It can contain only two values: TRUE or FALSE. When using TRUE the contact is hidden. If this field is left blank, the contact is made visible (=TRUE).

Script

Here’s the code for the script:

#### Create Function Logon to Office365 - Exchange Online
function Logon {
    #### Pop-up a dialog for username and request your password
    $cred = Get-Credential
    #### Import the Local Microsoft Online PowerShell Module Cmdlets and Connect to O365 Online
    Import-Module MSOnline
    Connect-MsolService -Credential $cred
    #### Establish an Remote PowerShell Session to Exchange Online
    $msoExchangeURL = “https://ps.outlook.com/powershell/”
    $session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $msoExchangeURL -Credential $cred -Authentication Basic -AllowRedirection
    Import-PSSession $session
                }

#### Create Function Logoff Office365 & Exchange Online
function Logoff {
    #### Remove the Remote PowerShell Session to Exchange Online ----
    Get-PsSession | Remove-PsSession
    #Remove-PsSession $session
                }

############################################################################################################################
############################################################################################################################

#### Logon to Office 365 & Exchange Online
Logon

#### Ask the user for input CSV File
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$ofd = New-Object System.Windows.Forms.OpenFileDialog
#$ofd.InitialDirectory = "d:\scripts"
$ofd.ShowHelp=$true
if($ofd.ShowDialog() -eq "OK") { $ofd.FileName }
$File = $ofd.Filename

#### Create Log File + Start Logging
if ($File -ne $Null) {
$Log = $File + ".log"
$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = "Continue"
Start-Transcript -path $Log -append
   }

#### Import CSV
Import-csv -Delimiter ";" $File | ForEach {

#### Create Contacts
New-MailContact -Name $_.Name -DisplayName $_.Name -ExternalEmailAddress $_.ExternalEmailAddress -FirstName $_.FirstName -LastName $_.LastName

#### Add Extra Information depending on Manager Field
if (!$_.Manager) {
    Set-Contact -Identity $_.Name -StreetAddress $_.StreetAddress -City $_.City -StateOrProvince $_.StateOrProvince -PostalCode $_.PostalCode -Phone $_.Phone -MobilePhone $_.MobilePhone -Pager $_.Pager -HomePhone $_.HomePhone -Company $_.Company -CountryOrRegion $_.CountryOrRegion -Title $_.Title -Department $_.Department -Fax $_.Fax -Initials $_.Initials -Notes $_.Notes -Office $_.Office
    }
if ($_.Manager) {
    Set-Contact -Identity $_.Name -StreetAddress $_.StreetAddress -City $_.City -StateOrProvince $_.StateOrProvince -PostalCode $_.PostalCode -Phone $_.Phone -MobilePhone $_.MobilePhone -Pager $_.Pager -HomePhone $_.HomePhone -Company $_.Company -CountryOrRegion $_.CountryOrRegion -Title $_.Title -Department $_.Department -Fax $_.Fax -Initials $_.Initials -Notes $_.Notes -Office $_.Office -Manager $_.Manager
    }

#### Hide or show the DistributionGroup in the Global Address List
if ($_.Hidden) {
[boolean] $StoreBool = [System.Convert]::ToBoolean($_.Hidden)
Set-MailContact $_.Name -HiddenFromAddressListsEnabled $StoreBool
    }

}

#### Stop Logging
Stop-Transcript

#### Logoff
Logoff

Copy and paste the code in notepad (for example) and save it as “CreateExternalContacts.ps1”. Go through the following steps to use the script.

Steps


This is part 5 of a series of posts about some PowerShell scripts I created or used and modified for some Office 365/Exchange Online migrations.

In the final part (6) of this series I will share a script which can be used to assign a license to users based on group membership.

An overview of the series can be found here.

Advertisements
Create External Contacts with CSV and PowerShell

Create Distribution Group(s) with CSV and PowerShell


To create Distribution Groups in Office 365/Exchange Online I’ve created a script which uses a CSV file for input. The file must be saved as “Unicode, Semicolon separated” (;) and must contain the following fields:

Name Description
Name Name of the Shared Mailbox (this field cannot contain spaces!)
Display Name Full / Display Name
Email Primary email address (this field can contain only one address)
Alias Email alias address(es) – when using more than one: separate them by using a comma (,)
Members Specify the primary email addresses for users who will have access to this mailbox – separate them by using a comma (,)
Hidden Hide or show the Distribution Group in the Global Address List with this field (= required).It can contain only two values: TRUE or FALSE

Make sure that you give the right people (normally the IT department) owner rights for the Distribution Group(s). Because only owners can add members to a group. You need to specify the primary email addresses for the owners in the PowerShell script! Separate the email addresses by using a comma (,).

Script

Here’s the code for the script:

#### Set Variable
Set-Variable -name DGOwners -value admin@wortelltechready.com,user1@wortelltechready.com

#### Create Function Logon to Office365 - Exchange Online
function Logon {
    #### Pop-up a dialog for username and request your password
    $cred = Get-Credential
    #### Import the Local Microsoft Online PowerShell Module Cmdlets and Connect to O365 Online
    Import-Module MSOnline
    Connect-MsolService -Credential $cred
    #### Establish an Remote PowerShell Session to Exchange Online
    $msoExchangeURL = “https://ps.outlook.com/powershell/”
    $session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $msoExchangeURL -Credential $cred -Authentication Basic -AllowRedirection
    Import-PSSession $session
                }

#### Create Function Logoff Office365 & Exchange Online
function Logoff {
    #### Remove the Remote PowerShell Session to Exchange Online ----
    Get-PsSession | Remove-PsSession
    #Remove-PsSession $session
                }

############################################################################################################################
############################################################################################################################

#### Logon to Office 365 & Exchange Online
Logon

#### Ask the user for input CSV File
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$ofd = New-Object System.Windows.Forms.OpenFileDialog
#$ofd.InitialDirectory = "d:\scripts"
$ofd.ShowHelp=$true
if($ofd.ShowDialog() -eq "OK") { $ofd.FileName }
$File = $ofd.Filename

#### Create Log File + Start Logging
if ($File -ne $Null) {
$Log = $File + ".log"
$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = "Continue"
Start-Transcript -path $Log -append
   }

#### Import CSV
Import-csv -Delimiter ";" $File | ForEach {

#### Create DistributionGroup
New-DistributionGroup -Name $_."Name" -Alias $_."Name" -DisplayName $_."Display Name" -PrimarySmtpAddress $_."Email" -Type Distribution

#### Add Aliases
$Aliases = $_.Alias -split ','
if ($Aliases -ne $Null) {
Set-DistributionGroup -Identity $_."Name" -EmailAddresses @{add= $Aliases}
   }

#### Hide or show the DistributionGroup in the Global Address List
if ($_.Hidden) {
[boolean] $StoreBool = [System.Convert]::ToBoolean($_."Hidden")
Set-DistributionGroup -Identity $_."Name" -HiddenFromAddressListsEnabled $StoreBool
    }

#### Add Members
$Name = $_."Name"
$Member = $_.Members -split ","
$Member | Foreach-object {
Add-DistributionGroupMember -Identity $Name -Member $_
   }

#### Set Owners
Set-DistributionGroup $_."Name" -BypassSecurityGroupManagerCheck -ManagedBy $DGOwners

}

#### Stop Logging
Stop-Transcript

#### Logoff
Logoff

Copy and paste the code in notepad (for example) and save it as “CreateDistributionGorup.ps1”. Go through the following steps to use the script.

Steps

Other options you can use with a Distribution Group like Moderation, will have the default values. These can be set separately by using the web interface or with PowerShell.

This is part 4 of a series of posts about some PowerShell scripts I created or used and modified for some Office 365/Exchange Online migrations.

In part 5 of this series I will share a script which can be used to create External Contacts.

An overview of the series can be found here.

Download a sample CVS file from here: CreateDistributionGroup.csv. After the download, rename the file to “CreateDistributionGroup.csv”.

Create Distribution Group(s) with CSV and PowerShell

Issue: Windows Server 2012 Virtual Machines can only be backed up using saved state


I recently came across an issue in an environment where we used DPM 2012 SP1 to backup Windows Server 2012 Virtual Machines running on Server 2008R2 Hyper-V cluster. The only option available in DPM for the VM’s was “Backup Using Saved State”.

Of course this is not the preferred method for backing up a VM because it will be put in a saved state during backup causing the VM to be offline for that period of time. I’d like to use the online backup option: “Backup Using Child Partition Snapshot”.

After some digging I found a forum post which had a reply from a Microsoft employee that suggested to create the following registry key:
HKLM\Software\Microsoft\Windows NT\CurrentVersion\SystemRestore
REG_DWORD ScopeSnapshots 0x0

After implementing the registry key on all of the Windows Server 2012 VM’s I could backup using a child partition snapshot without any problems.

It seems that the Hyper-V integration components need a new feature in Windows Server 2012 (and Windows 8) called scoped VSS snapshots to be turned off in order to support online backups. This feature is enabled by default.

I also found some reports about Windows Server 2012 guests transferring the whole VM during backups instead on block level changes. Implement the registry key on every VM that is Windows 8 or Windows Server 2012 to resolve both of these issue.

Issue: Windows Server 2012 Virtual Machines can only be backed up using saved state