public/winvs/New-CloudCustomerEnvironment_winvs.ps1

<#PSScriptInfo
 
.VERSION 1.0.0.0
 
.GUID 2087fb86-bb93-40b6-a918-c3860000942e
 
.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.04.10 |mike |Script erstellt.
 
.PRIVATEDATA
 
#>


<# Requires -Module powershell crm classes #>

<#
 
.DESCRIPTION
create new cloud customer environment for winvs
 
#>


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

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

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

    # import additional modules
    $additionalmodules = [system.collections.generic.list[string]]::new()
    $additionalmodules.add("D:\CEC\script\Get-CloudCompany.ps1")
    $additionalmodules.add("D:\CEC\script\New-CloudCompany.ps1")
    $additionalmodules.add("D:\CEC\script\Add-CloudCompany.ps1")
    $additionalmodules.add("D:\CEC\script\New-CloudCustomer.ps1")
    $additionalmodules.add("D:\CEC\script\Set-CloudCustomer.ps1")
    $additionalmodules.add("D:\CEC\script\Get-CloudCustomer.ps1")
    $additionalmodules.add("D:\CEC\script\Get-CloudServer.ps1")
    $additionalmodules.add("D:\CEC\script\New-CloudServer.ps1")
    $additionalmodules.add("D:\CEC\script\Add-CloudServer.ps1")
    $additionalmodules.add("D:\CEC\script\Set-CloudServer.ps1")
    $additionalmodules.add("D:\CEC\script\New-CloudServerCredential.ps1")
    $additionalmodules.add("\\meta10.com\d2system\itservice\script\00_functions\function_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

    if(Test-CCCRMConnection){
        if($name){
            $cloudcustomer = get-cloudcustomer -name $name

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

                if($cloudcustomer){
                    Set-CloudCustomer -Name $cloudcustomer.name -Domain $name

                }

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

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

            if(!([string]::IsNullOrEmpty($cloudcustomer.name))){
                $error.clear()

                $cloudcompany = get-cloudcompany -name $name

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

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

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

            # vm role mapping dictionary
            # create cloud servers
            [string]$prefix = "56795-$($name.replace('56795-', ''))" # prefix for server name removing the initial parameter prefix

            $serverdictionary = [system.collections.generic.dictionary[string, string]]::new()
            $serverdictionary.add("$($prefix)-dc01", 'active directory')
            $serverdictionary.add("$($prefix)-crm01", 'crm')
            $serverdictionary.add("$($prefix)-sp01", 'sharepoint')
            $serverdictionary.add("$($prefix)-adfs01", 'adfs')

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

                switch -regex ($server.key.split('-')[-1]){
                    'dc' {$os = 'windows server 2022 core standard'}
                    'crm' {$os = 'windows server 2022 standard'}
                    'sp' {$os = 'windows server 2022 standard'}
                    'adfs' {$os = 'windows server 2022 standard'}
                } # end of switch

                $cloudserver = get-cloudserver -name $server.key -status 'active'

                if([string]::IsNullOrEmpty($cloudserver.name)){
                    remove-variable -name 'cloudserver' -ErrorAction 'SilentlyContinue'

                    # create new cloud server
                    $cloudserver = New-CloudServer -Name $server.key -Role $server.value -OperatingSystem $os -PrimaryServer $primaryserver -replicaserver $replicaserver

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

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

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

                    $null = Add-CloudServer -Name $cloudserver.name -CustomerName $name

                }
            } # end of foreach

            # create crm cloud users
            foreach($server in ($serverdictionary.GetEnumerator().where({$_.Key -match 'crm' -or $_.key -match 'sp' -or $_.key -match 'adfs'}))){
                switch -regex ($server.key.split('-')[-1]){
                    'crm' {
                        $cloudserver = get-cloudserver -name '56795-90002-crm01' -status 'active'

                        if($cloudserver){
                            $cloudcredentials = $cloudserver.cloudcredentials()

                            [string]$newpassword = new-password
                            foreach($cloudcredential in $cloudcredentials.where({$_.username -notmatch 'administrator'})){
                                # create cloud server credential
                                $null = New-CloudServerCredential -Name $server.key -UserName $cloudcredential.username -Password $newpassword -CredentialType 'securecloud'

                            }
                        } # end of if cloudserver
                    }
                    'sp' {
                        $cloudserver = get-cloudserver -name '56795-90002-sp01' -status 'active'

                        if($cloudserver){
                            $cloudcredentials = $cloudserver.cloudcredentials()

                            [string]$newpassword = new-password
                            foreach($cloudcredential in $cloudcredentials.where({$_.username -notmatch 'administrator'})){
                                # create cloud server credential
                                $null = New-CloudServerCredential -Name $server.key -UserName $cloudcredential.username -Password $newpassword -CredentialType 'securecloud'

                            }
                        } # end of if cloudserver
                    }
                    'adfs' {
                        $cloudserver = get-cloudserver -name '56795-90002-adfs01' -status 'active'

                        if($cloudserver){
                            $cloudcredentials = $cloudserver.cloudcredentials()

                            foreach($cloudcredential in $cloudcredentials.where({$_.username -notmatch 'administrator'})){
                                # create cloud server credential
                                $null = New-CloudServerCredential -Name $server.key -UserName $cloudcredential.username -Password (new-password) -CredentialType 'securecloud'

                            }
                        } # end of if cloudserver
                    }
                } # end of switch
            } # end of foreach server

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

        $logal

        foreach($log in $output){
            #write-customlog -path $pathtolog -message $log -level 'info'

        } # end of foreach
    }
} # end of function