Sample PowerShell Code to Get Host Details

This Appendix section attached the sample codes to receive the Software Vulnerability Host Details as shown below:

Sample PowerShell Code

#

#Fetch Host Details

#

$Site = ( "Account", "https://csi7.secunia.com/csi/api/","username=user_name&password=*********")

$global:QueryLimit = 10000

$global:WebServiceHeader = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"

$global:WebServiceHeader.Add("Content-Type", 'application/x-www-form-urlencoded')

$global:URL = $Site[1]

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

$global:ErrorArray = @()

function GetData ($URL, $Retry, $Post, $Body)

{

$result = @()

$Count = 0

while ($Count -lt $Retry)

{

try

{

$Count++

if ($Post)

{

$result = Invoke-RestMethod -Uri $URL -Method Post -Headers $global:WebServiceHeader -Body $Body -WebSession $global:Session

}

else

{

$result = Invoke-RestMethod $URL -Method Get -Headers $global:WebServiceHeader -TimeoutSec 10 -WebSession $global:Session

}

$result.data

$Count = $Retry

}

catch

{

Start-Sleep -s 2

if ($Count -eq $Retry)

{

$global:ErrorArray += ("Error GetData " + $URL + " " + $_.Exception.Message + " " + $_.Exception.ItemName+ " " + $_.Exception.Status + " " + $_.Exception.Response)

Write-Host "Timeout Exceeded and Exhausted Retries" -ForegroundColor Red

}

else

{

Write-Host "Timeout Exceeded -- will retry in 2 sec" -ForegroundColor Yellow

}

}

}

return $result

}

function QueryData ($Post, $Token, $URL, $Body)

{

# Get First Page of results (25 items)

[int] $Start = 0

[int] $Limit = 11

[int] $CurrentTotal = -1

$Total = 0

$results = @()

while ($CurrentTotal -lt $Total)

{

$CurrentTotal = $CurrentTotal + $Limit

$FullURLGet = $global:URL + "?uid=" + $Token + $URL + "&start=" + [string]$Start + "&limit=" + [string]$Limit

$FullURLPut = $global:URL + "?uid=" + $Token + $URL

$BodyFull = $Body + "&start=" + [string]$Start + "&limit=" + [string]$Limit

try

{

if ($Post)

{

$result = GetData $FullURLPut 5 $Post $BodyFull

if ($result)

{

$results = $results + $result

}

}

else

{

$result = GetData $FullURLGet 5 $Post $Body

if ($result.rows)

{

$results = $results + $result.rows

}

if ($result -and $result.rows)

{

$results = $results + $result

}

}

[string]$TotalString = $result.total;

$Total = [int]$TotalString.Trim(" ");

if ($results.Count -gt $global:QueryLimit)

{

break;

}

}

catch

{

$global:ErrorArray += ("Error QueryData2 " + $result.next + " " + $_.Exception.Message + " " + $_.Exception.ItemName)

return $results

}

$Start = $Start + $Limit

}

$results = $results | ? {$_}

return $results

}

function GetUserToken ($Cred)

{

$Data = Invoke-WebRequest -Uri ($global:URL + "?action=manuallogin") -Body $Cred -Method Post -Headers $global:WebServiceHeader -SessionVariable 'global:Session'

if ($Data.StatusCode -eq 200)

{

$Response = ConvertFrom-Json $Data.Content

return $Response.uid

}

return ""

}

$Token = GetUserToken $Site[2]

if (![string]::IsNullOrWhiteSpace($Token))

{

$Data = QueryData $False $Token "&action=smart_groups&which=getSmartGroupContents&smartGroupTextType=host&smartGroupId=1"

$Count = 0

$Data | Format-Table -Property host_name, num_insecure, num_eol, num_patched, num_installations, nsi_device_id, score

$Data2 = QueryData $False $Token "&action=hosts&which=get_host_scan_results&device_id=14&dir=ASC&dir=ASC&insecure=true&patched=true"

$Data2 | Format-Table -Property product_name, version, state, vuln_id, vuln_title

}