Hun Pham

Powershell with Qualys VM API

Discussion created by Hun Pham on May 22, 2017
Latest reply on May 23, 2017 by Hun Pham

After searching long on for information and examples of the Quayls VM API, I found there was lacking examples and information. So i am posting mine to maybe help some else.  Hope this helps the community. As always please check the script before running it.  Please have meaningful comments on way I can improve the script.  

Thank you.

 

cls

#Change these variables to fit your settings
$Global:qualysUsername = "uuuuuuuuu" #Please Change
$Global:qualysPassword = "ppppppppp" #Please Change
$Global:reportType = "ZZZZ" #pdf,html,mht,xml, or csv
$hostName = "aaaaaaaaaaa"

#Verify that the host is online by ping
if (test-Connection -ComputerName $hostname -count 1)
{
break
write-host "$hostName is online"
Get-QualysVMReport
}
else
{
Write-Host "$hostName is not Pingable"
}

function Get-QualysVMReport {
#Defining the variables
[string]$Global:scannerName = "QuaylsScanner"
[string]$Global:ipAddress = ([System.Net.Dns]::GetHostAddresses($hostName) | Where {$_.IPAddressToString -ne "::1"}).IPAddressToString #ip address to scan

$currentDate = Get-Date -Format "dMMMyyyy HHmm"
$Global:title = "Qualys" + "$currentDate "
$Global:vmScanProfile = "#######" #Default Scanner ID
$Global:vmReportProfile = "######" #Default Report ID
$Global:Outfile = "$title" +".$reportType"

#declare url and working with powershell
$Global:headers = @{"X-Requested-With"="powershell"}
$Global:baseUrl = "https://qualysapi.qualys.com/api/2.0/fo"
$Global:body = "action=login&username=$qualysUsername&password=$qualysPassword"

#Login and create an open session
Invoke-RestMethod -Headers $headers -Uri "$baseurl/session/" -Method Post -Body $body -SessionVariable websession

#Launch New Vunerability Management Scan
Invoke-RestMethod -Headers $headers -Uri "$baseUrl/scan/?action=launch&scan_title=$title&ip=$ipAddress&option_id=$pcScanProfile&iscanner_name=$scannerName" -Method Post -WebSession $webSession

#Check to see if Scan Complete based on scan title.
function Get-ScanStatus {
Write-Host "Checking on Status of the Scan."
$Global:latestQualysScan = (Invoke-RestMethod -Headers $headers -Uri "$baseurl/scan?action=list" -WebSession $webSession).SelectNodes("//SCAN[contains(TITLE, '$title')]")
if ($latestQualysScan.Status.OuterXml -like "*Finished*"){
Write-Host "Scan is Finished"}
else {
start-sleep -Seconds 30
write-host "Scan still Running"
Get-ScanStatus
}
}

Get-ScanStatus

#Launch New Report base from the Scan
Invoke-RestMethod -Headers $headers -Uri "$baseUrl/report/?action=launch&template_id=$pcReportProfile&report_title=$title&output_format=$reportType&ips=$ipAddress" -Method Post -WebSession $webSession

#Check to see if Report Complete
function Get-ReportStatus {
Write-Host "Checking on Status of the Report."
$Global:latestQualysReport = (Invoke-RestMethod -Headers $headers -Uri "$baseurl/report?action=list" -WebSession $webSession).SelectNodes("//REPORT[contains(TITLE, '$title')]")
if ($latestQualysReport.Status.OuterXml -like "*Finished*"){
Write-Host "Scan is Finished"
$global:reportID = $latestQualysReport.id}
else {
start-sleep -Seconds 30
write-host "Report still Running"
Get-ReportStatus
}
}
Get-ReportStatus

#Download the Report File
Invoke-RestMethod -Headers $headers -Uri "$baseUrl/report/?action=fetch&id=$reportID" -Method Post -WebSession $webSession -OutFile $outFile

#Logout
Invoke-RestMethod -Headers $headers -Uri "$baseurl/session/" -Method Post -Body "action=logout" -WebSession $webSession
}

Outcomes