When upgrading licensing from the User Device (U/D) licenses to the Universal Hybrid Multi Cloud (UHMC), I noticed that there was a change on the report outcome. I would receive a divide by zero error. I was like I haven’t tried dividing by zero in a long time, so I guess it was my turn to do so. The link that was previously used to get license info below was no longer working.
https://api-us.cloud.com/licensing/license/enterprise/cloud/cvad/ud/current
This had to be updated because the new UHMC licenses are concurrent licenses and the link for the information on this is different.
https://api.cloud.com/licensing/license/enterprise/cloud/cvad/ccd/summary
This link has some different options to choose from.
I chose the monthPeak in order to get the high mark for the month. There are sub options there I use which are assignedLicenseCount and totalLicenseCount in the script.
In order to get it to work correctly, I had to add my CustomerID in the secureclient.csv and assign it as $CUSTOMER_ID = $creds.CustomerID in the script. You will also need your SDK installed as well as your secureclient.csv file for your credentials to run this. This assumes you want to run Monday through Friday. You can change the section regarding that if you want to run every single day.
When it runs, you get this output
asnp Citrix*
$Today = Get-Date
if(($Today.DayOfWeek) -eq 'Monday')
{$when = $Today.AddDays(-3)}
else{$when = $Today.AddDays(-1)}
$licenseIOwnCount = licensecount
$creds = import-csv "c:\scripts\Citrix\secureclient.csv"
$CLIENT_ID = $creds.ID
$CLIENT_SECRET = $creds.Secret
$CUSTOMER_ID = $creds.CustomerID
$tokenUrl = 'https://api-us.cloud.com/cctrustoauth2/root/tokens/clients'
$response = Invoke-WebRequest $tokenUrl -Method POST -Body @{
grant_type = "client_credentials"
client_id = $CLIENT_ID
client_secret = $CLIENT_SECRET
}
$token = $response.Content | ConvertFrom-Json
$headers = @{
Accept = "application/json"
Authorization = "CwsAuth Bearer=$($token.access_token)"
'Citrix-CustomerId' = $CUSTOMER_ID
}
$resourceLocUrl = "https://api-us.cloud.com/catalogservice/$CUSTOMER_ID/sites"
$response = Invoke-WebRequest $resourceLocUrl -Headers $headers
$content = $response.Content | ConvertFrom-Json
$siteID = $content.sites.id
$headers = @{
Accept = "application/json"
Authorization = "CwsAuth Bearer=$($token.access_token)"
'Citrix-CustomerId' = $CUSTOMER_ID
'Citrix-InstanceId' = $siteID
}
# Get Licensing Info
$response = Invoke-WebRequest "https://api.cloud.com/licensing/license/enterprise/cloud/cvad/ccd/summary" -Method Get -Headers $headers
$content = $response.Content | ConvertFrom-Json
$response.Content | ConvertFrom-Json | ConvertTo-Json -Depth 10
$licensingTotalCount = $content.monthPeak.totalLicenseCount
$licensingUsageCount = $content.monthPeak.assignedLicenseCount
$licensingRemaining = ($licenseIOwnCount - $licensingUsageCount)
$connections = Get-BrokerConnectionLog -BearerToken $headers.Authorization -Filter {BrokeringTime -gt $when} -MaxRecordCount 100000 | Select-Object BrokeringUserName
$billedUnits = import-csv "c:\scripts\Citrix\billedunits.csv"
$CitrixVDIConnected = (Get-BrokerSession -BearerToken $headers.Authorization -MaxRecordCount 100000 | Where-Object SessionSupport -eq "SingleSession" | Where-Object SessionState -eq "Active").count
$CitrixVDIDisconnected = (Get-BrokerSession -BearerToken $headers.Authorization -MaxRecordCount 100000 | Where-Object SessionSupport -eq "SingleSession" | Where-Object SessionState -eq "Disconnected").count
$ctxUsers = [PSCustomObject] @{
UniqueCitrixUsers = ($connections.BrokeringUserName | Select-Object -Unique).count
CurrentSessions = (Get-Brokersession -BearerToken $headers.Authorization -MaxRecordCount 100000 | Select-Object BrokeringUserName).count
CitrixVDISessions = $CitrixVDIConnected + $CitrixVDIDisconnected
CitrixLicensesUsed = $licensingUsageCount
CitrixTotalLicenses = $licensingTotalCount
CtxLicenseFreePercent = ((($licensingRemaining) / $licensingTotalCount ) * 100).ToString("#.##")
CtxRemainingLicenses = $licensingRemaining
CX10BilledUnits = $billedUnits.CitrixApp
VD10BilledUnits = $billedUnits.CitrixVDI
}
# HTML Formatting
$style = "<style>BODY{font-family: Arial; font-size: 10pt;}"
$style = $style + "TABLE{border: 1px solid black; border-collapse: collapse;}"
$style = $style + "TH{border: 1px solid black; background: #dddddd; padding: 5px; }"
$style = $style + "TD{border: 1px solid black; padding: 5px; }"
$style = $style + "</style>"
# HTML Email Body
$body = $ctxUsers | ConvertTo-Html -Head $style
# Generates email with attachment
$style = "<style>BODY{font-family: Arial; font-size: 10pt;}"
$style = $style + "TABLE{border: 1px solid black; border-collapse: collapse;}"
$style = $style + "TH{border: 1px solid black; background: #dddddd; padding: 5px; }"
$style = $style + "TD{border: 1px solid black; padding: 5px; }"
$style = $style + "</style>"
$body = $report | ConvertTo-Html -Head $style
$dateEmail = Get-Date -Format "MM-dd-yyyy"
$emailFrom = "EmailFrom@company.com"
$emailTo = "Someone@company.com"
$subject = "Daily Citrix User Check | $dateEmail"
$email = New-object System.Net.Mail.MailMessage
$email.to.Add($emailTo)
$email.From = New-Object system.net.Mail.MailAddress $emailFrom
$email.Subject = $subject
$email.IsBodyHtml = $true
$email.body = $body
$smtpserver = "mail.company.com"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($email)