page contents

About the Post

Author Information

Using Variables in PowerShell

In my last article, I talked about using variables in PowerShell. Today, I want to talk about a problem that I encountered and solved using a variable to help me present the data for analysis.

Over the years, I have a lot of friends call on me for assistance when their computers stopped working as expected. Most of the time, the concern with was some sort of network connectivity issue, like not being able to access a web site. I have a script that would gather basic troubleshooting information to assist me when my friends would call for help. I recently updated it, which you will see below, if you are interested, here is what it used to look like earlier this year.

For some reason networking issues can be a more difficult topic to troubleshoot with someone remotely, since most of the conversation may feel like we have 2 people talking a completely different language. I am not sure why, but people really get uncomfortable typing basic commands or even running a packet capture with wireshark, even though there are a lot of great and helpful Youtube videos. We just make better decisions when we have data that supports our theory, so I do my best to gather it.

Here are the commands that I gather to help me determine most issues for network connectivity:

  • ipconfig /all
  • route print
  • systeminfo
  • net statistics workstation
  • netstat -aon
  • tcping
  • tracetcp
  • Get-WmiObject -Class Win32_BIOS -ComputerName yourcomputername
  • Portqry -n yourcomputername -o 88,135,139,445,636,3268,3269 -p BOTH

Tools that are needed to be downloaded, since these are not native to Microsoft Windows:

Before I being, lets download these files and then set up the environment variables, so that we do not have to browse to the folder each time. Instead we can just open a command prompt anywhere and just type the command and it will run. For these tools, I normally add them files to a folder called Nettools in C:\Program Files.

Then I will add them to the System variables as follows:

  1. Open Control Panel and search for System and then click on Edit the system environment variables:

  2. Click on Environment Variables

  3. Under System variables (bottom section) look for Path and then select edit

  4. Add ;C:\Program Files\Nettools to the Variable value field (the “;” should be seen like the word and, which allows you to add another thought or item)

Now that we have the environment variables setup, we can use run the command from anywhere on our system:

Notice that we are in the directory C:\Users\StangSCT (where the files are not located) instead of C:\Program Files\Nettools (where the files are actually located) As you can see running one command is pretty easy to run and look at the results, but when you are running multiple commands like the none examples that I listed above it can be more of a pain. To make it worse, it is harder to view the command and if you tried to copy the commands to notepad to view, then it can look rather odd. (Seen below with just 2 commands)

In order to capture these 2 outputs, I would have to scroll down to get all of the information and then I have to make sure I copied enough text to the right of the screen to avoid cutting off text. What I do to avoid that mess is I run all of these commands in PowerShell and out put the results to a text file. (As seen below)

Now I have modified the commands to output to a text file and also grouped them with “;” to be quickly run in PowerShell as one set of commands:

ipconfig /all >C:\Users\stangsct\Documents\Text\tshoot.txt; route print >>C:\Users\stangsct\Documents\Text\tshoot.txt; systeminfo >>C:\Users\stangsct\Documents\Text\tshoot.txt; net statistics workstation >>C:\Users\stangsct\Documents\Text\tshoot.txt; netstat -aon >>C:\Users\stangsct\Documents\Text\tshoot.txt; Get-WmiObject -Class Win32_BIOS -ComputerName slave1 >>C:\Users\stangsct\Documents\Text\tshoot.txt; Portqry -n slave1 -o 88,135,139,445,636,3268,3269 -p BOTH >>C:\Users\stangsct\Documents\Text\tshoot.txt

The commands would run but the problem was that PortQry would never put the output in the text file and instead it would fail with a generic syntax suggestion. (It acted like I asked the command for help) SO I tried to run the command by itself

I verified that I was using the latest version of PortQry (version 2.0) and that there was no other copy on my system. I tried running the command by itself in PS and it would failed, even though it worked in CMD with no issues. Then I tried opening a CMD process in my script to launch PortQRY and output the text, but it came back in gibberish. I also checked for my version of PowerShell (using Get-Host) to see if there were any compatibility issues with PortQry.

I found this article after a friend suggested that this tool might be too old to work. After researching, it became clear that the issue was the format of the range of ports I was using to check for active directory network issues. PowerShell just did not like this format of numbers and commas all backed up like this: 88,135,139,445,636,3268,3269.

Basically, all I had to do was generate the file into a variable and then have the variable output to a text file.

ipconfig /all >C:\Users\stangsct\Documents\Text\tshoot.txt; route print >>C:\Users\stangsct\Documents\Text\tshoot.txt; systeminfo >>C:\Users\stangsct\Documents\Text\tshoot.txt; net statistics workstation >>C:\Users\stangsct\Documents\Text\tshoot.txt; netstat -aon >>C:\Users\stangsct\Documents\Text\tshoot.txt; Get-WmiObject -Class Win32_BIOS -ComputerName slave1 >>C:\Users\stangsct\Documents\Text\tshoot.txt; $result = iex “cmd.exe /c `”Portqry -n slave1 -o 53,88,135,139,445,636,3268,3269 -p BOTH`””;$result >>C:\Users\stangsct\Documents\Text\tshoot.txt

So, outputting the file to a variable allowed PowerShell to capture the data form the PortQry command and then instructing the variable to place the results to the text file worked. The issue was not that PortQry was too old to use or even a compatibility issues between the programs. I hope this article helps you if you ever run into an issue where you are gathering a bunch of commands and saving them to a text file for analysis.

One Response to “Using Variables in PowerShell”

  1. manoj k #

    Great article and simple to follow

    December 9, 2013 at 5:38 AM
Copy Protected by Chetan's WP-Copyprotect.