Function New-Computer {
    <#
    .SYNOPSIS
        Creates a new computer account in DRA.

    .DESCRIPTION
        This function creates one or more new computer accounts in the McGuire AFB Computers OU within the DRA environment. It allows setting common attributes like location, description, and organization.

    .PARAMETER ComputerName
        The name(s) of the new computer(s) to create.

    .PARAMETER Organization
        The 'Organization' (o) attribute for the new computer object.

    .PARAMETER BuildingNum
        The building number, used to construct the 'Location' attribute.

    .PARAMETER RoomNum
        The room number, used to construct the 'Location' attribute.

    .PARAMETER Location
        The 'Location' attribute. If not specified, it is constructed from BuildingNum and RoomNum.

    .PARAMETER Description
        The 'Description' attribute for the new computer object.

    .PARAMETER SecurityGroup
        The security group that has permissions to join the computer to the domain. Defaults to "514 FSS SAShare Admins".

    .PARAMETER DraServer
        The FQDN of the DRA server. Defaults to "lackland.dra.us.af.mil".

    .EXAMPLE
        New-Computer -ComputerName 'NEWPC1' -Organization '514 FSS' -BuildingNum '123' -RoomNum 'A101' -Description 'New standard workstation'
        Creates a new computer account with the specified attributes.
    #>
    [CmdletBinding(SupportsShouldProcess=$true)]
    param(
        [Parameter(Mandatory=$true, Position=0)]
        [string[]]$ComputerName,

        [Parameter(Mandatory=$true)]
        [string]$Organization,

        [Parameter(Mandatory=$true)]
        [string]$BuildingNum,
        
        [Parameter(Mandatory=$true)]
        [string]$RoomNum,

        [string]$Location,
        
        [string]$Description,

        [string]$SecurityGroup = "514 FSS SAShare Admins",

        [string]$DraServer = "mcguire.dra.us.af.mil"
    )
    
    Begin {
        # Construct DNs and paths once
        $ouPath = ",OU=McGuire AFB Computers,OU=McGuire AFB,OU=AFCONUSWEST,OU=Bases,DC=AREA52,DC=AFNOAPPS,DC=USAF,DC=MIL"
        $accountToAddDn = "CN=$SecurityGroup,OU=McGuire AFB Security Groups,OU=McGuire AFB,OU=AFCONUSWEST,OU=Bases,DC=AREA52,DC=AFNOAPPS,DC=USAF,DC=MIL"
        
        # Default Location if not provided
        if ([string]::IsNullOrEmpty($Location)) {
            $Location = "BLDG:$BuildingNum;RM:$RoomNum"
        }
        
        $i = 1
        $total = $ComputerName.Count
    }

    Process {
        foreach ($computer in $ComputerName) {
            $percentComplete = ($i / $total) * 100
            Show-ProgressBar -Activity "Creating $computer" -PercentComplete $percentComplete

            $computerDn = "cn=$computer$ouPath"
            Write-Verbose "Attempting to create computer with DN: $computerDn"

            if ($pscmdlet.ShouldProcess($computerDn, "Create New Computer")) {
                try {
                    $properties = @{
                        DistinguishedName                 = $computerDn
                        Location                          = $Location
                        Description                       = $Description
                        AccountDisabled                   = $false
                        AccountThatCanAddComputerToDomain = $accountToAddDn
                        o                                 = $Organization
                        l                                 = "McGuire AFB"
                    }

                    Add-DRAComputer -ErrorAction Stop `
                        -Domain $env:USERDNSDOMAIN `
                        –DRARestServer $DraServer `
                        -DRARestPort 8755 `
                        –DRAHostServer $DraServer `
                        –DRAHostPort 11192 `
                        -Properties $properties

                    Write-Verbose "Successfully created computer: $computer"
                }
                catch {
                    Write-Warning "Could not create computer '$computer'. Error: $($_.Exception.Message)"
                }
            }
            $i++
        }
    }
    
    End {
        Show-ProgressBar -Activity "Operation Complete" -PercentComplete 100
    }
}
