Techtip: One-liner to get free space

Say you’d like to copy an exuberant amount of data from one server to another and you’re unsure if the target disk has sufficient space. Perhaps you’d simply like to know how much space you’ve got.¬† There are several way to check this, but here’s one more, and in my opinion the fastest:

Run this one-liner from any computer, and as long as you run it a user context where you have user rights on the server, you will get the amount of free space in GB.

You can also use this a basis for a script listing out renaming space on several computers or several disks on a single server, but that might be the subject for another article!

Working around number ranges limited to 32 bit integers

I head a real brain teaser when working a script earlier today. Basically I was modifying a script which lists out unused phone numbers in a range. It turns out German phone numbers (and any number greater than¬†2147483647) are incompatible with number ranges. Here’s why, and how to solve it.

When using number ranges, you are limited to signed 32 bit integers (-2147483646 to +2147483647). This is rarely an issue, but when working with untypical numbers, like unformatted phone numbers or the byte value of very large files, it can pose a problem.

This is an example of a range that will work:

This however, won’t work:

The high number is above the max value for a signed 32 bit integer.

Solving required a bit of a hack. To work around this limitation, use a While loop to create an array with the number series you’d like to feed into to your variable. Below is an example:

$Counter is initially set to be the starting number of your range. It will be the control parameter that the While loop uses to check if it’s done working.
$NumberStart is the starting (low) number in you range.
$NumberEnd is the end (high) number in you range
$Array is your range (or it’s equivalent). It gets fed each value from $NumberStart to $NumberEnd. This array will hold 64 bit integers.


Using Powershell to identify Child Processes

I got a challenge yesterday on how to identify child processes, and googling it led to the conclusion that this is either not a common issue or no one knows the answer.

My issue was this: if a child process locks up or needs to be killed, how do we identify the process using powershell. The thing about finding child processes is that you need to know who their parents are. In fact, you need to use the parent process id (PID) to identify the child.

How does it work?

First choose how to identify the parent process in Powershell. You can usually use ProcessName, Description, Path etc. Then you need to find any process which has the ParentProcessId of it’s parent (obviously)!

Here’s my solution:

$procid = (Get-WmiObject win32_process | where {$_.ProcessName -eq ‘Powershell.exe’} | select processid)
Get-WmiObject win32_process | where {$_.ParentProcessId -eq $id}

This only identifies the child process, what you’d like to do with it afterwards is your call. Nonetheless, this could help you automate some of the tasks you’ve been using Process Explorer to solve until now.

Copy group memberships for AD users

This techtip handles how to copy group memberships from a single user to one or more users in your organization. In order to accomplish this we use only the Active Directory module included in RSAT or available with Windows Server 2012.

In order to copy the group membership to several users, simply add a foreach-loop in the script.

In the last example, the variable $users would typically be populated using the the cmdlet Get-ADUser to pull from AD based on specific criteria, like OU etc.

This script only adds groups, it does not replace existing group memberships.