SDNLogCollector.ps1.txt

# Fill in the following information
$uri = 'https://sdn-pod06.tailwindtraders.com'
$WorkspaceID = "5d5465e7-67f9-4d00-bb32-06c0b2674d9b"
$SharedKey = "tllWLVuof236UBPQW196J3Sunr7y9wTmzXF65IylE5/eOBdIy2Y4TOkm7Q3bdZcIm8Zsy4te0memRwspPEgVbQ=="
$LogType = "SDN"
 
# Create the function to create the authorization signature
Function Build-Signature ($WorkspaceID, $sharedKey, $date, $contentLength, $method, $contentType, $resource)
{
    $xHeaders = "x-ms-date:" + $date
    $stringToHash = $method + "`n" + $contentLength + "`n" + $contentType + "`n" + $xHeaders + "`n" + $resource
 
    $bytesToHash = [Text.Encoding]::UTF8.GetBytes($stringToHash)
    $keyBytes = [Convert]::FromBase64String($sharedKey)
 
    $sha256 = New-Object System.Security.Cryptography.HMACSHA256
    $sha256.Key = $keyBytes
    $calculatedHash = $sha256.ComputeHash($bytesToHash)
    $encodedHash = [Convert]::ToBase64String($calculatedHash)
    $authorization = 'SharedKey {0}:{1}' -f $WorkspaceId,$encodedHash
    return $authorization
}
  
# Create the function to create and post the request
Function Post-LogAnalyticsData($WorkspaceID, $sharedKey, $body, $logType)
{
    $method = "POST"
    $contentType = "application/json"
    $resource = "/api/logs"
    $rfc1123date = [DateTime]::UtcNow.ToString("r")
    $contentLength = $body.Length
    $signature = Build-Signature `
        -WorkspaceId $WorkspaceID `
        -sharedKey $sharedKey `
        -date $rfc1123date `
        -contentLength $contentLength `
        -method $method `
        -contentType $contentType `
        -resource $resource
    $uri = "https://" + $WorkspaceId + ".ods.opinsights.azure.com" + $resource + "?api-version=2016-04-01"
 
    $headers = @{
        "Authorization" = $signature;
        "Log-Type" = $logType;
        "x-ms-date" = $rfc1123date;
    }
 
    $response = Invoke-WebRequest -Uri $uri -Method $method -ContentType $contentType -Headers $headers -Body $body -UseBasicParsing
    return $response.StatusCode
 
}
 
 
    
 
# main
 
$logNetworks = Get-NetworkControllerLogicalNetwork -ConnectionUri $uri
$vNets = Get-NetworkControllerVirtualNetwork -ConnectionUri $uri
$vServers = Get-NetworkControllerVirtualServer -ConnectionUri $uri
$nics = Get-NetworkControllerNetworkInterface -ConnectionUri $uri
$servers = Get-NetworkControllerServer -ConnectionUri $uri
$lb = Get-NetworkControllerLoadBalancer -ConnectionUri $uri
$lbMux = Get-NetworkControllerLoadBalancerMux -ConnectionUri $uri
$lbConfig = Get-NetworkControllerLoadBalancerConfiguration -ConnectionUri $uri
$acls = Get-NetworkControllerAccessControlList -ConnectionUri $uri
$ncCreds = Get-NetworkControllerCredential -ConnectionUri $uri
$pips = Get-NetworkControllerPublicIpAddress -ConnectionUri $uri
$udrs = Get-NetworkControllerRouteTable -ConnectionUri $uri
$gateways = Get-NetworkControllerGateway -ConnectionUri $uri
$gatewayPools = Get-NetworkControllerGatewayPool -ConnectionUri $uri
$virtualgateways = Get-NetworkControllerVirtualGateway -ConnectionUri $uri
 
 
# Format data
 
# Format Logical Networks
 
$logNetworkFMT = @()
 
foreach ($logNetwork in $logNetworks) {
 
    $logNetworkFMT += [pscustomobject]@{
 
        Name = $logNetwork.ResourceRef
        ResourceID = $logNetwork.ResourceId
        NetworkVirtualizationEnabled = $logNetwork.Properties.NetworkVirtualizationEnabled
        ProvisioningState = $logNetwork.Properties.ProvisioningState
        Subnets = $logNetwork.Properties.Subnets
        VirtualNetworks = $logNetwork.Properties.VirtualNetworks
 
    }
 
}
 
# Format Virtual Networks
$vNetsFMT = @()
foreach ($vNet in $vNets) {
 
 
 
    $vNetsFMT += [pscustomobject]@{
 
 
 
    }
 
}
 
# Format Virtual Servers
$vServersFMT = @()
foreach ($vServer in $vServers) {
 
    $vServersFMT += [pscustomobject]@{
 
        Name = $vServer.ResourceRef
        ResourceID = $vServer.InstanceId
        Certificate = $vServer.Properties.Certificate
        ProvisioningState = $vServer.Properties.ProvisioningState
        VMGuid = $vServer.Properties.VMGuid
        Connections = $vServer.Properties.Connections.ManagementAddresses
 
    }
 
}
 
 
# Format Network Interfaces
$nicsFMT = @()
foreach ($nic in $nics) {
 
    $nicsFMT += [pscustomobject]@{
 
        Name = $nic.ResourceRef
        ResourceID = $nic.ResourceId
        PrivateMacAddress = $nic.Properties.PrivateMacAddress
        PrivateMacAllocationMethod = $nic.Properties.PrivateMacAllocationMethod
        InternalDnsNameLabel = $nic.Properties.InternalDnsNameLabel
        ProvisioningState = $nic.Properties.ProvisioningState
        IsHostVirtualNetworkInterface = $nic.Properties.IsHostVirtualNetworkInterface
        ConfigurationState = $nic.Properties.ConfigurationState.Status
        DetailedInfo = $nic.Properties.ConfigurationState.DetailedInfo
 
    }
 
 
}
 
 
# Format Servers
$serversFMT = @()
foreach ($server in $servers) {
 
    $serversFMT += [pscustomobject]@{
 
        Name = $server.Properties.Connections.ManagementAddresses.ToString()
        ResourceID = $server.ResourceId
        ProvisioningState = $server.Properties.ProvisioningState
        ConfigurationState = $server.Properties.ConfigurationState.Status
        ConfigurationStateLastUpdated = $server.Properties.ConfigurationState.LastUpdatedTime
        ConfigurationStateDetailedInfo = $server.Properties.ConfigurationState.DetailedInfo
        NetworkInterfaces = $server.Properties.NetworkInterfaces.ResourceID
 
    }
 
}
 
# Format load balancers
 
 
 
$NetworkControllerInfo = New-Object -TypeName PSObject
$NetworkControllerInfo | Add-Member -MemberType NoteProperty -Name NCURI -Value $uri
$NetworkControllerInfo | Add-Member -MemberType NoteProperty -Name LogicalNetworks -Value $logNetworkFMT
$NetworkControllerInfo | Add-Member -MemberType NoteProperty -Name VirtualNetworks -Value $vNetsFMT
$NetworkControllerInfo | Add-Member -MemberType NoteProperty -Name VirtualServers -Value $vServersFMT
$NetworkControllerInfo | Add-Member -MemberType NoteProperty -Name NetworkInterfaces -Value $nicsFMT
$NetworkControllerInfo | Add-Member -MemberType NoteProperty -Name Servers -Value $vServersFMT
$NetworkControllerInfo | Add-Member -MemberType NoteProperty -Name LoadBalancers -Value $lb
$NetworkControllerInfo | Add-Member -MemberType NoteProperty -Name LoadBalancerMux -Value $lbMux
$NetworkControllerInfo | Add-Member -MemberType NoteProperty -Name LoadBalancerConfig -Value $lbConfig
$NetworkControllerInfo | Add-Member -MemberType NoteProperty -Name NetworkSecurityGroups -Value $acls
$NetworkControllerInfo | Add-Member -MemberType NoteProperty -Name NCCredentials -Value $ncCreds
$NetworkControllerInfo | Add-Member -MemberType NoteProperty -Name PublicIPs -Value $pips
$NetworkControllerInfo | Add-Member -MemberType NoteProperty -Name UserDefinedRoutes -Value $udrs
$NetworkControllerInfo | Add-Member -MemberType NoteProperty -Name Gateways -Value $gateways
$NetworkControllerInfo | Add-Member -MemberType NoteProperty -Name GatewayPools -Value $gatewayPools
$NetworkControllerInfo | Add-Member -MemberType NoteProperty -Name VirtualGateways -Value $virtualgateways
 
$json = $NetworkControllerInfo | ConvertTo-Json -Depth 100 -Compress
 
Post-LogAnalyticsData -WorkspaceID $WorkspaceID -sharedKey $sharedKey -body ([System.Text.Encoding]::UTF8.GetBytes($json)) -logType $logType