Josh-CO Dev

Solving the worlds problems one line of code at a time.


1 Comment

Automating a WebInspect Scan using PowerShell and the WebInspect API

I have been struggling with the WebInspect API for quite some time now. It seems that I could always call the get methods just fine, but some of the post methods had some weird syntax that I could never figure out. After a lot of trial and error, I finally figured out how to use PowerShell to start a scan with the WebInspect API. I am posting this here hoping it will help out others. It seems that Google does not turn up anything on the API other than some HP help files which really do not help.

$wiapiScan = "http://vmwebinspect01:8083/webinspect/scanner" #set this to the location of your webinspect API instance

$body = @{
    settingsName="Default"    #the settings name to use for this scan
    
    overrides= 
    @{
        ScanName="Testing"    #The name to give the scan
        StartUrl="http://scrubbed"   #the url to scan
        CrawlAuditMode="CrawlOnly"   #crawl, audit, or both.
        StartOption="Url"    #refer to the API documentation for the other options
    } | convertto-json
} 

$response = Invoke-webrequest -Method Post -Body $body -Uri $wiapiScan #put it all together

Be sure that $wiapiScan is set to point to your WebInspect instance. Remember that the API has to be configured and started wherever you are hosting WebInspect. In my case, I am using a dedicated VM.


Leave a comment

PowerShell – Browser Automation

I have had a need to perform browser automation for some time. Apparently, this can be done pretty easily with PowerShell, providing you have a basic understanding of the DOM and how to manipulate it.

Start by create an Internet Explorer object. I know, I know. Internet Explorer. PowerShell is Microsoft, so just get over it. We all know it sucks.

#create a new browser object to handle all of our navigation
    $Browser = New-Object -com InternetExplorer.Application

If you want to watch the automation, which you do because it’s cool, be sure to set the visible property to true.

#go ahead and show the browser window
    $Browser.visible = $true

To browse to a specific url, just use the navigate method.

#Navigate to the URL Defined above
    $Browser.navigate("www.google.com") 

In some cases, you need to tell the thread to sleep for an arbitrary amount of time. Otherwise the script will just keep running and you won’t accomplish much. I put a statement similar to this after almost every call.

#Sleep the thread while the page is loading
    Start-Sleep -Milliseconds 1000;

To click on an item, you need to know it’s name in the DOM

$Browser.Document.getElementByID("overridelink").Click()

You can set the value of a text-box

$Browser.Document.getElementById("tbName").value = $reportName

Check a box

$Browser.Document.getElementById("CheckBoxList1_1").checked = $true

There’s a lot more that can be done. Play around with the Browser.Document properties a little bit. In the end, it’s a lot easier to use a tool like Auto IT, but it’s cool to script out some automation.


Leave a comment

PowerShell – Delete folders older than a certain timeframe based on a filename

I was recently asked to come up with a script that will go through a directory and delete any folders based on a naming convention. The naming convention was something like 20150730…….The obvious pattern here is YYYYMMDD……. Now that we know this, a loop and some substrings are our friend.

$directory = "directory to purge"

Get-ChildItem $directory | Where-Object {$_.PSIsContainer} | ForEach-Object {
    $DirName = $_.Name

    $DirYear = $_.Name.Substring(0,4)
    $DirMonth = $_.Name.Substring(4,2)
    $stringDate = $DirMonth + "/01/" + $DirYear

    $FolderDTime = [datetime]$stringDate
    
    $Today = get-date

    $timespan = NEW-TIMESPAN –Start $Today –End $FolderDTime

    if ($timespan.Days -lt -365) {
        Remove-Item $_.FullName
        write-output ((get-date).ToShortDateString() + " - " + $DirName + " was deleted") | out-file -append "C:\LogRhythmArchives\Inactive\del_archives_log.txt"
    }

} 

So, let’s break this down.

Get-ChildItem $directory | Where-Object {$_.PSIsContainer} | ForEach-Object {

This is our for loop. This does a Get-ChildItem of $directory. Be sure to set this variable to the folder you want to loop through. The where object is a weird powershell way of saying, exclude files and only give me directories.

    $DirName = $_.Name
    $DirYear = $_.Name.Substring(0,4)
    $DirMonth = $_.Name.Substring(4,2)
    $stringDate = $DirMonth + "/01/" + $DirYear

First, take our goofy name and throw it into a variable. Use a substring to pull the first four characters to make the year, then similarly for the month. Now we use concatenation to build a fake date.

$FolderDTime = [datetime]$stringDate

$Today = get-date

    $timespan = NEW-TIMESPAN –Start $Today –End $FolderDTime

Now, we can take our fake date string and tell powershell to force it into a date time object. Now that we have this, we can create a timespan object to determine the length of time between the file name and today.

    if ($timespan.Days -lt -365) {
        Remove-Item $_.FullName
        write-output ((get-date).ToShortDateString() + " - " + $DirName + " was deleted") | out-file -append "del_archives_log.txt"
    }

Finally, we check to see if the timespan is greater than 365 days. They way I did the timespan creation, we get left with a negative number so we check less than. Now we can call remove-item to permanently delete the file, and write our output to a log.

Please note, remove-item does not send the items to the recycle bin. It is a permanent deletion.