functions/general/Invoke-TmfTenant.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
function Invoke-TmfTenant
{
    <#
        .SYNOPSIS
            Invoke required actions for all configured resources.
        
        .DESCRIPTION
            This command applies the desired configuration to the Tenant you are connected to.
            You can connect to a Tenant using Connect-MgGraph.

        .PARAMETER Exclude
            Exclude resources from invoking.
            For example: -Exclude groups, users

        .PARAMETER DoNotRequireTenantConfirm
            Do not ask for confirmation when invoking configurations.
    #>

    Param (
        [ValidateSet({$script:supportedResources.Keys()})]
        [string[]] $Exclude,
        [switch] $DoNotRequireTenantConfirm
    )
    
    begin
    {
        Test-GraphConnection -Cmdlet $PSCmdlet
        $tenant = Get-MgOrganization -Property displayName, Id        
    }
    process
    {
        Write-PSFMessage -Level Host -FunctionName "Invoke-TmfTenant" -String "TMF.TenantInformation" -StringValues $tenant.displayName, $tenant.Id        
        if (-Not $DoNotRequireTenantConfirm) {
            if ((Read-Host "Is this the correct tenant? [y/n]") -notin @("y","Y"))    {
                Write-PSFMessage -Level Error -String "TMF.UserCanceled"
                throw "Connected to the wrong tenant."
            }
        }        
        
        foreach ($resourceType in ($script:supportedResources.GetEnumerator() | Where-Object {$_.Value.invokeFunction -and $_.Name -notin $Exclude} | Sort-Object {$_.Value.weight})) {            
            if ($script:desiredConfiguration[$resourceType.Name]) {
                Write-PSFMessage -Level Host -FunctionName "Invoke-TmfTenant" -String "TMF.StartingInvokeForResource" -StringValues $resourceType.Name                    
                & $resourceType.Value["invokeFunction"] -Cmdlet $PSCmdlet
            }                        
        }
    }
    end
    {
    
    }
}