Skip to content

Instantly share code, notes, and snippets.

@lansalot
Last active September 29, 2020 07:56
Show Gist options
  • Save lansalot/a8e4addeb4043b96c9acade3dcbe2871 to your computer and use it in GitHub Desktop.
Save lansalot/a8e4addeb4043b96c9acade3dcbe2871 to your computer and use it in GitHub Desktop.
[OutputType("PSAzureOperationResponse")]
$LogAnalyticsWorkspaceId = "<as it sounds>"
$LogAnalyticsPrimaryKey = "<primary key>"
$HostPool = "<name of host pool>"
$RGName = "<resource group host pool is in>"
$connection = Get-AutomationConnection -Name AzureRunAsConnection
[void](Connect-AzAccount `
-ServicePrincipal `
-Tenant $connection.TenantID `
-ApplicationId $connection.ApplicationID `
-CertificateThumbprint $connection.CertificateThumbprint)
Try {
$ActiveSessions = (Get-AzWvdUserSession -HostPoolName $HostPool -ResourceGroupName $RGName | Where {$_.SessionState -eq 'Active'}).Count
$DisconnectedSessions = (Get-AzWvdUserSession -HostPoolName $HostPool -ResourceGroupName $RGName | Where {$_.SessionState -eq 'Disconnected'}).Count
Write-Output "Logging $($ActiveSessions) active sessions and $($DisconnectedSessions) disconnected sessions"
$body_obj = @{
'hostpoolName' = $HostPool
'ActiveSessions' = $ActiveSessions
'DisconnectedSessions' = $DisconnectedSessions
}
$json_body = ConvertTo-Json -Compress $body_obj
Send-OMSAPIIngestionFile -customerId $LogAnalyticsWorkspaceId -sharedKey $LogAnalyticsPrimaryKey -Body $json_body -logType 'WVDSessions_CL'
} catch {
Write-Output "Error in script"
$_
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment