public/New-CloudCustomerEnvironment.ps1

<#PSScriptInfo
 
.VERSION 1.0.0.0
 
.GUID a4c7a6f6-8e3f-4c01-934e-0f9e204cb110
 
.AUTHOR Mikail Aras - Meta10
 
.COMPANYNAME Meta10
 
.COPYRIGHT (C) 2024 by Meta10 - Alle Rechte vorbehalten
 
.TAGS Script PowerSHELL function_connect_exchangeonline_unattended
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
Aenderungsverlauf des Scripts nach dem Schema Major.Minor.Build.Revision,jeweils Major Versionen sind produktiv zu verwenden
Version |Type |Datum |Benutzer |Bemerkungen
1.0.0.0 |BUILD |2024.02.04 |mike |Script erstellt.
 
.PRIVATEDATA
 
#>


<# Requires -Module powershell crm classes #>

<#
 
.DESCRIPTION
create new cloud customer environment
 
#>


function New-CloudCustomerEnvironment {
    param(
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$Name,

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$CompanyName,

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$PrimaryServer,

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$ReplicaServer,

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [int16]$VlanId
    )

    # import additional modules
    $additionalmodules = [system.collections.generic.list[string]]::new()
    $additionalmodules.add("$($PSScriptRoot)\public\Get-CloudCustomer.ps1")
    $additionalmodules.add("$($PSScriptRoot)\public\New-CloudCustomer.ps1")
    $additionalmodules.add("$($PSScriptRoot)\public\Get-CloudCompany.ps1")
    $additionalmodules.add("$($PSScriptRoot)\public\New-CloudCompany.ps1")
    $additionalmodules.add("$($PSScriptRoot)\public\Add-CloudCompany.ps1")
    $additionalmodules.add("$($PSScriptRoot)\public\Get-CloudServer.ps1")
    $additionalmodules.add("$($PSScriptRoot)\public\New-CloudServer.ps1")
    $additionalmodules.add("$($PSScriptRoot)\public\Add-CloudServer.ps1")
    $additionalmodules.add("$($PSScriptRoot)\public\Set-CloudServer.ps1")
    $additionalmodules.add("$($PSScriptRoot)\public\New-CloudServerCredential.ps1")
    $additionalmodules.add("$($PSScriptRoot)\public\New-CloudAccount.ps1")
    $additionalmodules.add("$($PSScriptRoot)\public\Set-CloudCustomer.ps1")
    $additionalmodules.add("$($PSScriptRoot)\public\New-Password.ps1")

    foreach($additionalmodule in $additionalmodules){
        #Unblock-File -Path $additionalmodule
        import-module -name $additionalmodule -force
        write-host "importing module $($additionalmodule)" -ForegroundColor Green

    }

    $logal = [system.collections.arraylist]::new() # log arraylist
    $null = $logal.add("function new-cloudcustomerenvironment")
    $null = $logal.add("start parameters: $($name), $($companyname), $($primaryserver), $($replicaserver)")

    if(Test-CCCRMConnection){
        $null = $logal.add("test-cccrmconnection successful")
        if($name){
            $cloudcustomer = get-cloudcustomer -name $name

            if([string]::IsNullOrEmpty($cloudcustomer.name)){
                $cloudcustomer = new-cloudcustomer -name $name

            }else{
                write-host "following cloud customer $($clouducustomer.name) already exists" -ForegroundColor 'yellow'

            } # end of if cloudcustomer.name is null or empty

            if(!([string]::IsNullOrEmpty($cloudcustomer.name))){
                $null = $logal.add("found cloud customer $($cloudcustomer.name)")

                # set cloud customer domain
                try{
                    Set-CloudCustomer -Name $cloudcustomer.name -Domain $cloudcustomer.name
                    write-host "set cloud customer domain $($cloudcustomer.name)" -ForegroundColor 'yellow'
                    $null = $logal.add("set cloud customer domain $($cloudcustomer.name)")

                }catch{
                    write-error -message "couldnt set cloud customer domain $($cloudcustomer.name) due to following error $($error[-1].exception)"
                    $null = $logal.add("couldnt set cloud customer domain $($cloudcustomer.name) due to following error $($error[-1].exception)")

                } # end of try catch block

                if($vlanid){
                    # set cloud customer vlanid
                    try{
                        $cloudcustomer.SetCustomerVlanId($VlanId)
                        write-host "set cloud customer vlanid $($vlanid)" -ForegroundColor 'yellow'
                        $null = $logal.add("set cloud customer vlanid $($vlanid)")

                    }catch{
                        write-error -message "couldnt set cloud customer vlanid $($vlanid) due to following error $($error[-1].exception)"
                        $null = $logal.add("couldnt set cloud customer vlanid $($vlanid) due to following error $($error[-1].exception)")
                    } # end of try catch block
                }else{
                    write-warning -message "vlanid is null or empty"

                } # end of if vlanid

                $error.clear()

                $cloudcompany = get-cloudcompany -name $companyname

                # assign cloud company
                if($cloudcompany){
                    if([string]::IsNullOrEmpty($cloudcustomer.accountnumber)){
                        $cloudcompanyadded = Add-CloudCompany -Name $cloudcompany.name -CustomerName $name
                        $null = $logal.add("added cloud company $($cloudcompany.name)")

                    }else{
                        write-host "cloud company $($cloudcompany.name) already assigned to following customer $($cloudcustomer.name)" -ForegroundColor 'yellow'
                        $null = $logal.add("cloud company $($cloudcompany.name) already assigned to following customer $($cloudcustomer.name)")

                    } # end of if cloudcustomer account number is null or empty
                } # end of if cloudcompany

                # vm role mapping dictionary
                # create cloud servers
                $serverdictionary = [system.collections.generic.dictionary[string, string]]::new()
                $serverdictionary.add("$($cloudcompany.accountnumber)-dc01", 'active directory')
                $serverdictionary.add("$($cloudcompany.accountnumber)-mc01", 'management')
                $serverdictionary.add("$($cloudcompany.accountnumber)-file01", 'file')
                $serverdictionary.add("$($cloudcompany.accountnumber)-gw01", 'gateway')
                $serverdictionary.add("$($cloudcompany.accountnumber)-sh01", 'session host')
                $serverdictionary.add("$($cloudcompany.accountnumber)-sg01", 'security gateway')

                foreach($server in $serverdictionary.GetEnumerator()){
                    [string]$os = ''

                    switch -regex ($server.key.split('-')[-1]){
                        'dc' {$os = 'windows server 2022 core standard'}
                        'mc' {$os = 'windows server 2022 standard'}
                        'file' {$os = 'windows server 2022 core standard'}
                        'gw' {$os = 'windows server 2022 standard'}
                        'sh' {$os = 'windows server 2022 standard'}
                        'sg' {$os = 'vyos'}
                    }

                    # create new cloud server
                    $cloudserver = New-CloudServer -Name $server.key -Role $server.value -OperatingSystem $os -PrimaryServer $primaryserver -replicaserver $replicaserver
                    $null = $logal.add("created cloud server $($cloudserver.name)")

                    if($server.key -notmatch '-sg'){
                        # create cloud server credential
                        $null = New-CloudServerCredential -Name $cloudserver.name -UserName 'Administrator' -Password (new-password) -CredentialType 'local'

                    }else{
                        $null = New-CloudServerCredential -Name $cloudserver.name -UserName 'secadmin' -Password "m2XQbaxJQLHwfWJU" -CredentialType 'local'

                    } # end of if cloudserver key is not match sg

                    $null = $logal.add("created cloudservercredential for $($cloudserver.name)")

                    $null = Add-CloudServer -Name $cloudserver.name -CustomerName $name
                    $null = $logal.add("assigned cloud server $($cloudserver.name) to customer $($name)")

                } # end of foreach
            } # end of if crmserver

            $null = $logal.add("configured dns client server address for interface alias $($ethif) with server addresses $($args[2]) and $($args[3])")

            # new cloud account test user
            [string]$testusername = "$($cloudcustomer.accountnumber).test"

            if($cloudcustomer.begin){
                [datetime]$begin = ([datetime]::Parse($cloudcustomer.begin))

            }else{
                [datetime]$begin = [DateTime]::Now.AddDays(-(Get-Date).Day + 1)

            } # end of if cloudcustomer begin

            try{
                $null = New-CloudAccount -Name $testusername -Type 'Test User' -CustomerName $cloudcustomer.name -FirstName 'Test' -LastName 'User' -Password ((new-password).substring(0, 8)) -Datetime $begin
                write-host "created cloud account $($testusername) for following customer $($customername)" -ForegroundColor 'yellow'
                $null = $logal.add("created cloud account $($testusername) for following customer $($customername)")

            }catch{
                write-error -message "couldnt create cloud account with username $($usernamemodified01) due to following error $($error[-1].exception)"
                $null = $logal.add("couldnt create cloud account with username $($usernamemodified01) due to following error $($error[-1].exception)")

            } # end of try catch block
        } # end of if computername

        $logal
    }
} # end of function