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.

 

Solving ReFS integrity bit issues when working with Hyper-V

I was working with Hyper-V and I got the following error:

Export failed for virtual machine ‘webtest01’ (16CED29C-F6B2-4D86-91D1-DDBD635D24C2) with error ‘The requested operation could not be completed due to a virtual disk system limitation. On NTFS, virtual hard disk files must be uncompressed and unencrypted. On ReFS, virtual hard disk files must not have the integrity bit set.’ (0xC03A001A).

The volume I’m working on was an ReFS volume on a Storage Pool located on my test environment. I tried troubleshooting this issue, but there is hardly any good help online. Googling the issue gav me a few obscure Technet references and some rather incomplete blog posts, so this article is a concatenation of the information I found.

List out the files in your “Virtual Hard Disks” folder using this command:

Change the integrity bit on files using this command:

Or, use this one-liner:

I hope this helps shed some light on this issue.

Get rich quick, autogenerate lotto numbers

You can retire soon. With my fantastic “Generate lotto numbers automagically” script, you no longer need to let imagination keep you from playing the lottery!

This script will auto generate a number of unique lottery numbers. You can configure the amount of numbers, and the range to generate the numbers from by changing he value of variable in the script.

NB. Should you win big using numbers from script, my bank account is always open for donations 😉

 

 

 

Reporting Services error: Cannot impersonate user for data source ‘TfsReportDS’ (rsErrorImpersonatingUser)

After moving a reporting services database from SQL server 2008r2 to 2012, we had a problem running reports in TFS.

The error was:

An error has occurred during report processing. (rsProcessingAborted)
Cannot impersonate user for data source ‘TfsReportDS’. (rsErrorImpersonatingUser)
Log on failed. Ensure the user name and password are correct. (rsLogonFailed)
For more information about this error navigate to the report server on the local server machine, or enable remote errors

Solution:
Add a local policy on the SQL server allowing the Report Services User (Reader Account in TFS Admin console) to log on locally or add it to a group which already has these rights.

log on locally

 

 

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.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Test lab

I’m currently working on moving my Hyper-V VMs to an iSCSI target in order to better be able to test Hyper-V Live Migration in 2012. Tehre are alot of new features facilitating added functionality in SMB 3.0. In order to do that I of course have to move roughly 270GB of data from my local storage to the iSCSI target, and I just wanted to upload this screenshot from my test lab showing the more than decent speed the transfer is progressing with.

The transfer speed is stable between 90-110MB per second and utilizing in excess of 700Mbps of bandwidth. My NAS is a Thecus 4200pro with 4 disks in a RAID5 configuration.

Techtip: Reassociating an orphaned user after Database move

Every database on a Microsoft SQL Server instance maintains its own Access Control List (ACL) with a list of the users which have rights on the database, and what rights they have. This ACL however, doesn’t contains only contain user names, but also the Security Identifier (SID) of the user. This means that when moving a database, you can’t simply create a user on the instance you’re moving the database to and expect it to have the same rights. It won’t, because the SID of that user will be different, even if the user name is the same.

Microsoft has hedged against this, and allows you to update the ACL on the database by associating the user name in the ACL with the SID of the user with the same name on the instance to which the database has been moved. By doing this, you don’t have to manually delete the user permissions from the database security tab and set them up again. You can simply run a stored procedure.

To check whether or not there are any orphaned users in your database ACL, run this command on the database:

This will list any orphaned users with rights on the database.

To reassociate the users with a valid SID and keep it’s ACL entries on the database, run the following query:

After running the last command, your user rights will be correct for that user. You can test by running the first command again. No entry for that user should show up.

NB. The square brackets <> can be removed.

Installing Active Directory Domain Services using Powershell

This is going to be a short post, simply because it turned out to be ridiculously easy!

Traditionally when installing Active Directory Domain Services (ADDS) you’ve had to use DCPromo to initiate the install. Through this gui-based installation you could configure the name of your domain and your forest, your domain and forest level and whether or not to install a DNS-server along with your Domain Controller (DC).

You can easily do this using Powershell and it requires on two simple one line commands.

First you need to install the ADDS role on your server. Run this command:

Second, configure your ADDS role and decide whether or not to install DNS:

Voila, you’ve just installed a new domain called LAB in a new forest.