# ============================================================================== # Cayosoft License Counter version 1.0.11 # Copyright Cayosoft Inc., 2024 # Use this tool to count the number of licenses required by Cayosoft products # to manage your Active Directory domains and/or Microsoft 365 tenant. # # If you have any questions, please contact Cayosoft at sales@cayosoft.com. # ============================================================================== param($ADCredential, $OfficeCredential, $AzureEnvironment) $adPrams = @{} $offPrams = @{} if($Null -ne $ADCredential){ $adPrams["Credential"] = $ADCredential } if($Null -ne $OfficeCredential){ $offPrams["Credential"] = $OfficeCredential } if(-not [string]::IsNullOrWhiteSpace($AzureEnvironment)){ $offPrams["AzureEnvironment"] = $AzureEnvironment } function IsExoEnabledPlan($accountSku, $exoPlans){ if ($null -eq $exoPlans -or $null -eq $accountSku -or $null -eq $accountSku.ServiceStatus -or $accountSku.ServiceStatus.Count -eq 0) { return $false } $exoServices = $accountSku.ServiceStatus | ?{ $null -ne $_ -and $null -ne $_.ServicePlan -and $null -ne $_.ServicePlan.ServiceName -and $exoPlans.ContainsKey($_.ServicePlan.ServiceName) } return ($null -ne $exoServices -and $exoServices.Count -gt 0) } function OfficeCollectManagedObjects(){ $exoPlans = @{ "EXCHANGE_B_STANDARD" = $null; "EXCHANGE_L_STANDARD" = $null; "EXCHANGE_S_ESSENTIALS" = $null; "EXCHANGE_S_STANDARD_MIDMARKET" = $null; "EXCHANGE_S_DESKLESS" = $null; "EXCHANGE_S_ENTERPRISE" = $null; "EXCHANGE_S_ENTERPRISE_GOV" = $null; "EXCHANGE_S_STANDARD" = $null; "EXCHANGE_S_STANDARD_GOV" = $null; "EXCHANGE_S_DESKLESS_GOV" = $null; } try { $result = @{"Licenses"=@()} $result.Licenses = (Get-MsolAccountSku | ?{ (IsExoEnabledPlan $_ $exoPlans) }) return $result } catch { Write-Error "OfficeCollectManagedObjects failed: $($_)"; } return $null } $global:b = @() $global:result2=@() $global:collectAD = $False $global:collectOffice = $False function Main() { $workingMode = Read-Host "Select your environment: 1 - Active Directory only 2 - Azure AD / Microsoft 365 only 3 - Active Directory and Azure AD / Microsoft 365 Enter 1, 2 or 3" Write-Host "" if($workingMode -eq '1' -or $workingMode -eq '3'){ $collectAD = $True } if($workingMode -eq '2' -or $workingMode -eq '3'){ $global:collectOffice = $True } if($workingMode -ne '1' -and $workingMode -ne '2' -and $workingMode -ne '3') { Write-Error "Enter 1, 2 or 3 to select your environment." } $a = @() if($collectOffice -eq $True){ Write-Host "Provide credentials for your Azure AD / Microsoft 365 tenant ..." Connect-MsolService @offPrams Write-Host "Collecting information about Azure AD / Microsoft 365 tenant ..." $tenatName = '' $hybridCount = 0 $O365Info = OfficeCollectManagedObjects if ($null -ne $O365Info -and $null -ne $O365Info.Licenses -and $O365Info.Licenses.Count -gt 0){ $tenatName = (Get-MsolAccountSku)[0].AccountName + ".onmicrosoft.com" $O365Info.Licenses | %{ if($Null -eq $_){ return } $hybridCount += ($_.ActiveUnits + $_.WarningUnits) $item = New-Object PSObject -Property @{ System = $tenatName SKU = $_.SkuPartNumber Counter = ($_.ActiveUnits + $_.WarningUnits) } $global:b += $item } $item = New-Object PSObject -Property @{ Name = 'Exchange Online Licenses' System = $tenatName Counter = $hybridCount } $a += $item $global:result2 = ($b | select System,SKU,Counter) } } if($collectAD -eq $True){ try{ if($Null -eq $ADCredential){ Write-Host "Provide credentials for your Active Directory domain or forest ..." $adPrams["Credential"] = (Get-Credential -Message "Specify Active Directory credentials") } Write-Host "Collecting information about Active Directory domains ..." $totalCount = 0 $forest = Get-ADForest -Current LoggedOnUser @adPrams foreach($dom in $forest.Domains){ try{ $dcForDomain = Get-ADDomainController -DomainName $dom -Discover -Service ADWS,PrimaryDC -NextClosestSite -ForceDiscover if($Null -eq $dcForDomain -or $Null -eq $dcForDomain.HostName){ Write-Error "Error on getting DC: $dom" continue } Write-Host "Collecting information Domain: $dom DC: $($dcForDomain.HostName[0])" $domainAD = Get-ADDomain -Identity $dom -Server ($dcForDomain.HostName[0]) -ErrorAction Ignore @adPrams if($Null -ne $domainAD){ $usersCount = ([Int32](Get-ADUser -LDAPFilter '(&(objectCategory=person)(objectClass=user)(!userAccountControl:1.2.840.113556.1.4.803:=2))' -SearchBase $domainAD.DistinguishedName -Server ($dcForDomain.HostName[0]) @adPrams).Count) $item = New-Object PSObject -Property @{ Name = 'AD Enabled Users' System = $dom Counter = $usersCount } $totalCount += $usersCount $a += $item } }catch{ Write-Error "Domain: $dom | Error: $($Error[0])" } } if($totalCount -gt 0){ $item = New-Object PSObject -Property @{ Name = 'Total AD Enabled Users' System = 'All AD domains' Counter = $totalCount } $a += $item } }catch{ Write-Error "Error: $($Error[0])" } } Write-Host "" return $a } function MainRun{ $fileName = "$($env:USERPROFILE)\Desktop\" + "CayosoftLicenseCount_" + $(Get-Date -format "yyyyMMddHHmmss") + ".txt" Write-Host '============================================================================== Cayosoft License Counter version 1.0.11 Copyright Cayosoft Inc., 2024 Use this tool to count the number of licenses required by Cayosoft products to manage your Active Directory domains and/or Microsoft 365 tenant. If you have any questions, please contact Cayosoft at sales@cayosoft.com. ----------------------------------------------------------------------------- ' # TODO: pre-create filename and tell customer. # check with required level of privileges and error. $result = Main | select Name,System,Counter Write-Host "See results below and in the TXT file: " Write-Host $fileName Write-Host '-----------------------------------------------------------------------------' $result | FT if($collectOffice -eq $True){ $result2 | select System,SKU,Counter | FT } $result | out-file $fileName $result2 | out-file $fileName -Append } MainRun ####