Skip to content

Commit

Permalink
Merge pull request #2 from Microsoft/Dev
Browse files Browse the repository at this point in the history
Merge from base repo
  • Loading branch information
poiriersimon authored Dec 7, 2018
2 parents 946141e + 2d0f4eb commit badee47
Show file tree
Hide file tree
Showing 16 changed files with 1,266 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ function Get-TargetResource
[System.Management.Automation.PSCredential]
$GlobalAdminAccount
)

Write-Verbose "Get-TargetResource will attempt to retrieve information for group $($DisplayName)"
$nullReturn = @{
DisplayName = $DisplayName
GroupType = $GroupType
Description = $null
ManagedBy = $null
GlobalAdminAccount = $null
Ensure = "Absent"
}
Expand All @@ -57,8 +58,6 @@ function Get-TargetResource
Write-Verbose -Message "Getting Security Group $($DisplayName)"
$group = Get-MSOLGroup | Where-Object {$_.DisplayName -eq $DisplayName}

$group = Get-MSOLGroup | Where-Object {$_.DisplayName -eq $DisplayName}

if(!$group)
{
Write-Verbose "The specified Group doesn't already exist."
Expand All @@ -82,12 +81,14 @@ function Get-TargetResource
"MailEnabledSecurity" { $RecipientTypeDetails = "MailUniversalSecurityGroup" }
}

$allGroups = Invoke-ExoCommand -GlobalAdminAccount $GlobalAdminAccount -ScriptBlock{
$allGroups = Invoke-ExoCommand -GlobalAdminAccount $GlobalAdminAccount `
-Arguments $CurrentParameters `
-ScriptBlock{
Get-Group
}

$group = $allGroups | Where-Object {$_.DisplayName -eq $DisplayName -and $_.RecipientTypeDetails -eq $RecipientTypeDetails}
if(!$group)

if (!$group)
{
Write-Verbose "The specified Group doesn't already exist."
return $nullReturn
Expand All @@ -113,6 +114,7 @@ function Get-TargetResource
DisplayName = $group.DisplayName
GroupType = $GroupType
Members = $Members
ManagedBy = $group.ManagedBy
Description = $group.Notes
GlobalAdminAccount = $GlobalAdminAccount
Ensure = "Present"
Expand Down Expand Up @@ -190,6 +192,8 @@ function Set-TargetResource
$GlobalAdminAccount
)
Write-Verbose "Entering Set-TargetResource"
Write-Verbose "Retrieving information about group $($DisplayName) to see if it already exists"
$currentGroup = Get-TargetResource @PSBoundParameters
if( $Ensure -eq "Present")
{
$CurrentParameters = $PSBoundParameters
Expand All @@ -208,19 +212,70 @@ function Set-TargetResource
{
"Office365"
{
Write-Verbose -Message "Creating Office 365 Group $DisplayName"
Invoke-ExoCommand -GlobalAdminAccount $GlobalAdminAccount `
-Arguments $CurrentParameters `
-ScriptBlock {
New-UnifiedGroup -DisplayName $args[0].DisplayName -Notes $args[0].Description -Owner $args[0].ManagedBy
if ($currentGroup.Ensure -eq "Absent")
{
Write-Verbose -Message "Creating Office 365 Group $DisplayName"
Invoke-ExoCommand -GlobalAdminAccount $GlobalAdminAccount `
-Arguments $CurrentParameters `
-ScriptBlock {
New-UnifiedGroup -DisplayName $args[0].DisplayName -Notes $args[0].Description -Owner $args[0].ManagedBy
}
}

Invoke-ExoCommand -GlobalAdminAccount $GlobalAdminAccount `
-Arguments $CurrentParameters `
$groupLinks = Invoke-ExoCommand -GlobalAdminAccount $GlobalAdminAccount `
-Arguments $PSBoundParameters `
-ScriptBlock {
Add-UnifiedGroupLinks -Identity $args[0].DisplayName -LinkType Members -Links $args[0].Members
Get-UnifiedGroupLinks -Identity $args[0].DisplayName -LinkType "Members"
}
$curMembers = @()
foreach($link in $groupLinks)
{
if ($link.Name -and $link.Name -ne $currentGroup.ManagedBy)
{
$curMembers += $link.Name
}
}

$difference = Compare-Object -ReferenceObject $curMembers -DifferenceObject $CurrentParameters.Members

if ($difference.InputObject)
{
Write-Verbose "Detected a difference in the current list of members and the desired one"
$membersToRemove = @()
$membersToAdd = @()
foreach($diff in $difference)
{
if ($diff.SideIndicator -eq "<=" -and $diff.InputObject -ne $ManagedBy.Split('@')[0])
{
$membersToRemove += $diff.InputObject
}
elseif ($diff.SideIndicator -eq "=>")
{
$membersToAdd += $diff.InputObject
}
}

if ($membersToAdd.Count -gt 0)
{
$CurrentParameters.Members = $membersToAdd
Invoke-ExoCommand -GlobalAdminAccount $GlobalAdminAccount `
-Arguments $CurrentParameters `
-ScriptBlock {
Add-UnifiedGroupLinks -Identity $args[0].DisplayName -LinkType Members -Links $args[0].Members
}
}

if ($membersToRemove.Count -gt 0)
{
$CurrentParameters.Members = $membersToRemove
Invoke-ExoCommand -GlobalAdminAccount $GlobalAdminAccount `
-Arguments $CurrentParameters `
-ScriptBlock {
Remove-UnifiedGroupLinks -Identity $args[0].DisplayName -LinkType Members -Links $args[0].Members
}
}
$CurrentParameters.Members = $members
}

}
"DistributionList"
{
Expand Down Expand Up @@ -300,7 +355,8 @@ function Test-TargetResource
-DesiredValues $PSBoundParameters `
-ValuesToCheck @("Ensure", `
"DisplayName", `
"Description")
"Description", `
"Members")
}

function Export-TargetResource
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
function Get-TargetResource
{
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$DisplayName,

[Parameter()]
[System.String]
$PrimarySMTPAddress,

[Parameter()]
[ValidateSet("Present","Absent")]
[System.String]
$Ensure = "Present",

[Parameter(Mandatory = $true)]
[System.Management.Automation.PSCredential]
$GlobalAdminAccount
)
Write-Verbose "Get-TargetResource will attempt to retrieve information for Shared Mailbox $($DisplayName)"
$nullReturn = @{
DisplayName = $DisplayName
PrimarySMTPAddress = $null
GlobalAdminAccount = $null
Ensure = "Absent"
}

$mailboxes = Invoke-ExoCommand -GlobalAdminAccount $GlobalAdminAccount `
-ScriptBlock {
Get-Mailbox
}
$mailbox = $mailboxes | Where-Object {$_.RecipientTypeDetails -eq "SharedMailbox" -and $_.Identity -eq $DisplayName}

if(!$mailbox)
{
Write-Verbose "The specified Shared Mailbox doesn't already exist."
return $nullReturn
}
$result = @{
DisplayName = $DisplayName
PrimarySMTPAddress = $mailbox.PrimarySMTPAddress
Ensure = "Present"
GlobalAdminAccount = $GlobalAdminAccount
}
Write-Verbose "Found an existing instance of Shared Mailbox $($DisplayName)"
return $result
}

function Set-TargetResource
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$DisplayName,

[Parameter()]
[System.String]
$PrimarySMTPAddress,

[Parameter()]
[ValidateSet("Present","Absent")]
[System.String]
$Ensure = "Present",

[Parameter(Mandatory = $true)]
[System.Management.Automation.PSCredential]
$GlobalAdminAccount
)
Write-Verbose "Entering Set-TargetResource"
Write-Verbose "Retrieving information about Shared Mailbox $($DisplayName) to see if it already exists"
$currentMailbox = Get-TargetResource @PSBoundParameters

# CASE: Mailbox doesn't exist but should;
if ($Ensure -eq "Present" -and $currentMailbox.Ensure -eq "Absent")
{
Write-Verbose "Shared Mailbox $($DisplayName) does not exist but it should. Creating it."
Invoke-ExoCommand -GlobalAdminAccount $GlobalAdminAccount `
-Arguments $PSBoundParameters `
-ScriptBlock {
New-MailBox -Name $args[0].DisplayName -PrimarySMTPAddress $args[0].PrimarySMTPAddress -Shared:$true
}
}
# CASE: Mailbox exists but it shouldn't;
elseif ($Ensure -eq "Absent" -and $currentMailbox.Ensure -eq "Present")
{
Write-Verbose "Shared Mailbox $($DisplayName) exists but it shouldn't. Deleting it."
Invoke-ExoCommand -GlobalAdminAccount $GlobalAdminAccount `
-Arguments $PSBoundParameters `
-ScriptBlock {
Remove-Mailbox -Identity $args[0]..DisplayName -Confirm:$false
}
}
}

function Test-TargetResource
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$DisplayName,

[Parameter()]
[System.String]
$PrimarySMTPAddress,

[Parameter()]
[ValidateSet("Present","Absent")]
[System.String]
$Ensure = "Present",

[Parameter(Mandatory = $true)]
[System.Management.Automation.PSCredential]
$GlobalAdminAccount
)

Write-Verbose -Message "Testing Office 365 Shared Mailbox $DisplayName"
$CurrentValues = Get-TargetResource @PSBoundParameters
return Test-Office365DSCParameterState -CurrentValues $CurrentValues `
-DesiredValues $PSBoundParameters `
-ValuesToCheck @("Ensure", `
"DisplayName", `
"PrimarySMTPAddress")
}

function Export-TargetResource
{
[CmdletBinding()]
[OutputType([System.String])]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$DisplayName,

[Parameter(Mandatory = $true)]
[System.String]
$PrimarySMTPAddress,

[Parameter(Mandatory = $true)]
[System.Management.Automation.PSCredential]
$GlobalAdminAccount
)
$result = Get-TargetResource @PSBoundParameters
$content = "O365SharedMailbox " + (New-GUID).ToString() + "`r`n"
$content += "{`r`n"
$content += Get-DSCBlock -Params $result -ModulePath $PSScriptRoot
$content += "}`r`n"
return $content
}

Export-ModuleMember -Function *-TargetResource
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[ClassVersion("1.0.0.0"), FriendlyName("O365SharedMailbox")]
class MSFT_O365SharedMailbox : OMI_BaseResource
{
[Key, Description("The display name of the Shared Mailbox")] string DisplayName;
[Write, Description("The primary email address of the Shared Mailbox")] string PrimarySMTPAddress;
[Write, Description("Present ensures the group exists, absent ensures it is removed"), ValueMap{"Present","Absent"}, Values{"Present","Absent"}] string Ensure;
[Required, Description("Credentials of the SharePoint Global Admin"), EmbeddedInstance("MSFT_Credential")] string GlobalAdminAccount;
};

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Description

This resource allows users to create Office 365 Shared Mailboxes.
Loading

0 comments on commit badee47

Please sign in to comment.