The humble variable

Powershell has mushroomed recently into almost every facet of systems administration. From Exchange servers to system information management, IT professionals without programming backgrounds are working with scripts more than ever.

The ability to automate almost every aspect of operations is tantalizing. But before we unleash an automated process on critical infrastructure, we should understand what it does.

To that end, let’s look at a fundamental idea: the variable.

Remember when we talked about arrays last month? A series of containers to store your stuff that could be referenced by name or item number?

The variable is even simpler. A variable is a single container that you create. All it has is a name. What is in the container could stay static, or it could change. Think of it like a bucket with a name written on it. The variable name stands for everything in the bucket.

Variables are commonly used in a couple of ways.

The first is to store a value temporarily. This is often present in browser-side scripting like Javascript. If the operations performed on the page don’t really require a big database or even an array, why not just create one bucket to hold that value?

In Javascript, PHP or Powershell, the variable comes into existence as soon as you declare it.

$mybucket = "The text string I'm storing in the variable called mybucket";

In one line, we’ve created a new variable called mybucket and put something inside of it: a string of text. We could just as easily fill it with an integer, a URL or a filename on our web server. From this moment forward, the entire text string can be represented simply by the variable name. Want to change the text in your form field?

document.getElementByID("mytextfield").value = $mybucket;

In Powershell and PHP, declaring a variable also starts with a $ sign.

$rcpath = "c:\users\jimmyjoe\appdata\local\microsoft\outlook\roamcache";

Want to write a script that works with Jimmy Joe in accounting’s Outlook cache for Autocomplete entries, but don’t feel like typing out the entire path every time you want to reference it? Your friend the variable has your back.

So when you’re staring down the unforgiving scramble of an administration script with a bunch of $ signs in it, don’t panic. They’re only buckets, and now you know a little bit more about what they do.

The Array – Your Closet Organizer

There comes a time when you have a lot of stuff and need to put it somewhere. But you also need to easily access each piece of stuff in a hurry. Enter the array, which is sort of like a shelf or an organizer for your closet. An array is a container with many compartments.

The array has a name and stores each item inside. Each item is referred to by a number, starting with 0. Say we wanted an array to hold the names of cars. In Javascript, it looks like this:

carnames = new Array("ferrari","bugatti","porsche","volkswagen");

In this case, the name of the array is carnames. The array contains four items. They’re referenced by numbers 0, 1, 2 and 3. The numbering of array items starts with zero.

When you want to use one of the items from your closet organizer, you can call it by the name of the array and its corresponding number, like this:

carnames[0]

Say you had a page element that you wanted to pull in the name ferrari. Your page element is called label. Your request could look something like this:

document.getElementById("label").value = carnames[0];

Arrays can be used to store text strings, numerical values and even file names. Say you had an image on your page that you wanted to swap out with others from a selection of images. Your array could look this way:

pictures = new Array("img/picture1.jpg","img/picture2.jpg","img/picture3.jpg");

Your element on the page is called imgdrop. A reference to swap the image for the file called picture2.jpg could look like this:

document.getElementById("imgdrop").src = pictures[1];

Arrays are a wonderful tool for many uses. You can read more about them in this handy tutorial!

Better Wi-Fi: Your Favourite Band

At its heart, Wifi is data over radio link. We use this same technology to communicate with ships and satellites. But different frequencies have different behaviours. Any band we choose to use will have benefits and drawbacks.

Many of the current crop of wireless routers can broadcast in both 2.4GHz and 5GHz radio bands.

2.4GHz has been in use for much longer by consumer networking equipment. Its effective range without any obstructions is 100 metres. In perfect conditions, it can achieve data transfer rates of 54 Mbps. Perfect conditions almost never occur, with the average down towards 25 Mbps, a little slow by current networking standards.

But 2.4GHz has many benefits. The longer the wavelength, the easier it penetrates solid objects. Cellular networks here in Canada use 850-2100MHz, far lower on the radio spectrum. Using these much lower bands, you can make a phone call while farther away from a tower, behind intervening buildings or inside your house. Your 2.4GHz wireless router will not reach you across town, but it has some distance.

Like every useful and awesome thing, it is soon discovered by many others. The current state of 2.4GHz is like a crowd of people in a parking lot, each with their own speaker blasting a different kind of music. The more wireless routers or access points near your own equipment, the harder it is for your laptop/tablet/phone to hear your own wireless network over the noise.

I live in an apartment building. Here is the 2.4GHz capture from my desk:

2-4ghz

The readings on the left are signal strength. My own network is marked in blue. You can see several other access points broadcasting at similar strengths, including the Shaw access point mounted on the hallway ceiling.

This is one of the reasons other bands were opened up for Internet use and manufacturers began to build wireless equipment using 5GHz. While the range is shorter with 5GHz, the data transfer rates are much higher, with claims of 1.3 Gbps under perfect conditions. As previously noted, the 5GHz band doesn’t deal with interfering objects like walls so well. But in an home network where your laptop/tablet/phone isn’t far from the access point, this becomes less of an issue.

So which band should you be using to take your wireless network to the top level of performance? If your wireless router and devices are both capable of 5GHz and your environment supports its use (devices close to access points, not many obstructions), then 5GHz is a preferable choice to avoid the band clogging currently happening in 2.4GHz.

But if your network or environment can’t support use of the 5GHz band, all is not lost. In a future post on channels, we’ll look at how to tailor your 2.4GHz broadcast depending on what’s around you.

Automating backups with Beyond Compare 4

Beyond Compare 4 by Scooter Software is handy for syncing folders. Many people use this application to sync data from servers to local storage devices. But hold on to your chair, my friends. Their folder sync can be automated through a simple script.

How It Works

load "f:\" "\\192.168.0.47\Storage Backup\F"
sync mirror:left->right

The first line states the local source folder and the destination folder on your storage device.

The second line syncs files from the left folder to the right folder — nothing crazy.

This script can be saved in a text file and run as an automated task as often as you like. If you want a log of the results, you can add another line.

log normal "c:\myscripts\synclog-f.txt"

This saves a log with any failed files, the timestamp and if the script completed. Now you can use the time you spent manually syncing folders for more important activities, like reading blogs.

Scaling elements for responsive design with CSS

CSS gives us all kinds of options for sizing text and elements. If you are new to cascading style sheets, read more here to find out about their uses and benefits. They will make your sites faster, let you update page layouts more easily, help you lose fat, build muscle and just generally become more attractive.

Responsive designs need to flex to the screen size of many different devices, from large monitors to small phones. This has led to the current trend in basic and flat designs. The layout needs to work on a 1920-pixel wide monitor just as well as on a 320-pixel wide iPhone screen. Simple is better in this instance. Few companies have the time or desire to maintain separate purpose-built websites, although it does happen.

The easiest way previously was to size elements using percentages. Say we have an h1 heading inside our div with the id of “toast”. In our style sheet, we’ll say #toast h1{width:80%;}. This sizes the h1 to 80% of the width of the parent element, our div called toast. But the parent element is also flexing to different screen sizes, and what if that parent element is fitting to a percentage of its parent, and so on up the chain?

This leads to unpredictable results, and we’re really more concerned about the size of our h1 in relation to the actual screen.

CSS3 has helped us out here by introducing four different viewport percentage units.

  • vh – 1% of viewport height
  • vw – 1% of viewport width
  • vmin – 1% of whatever is smaller, viewport width or height
  • vmax – 1% of whatever is larger, viewport width or height

The viewport referred to is the size of our actual screen. So the iPhone previously mentioned has a viewport width of 320 points. Scaling text at font-size:10vw; will size the text to 10% of the viewport width, or 32 points. If we were to scale the text with font-size:10vmax, it would size the text to 10% of the larger size. In the iPhone 5’s case, the text would be 56.8 points.

Using this set of viewport sizing units can help you hit on the best resizing factor for different elements in your site or app. Find out more at:

https://www.w3.org/TR/css3-values/#viewport-relative-lengths

How to remove unwanted Windows 10 apps

Windows 10 is proving stable and useful. But what if you want a cleaner environment? What if you don’t like Bing News? What if you don’t want to Bing anything?

Powershell can help! If you open this command line tool and enter Get-AppxPackage, it returns a list of currently installed apps under your user account.

You can also export the resulting list to a text file with Get-AppxPackage | Out-File c:\users\youareawesome\desktop\yourfilename.txt.

These are not just full-blown programs as seen under Programs & Features, but smaller applications built into Windows. These include both critical pieces like the .NET framework and less useful apps like Bing Food and Drink. The second category are the apps that haunt your Start menu live tiles.

What you’re looking for in the resulting list is the value in PackageFullName for the app you want to weed out. You can even zero in further by using wildcard text in the package call. Get-AppxPackage *bing* returns any apps with “bing” in the name text.

Let’s assume the Bing Health and Fitness app is not going to put more broccoli in the fridge or increase your overall deadlift. Let’s get rid of it.

We copy the PackageFullName of the app. In this case, it is Microsoft.BingHealthAndFitness_3.0.4.336_x64__8wekyb3d8bbwe.

This helps build our overall command in Powershell to remove it:

Get-AppxPackage Microsoft.BingHealthAndFitness_3.0.4.336_x64__8wekyb3d8bbwe | Remove-AppxPackage

If the command runs successfully, no errors show and the cursor displays a second time. Bing Health and Fitness has been removed from your system, but only within the current user account. It still lurks elsewhere.

To remove the app for all users of your machine, our command needs the switch -AllUsers. It goes into our command behind the first cmdlet, like so:

Get-AppxPackage -AllUsers Microsoft.BingNews_4.11.156.0_x86__8wekyb3d8bbwe | Remove-AppxPackage

Running this command for all users requires administrative privileges on your system. If you’re a local administrator and the command is still denied, try running Powershell as administrator.

If you’d like to try it out, you can download our sample Powershell script: Remove Windows 10 Apps. This removes some of the most common third-party apps from Windows 10 for all users. Feel free to modify in the same format to clear out whichever apps you want.

Happy cleaning!

Better Wi-Fi: Check your environment

We’d all love fast and seamless wireless that never has problems. Most of us would also like a winning lottery ticket, a pony and a trip to Hawaii if wishes are being granted.

But better Wi-Fi is not so impossible when keeping in mind how the technology works. Receiving wireless Internet depends heavily on a strong radio signal between your device and the access point. Your surrounding environment is the biggest single influence on the quality of your wireless radio signal.

Here are some questions to ask yourself.

What objects are between me and the access point?

Radio frequencies are affected by some objects and can reflect off of them or scatter. Dense objects can block the signal. So can metal that forms any type of grid or lattice, creating a Faraday cage effect. This means wireless signals are vulnerable to blockage by metal-framed walls or rebar within concrete. Radio frequencies are also affected by electrical and magnetic fields, or other strong radio transmitters.

In the case of outdoor wireless networks at marinas, navigation equipment on vessels can interfere with wireless radio signals.

Do I have a clean line of sight to the access point?

This builds on the previous solution. Move or position your equipment to reduce the amount and density of interfering objects between your device and the access point.

How far am I from the access point?

Currently, wireless Internet operates on both the 2.4 GHz and 5 GHz spectrum. The official range of older 2.4GHz transmitters is 100 metres, but actual performance is heavily affected by your environment. Newer transmitters using the 802.11n protocol officially reach up to 400 metres at either 2.4 GHz or 5 GHz, but again this is affected by what is around you. Ensure you are within the broadcast range of the access point.

Checking your surroundings is only the first step in wireless troubleshooting, but awareness of your environment alone can greatly reduce the severity of connection issues.

How to find your IP address in Windows

There are many different situations where you’ll need to know the IP address of your Windows machine.

A quick way to find it is to use the Command Prompt application. Search Windows for command prompt, or type run in the search box and run cmd.

In the command prompt window, type ipconfig.

Ipconfig displays the current state of your network adapters. The Ethernet adapter is for the cable connected to your machine. The Wireless LAN or Wi-Fi adapter is for your wireless card. They operate independently and can each have their own IP address.

Under each adapter name, you’ll see your current IPv6 and IPv4 addresses. In the case below, the current address of the Wireless LAN adapter is 192.168.1.68.

ipconfig
ipconfig

At this time, IPv4 is still the standard for addressing on home and small business networks. IPv6 will eventually take its place entirely due to the much greater range of possible addresses.

This method to find your IP address is simple and works from Windows XP all the way through to Windows 10.

How to shorten if/else statements in Javascript

If there’s ever a programming concept that should be carved on a stone tablet, it’s the if/else statement.

If something is true, do this. If it’s not, do something else. An if/else statement is foundational. We’re telling the system to make a choice.

But the syntax can sometimes be long and clunky. Solving smaller conditions with a big if/else statement is like patching that big scrape on your car with a can of spray paint. It works, but it could be a lot better.

While building the compound interest calculator, I finally learned a shorter way that the rest of the world has probably been using for years.

Enter the ternary.

It does the same job and sounds way cooler when you tell your friends about it.

Say we want to find out if a number is something other than zero. We want to write a different result on the page depending on what the number is. In ancient times, I would use this:

if(number == 0){

document.write(“This number is just a zero.”);

}else{

document.write(“This number is definitely not zero, it is definitely a value!”);

}

And if there are multiple conditions you’re looking for, this method still works.

But what if I told you there was a shortcut? The ternary checks a single condition and does different things if it is true or false.

Condition ? First action : Second action;

The part before the ? checks our condition. In the example above, that would be number == 0. After the ? Come our options for action. The first action will be performed if the preceding condition is true. The second action will be performed if the condition is false, just like our else statement.

Our example would look like this in a ternary:

number = 0 ? document.write(“This number is just a zero.”) : document.write(“This number is definitely not zero, it is definitely a value!”);

Simple and shorter. Ternaries are especially useful when you’re changing numerical values or dealing with a lot of variables and conditions.

You can read much more about our friend the ternary operator here through the Mozilla Developer Network.