public/Import-CloudAccount.ps1

<#PSScriptInfo
 
.VERSION 1.0.0.0
 
.GUID 64fb72f9-b11d-46d2-88b5-c68910eeebc1
 
.AUTHOR Mikail Aras - Meta10
 
.COMPANYNAME Meta10
 
.COPYRIGHT (C) 2024 by Meta10 - Alle Rechte vorbehalten
 
.TAGS Script PowerSHELL wrapper function New-CloudAccountUnattended
 
.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 account unattended
 
#>


function Import-CloudAccount {
    param(
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$Path,

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

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [datetime]$StartDate

    )

    # import additional modules
    $additionalmodules = [system.collections.generic.list[string]]::new()
    $additionalmodules.add("D:\CEC\script\Get-CloudCustomer.ps1")
    $additionalmodules.add("D:\CEC\script\New-CloudAccount.ps1")
    $additionalmodules.add("\\meta10.com\d2system\itservice\script\00_functions\function_new_password.ps1")
    $additionalmodules.add("\\meta10.com\d2system\itservice\script\00_functions\function_convert_umlauts.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($customername){
            $cloudcustomer = get-cloudcustomer -name $customername

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

                if(test-path -path $path){
                    $importexcel = import-excel -path $path -WorksheetName 'benutzer' -StartRow 12 -StartColumn '3' -EndColumn '4' -NoHeader

                    if($importexcel){
                        if($importexcel.count -gt 0){
                            $importexceltemp01 = $importexcel.where({![string]::IsNullOrEmpty($_.p1)}) # only get rows which are not empty

                            foreach($user in $importexceltemp01){
                                $error.clear()

                                remove-variable -name 'username', 'usernamemodified01' -ErrorAction 'silentlycontinue' -Force

                                if([string]::IsNullOrEmpty($user.p2)){
                                    [string]$username = $user.p1.substring(0, 4).tolower()
                                    [string]$lastname = $cloudcustomer.domain

                                }else{
                                    try{
                                        [string]$username = "$((convert-umlauts -inputobject $user.p1).substring(0, 2))$((convert-umlauts -inputobject $user.p2).substring(0, 2))"
                                        [string]$lastname = $user.p2

                                    }catch{
                                        write-error -message "couldnt build username with $($user.p1) and $($user.p2) due to following error $($error[-1].exception)"

                                    } # end of try catch
                                } # end of if string null or empty user.p2

                                if($username){
                                    if($username.length -eq 4){
                                        $usernamemodified01 = "$($cloudcustomer.accountnumber).$($username)"

                                        try{
                                            $null = New-CloudAccount -Name $usernamemodified01 -Type 'Named User' -CustomerName $cloudcustomer.name -FirstName $user.p1 -LastName $lastname -Password ((new-password).substring(0, 8)) -Datetime $startdate
                                            write-host "created cloud account $($usernamemodified01) for following customer $($customername)" -ForegroundColor 'yellow'

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

                                        } # end of try catch block
                                    }else{
                                        write-warning -message "username initials $($username) is longer than 4 characters. manually create the cloud account for following user $($user.p1) $($user.p2) with the command New-CloudAccount via dynamics UI"

                                    } # end of username length
                                }else{
                                    write-warning -message "username initials for following user $($user.p1) $($user.p2) is empty. please check if the excel content for this user is correct"

                                } # end of if username
                            } # end of foreach
                        }else{
                            write-warning -message "excel user count is 0"

                        } # end of if importexcel count
                    }else{
                        write-warning -message "excel is empty"

                    } # end of if importexcel
                }else{
                    write-warning -message "test path to excel file is unsuccessful"

                } # end of if test path
            }else{
                write-warning -message "cloud customer is empty"

            } # end of if cloud customer string null or empty

            #$null = $logal.add("configured dns client server address for interface alias $($ethif) with server addresses $($args[2]) and $($args[3])")
        }else{
            write-warning -message "customername is empty"

        } # end of if customername

        $logal

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

        } # end of foreach
    }else{
        write-warning -message "no crm connection"

    }
} # end of function