PS to remember Part 2

Building Directories with PowerShell

I needed to create a script that will build out directories following a pattern. In this case, Microsoft Security Updates. Perfect opportunity to practice a little coding. The script needs to be functioning in an all Windows environment, and easy to maintain. Sadly, this eliminated Python, which is more interesting to me, but PowerShell is a very close second. And despite my desire to not be a Windows guy, the almighty paycheck comes from a company that builds a product that runs exclusively on Windows. So away we go(sanitized where appropriate).

# get current year from system and format for directories
$year = get-date -format yyyy
$year_short = get-date -f yy
$month = get-date -format MMMM
$month_short = get-date -format MM

# constants; current month name and prefix
$parent_dir = "$month_short - $month"
$prefix = "\MS$year_short-"

# actual, live working directory
$checkifdir = "\\servername.domain\software\patches\$year\$parent_dir"
# testing/debugging directory
#$checkifdir = "C:\Users\wkopp\Desktop\temp\sandbox\$year\$parent_dir"

# check if current month directory exists, if not, create (w/loop)
# this happens without user input

if ($checkifdir -eq $false){
md $checkifdir

# ask user for range of this month's bulletins(user input)
# create the names for directories needed.
# create directories

$st = Read-Host "Please enter the number of the first bulletin: "
$end = Read-Host "Please enter the number of the last bulletin: "

$bulletin_range = $st..$end

for ($i = 0; $i -lt $bulletin_range.length; $i++){
$string_name = $prefix + $bulletin_range[$i].ToString("000")
$folder = $checkifdir + $string_name

Write-Host $folder

if ($folder -ne $false){
md $folder

PS to remember Part 1

Reading through this link Raphael Mudge talks about using rogue applications called notepad.exe to call back out, and then drops this tidbit(emphasis mine):

netstat -nab is a tool to help you discover rogue notepad.exe instances connecting to the internet

the output of which looks something like this:

I think to myself, this is a fantastic tool to use for troubleshooting, however, the default output is huge. I need to pare it down a bit. In Bash, I would just pipe to grep and be done. I’m very new at PowerShell, but it seems overly optimistic to thing it has grep.

After a bit of searching, no, there’s no grep. However, somewhere in the StackExchange network there was a more appropriate solution. “Out-String” and “Select-String”. Mixing all that together gave me the following:

netstat -nab | Out-String -Stream | Select-String -pattern “ESTABLISHED” -context 1

So what does all that mean, exactly?

netstat: “Displays protocol statistics and current TCP/IP network connections

    -n : shows addresses and ports as numerical infomation
    -a : all connections and ports
    -b : show executable involved

Out-String: Sends objects as strings (pipes the output of netstat as strings instead of data)

    -stream : sends each string individually rather than concatenating to a single string

Select-String: …You can use it like Grep in UNIX…

    -Pattern “” : inside the quotes goes what you’re looking to filter with
    -context # : this is the number of lines after your match that you want to return.

So, as a baby step into PowerShell and learning how it is not Bash, this was fun. More of these to come as I get better at it.