Josh-CO Dev

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


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.

Advertisements


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.


Leave a comment

XNA – Sprite Sheet Animation

Dug this article out from the old archives and thought I would post it here. Context might not make sense, but the basic code should work all the same. You can tie this into the tile engine tutorial.

Haven’t had too much time to do any programming this week, but I did just convert my player animation engine to animate a sprite based on a sprite sheet rather than a horizontal imageset. Here is the relevant code.

I realize that this needs to be more object oriented, but I’ll get there.
This is added to the animated sprite class:

public int YFrame
        {
            get { return yFrame; }
            set { yFrame = value; }
        }
        int yFrame = 0;

All the rest of the code is in the player class

private void AnimateLeft(GameTime gt)
        {
            if (currentKBState != previousKBState)
            {
                //CurrentFrame = 5;
                CurrentFrame = 0;
                YFrame = 1;
            }

            Timer += (float)gt.ElapsedGameTime.TotalMilliseconds;

            if (Timer > Interval)
            {
                CurrentFrame++;

                //if (CurrentFrame > 7)
                    //CurrentFrame = 4;
                if (CurrentFrame > 2)
                    CurrentFrame = 0;

                Timer = 0f;
            }
        }

        private void AnimateRight(GameTime gt)
        {
            if (currentKBState != previousKBState)
            {
                //CurrentFrame = 9;
                CurrentFrame = 0;
                YFrame = 2;
            }

            Timer += (float)gt.ElapsedGameTime.TotalMilliseconds;

            if (Timer > Interval)
            {
                CurrentFrame++;

                //if (CurrentFrame > 11)
                   //CurrentFrame = 8;
                if (CurrentFrame > 2)
                    CurrentFrame = 0;

                Timer = 0f;
            }
        }

SourceRect = new Rectangle(CurrentFrame * SpriteWidth, YFrame * SpriteHeight, SpriteWidth, SpriteHeight);

And that’s it. Just use the code that we had from the previous articles, and the animation will now pull from the sprite sheet.


Leave a comment

The Convergence of Programming and Security

In my previous post, we talked about the basics of application security and how only 60% of corporations have implemented a DAST tool set. The scary part is that most corporations who are implementing basic application level security start with DAST. What this really means is that only 60% of corporations have even started to implement basic application security principles. Obviously, some companies are much further along and have implemented other technologies such SAST, glass box testing, and secure coding guidelines, but these are the minority.
Continue reading