Mar 21 2017

Count Results in PowerShell

Published by under Windows





 

I was astonished to see PowerShell would display nothing when it should count 0 or 1.
 
Here’s a screenshot retrieving the number of AD accounts.
 
Instead use the Measure cmdLet and select the count value:

C:\>(Get-ADUser -filter {SamAccountName -like "dron*"} |
     select SamAccountName | measure).count
1

 

 
Another useful command:
The “tee” command lets you store the result set into a variable and its number of elements into another:
 

C:\>$accountnb = (Get-ADUser -filter {SamAccountName -like "dr*"} |
                 select SamAccountName |
                 tee -Variable accounts | measure).count
C:\>$accountnb
3
C:\>$accounts

SamAccountName                                                                                                     
--------------                                                                                                     
draguene                                                                                                           
drondy                                                                                                             
droze                                                                                                              
 

No responses yet

Mar 09 2017

Move RDS profiles to another volume/drive

Published by under Windows




You’re in charge of a Remote Desktop Services (RDS) server but unfortunately, C: drive starts running out of space, RDS profiles being on that same volume.
Lucky enough, a huge amount of space remains on D: drive, but how shall I migrate?
 
You can run a GPO that creates new profiles in D: like this:
Computer Configuration -> Administrative Templates -> Windows Components -> Remote Desktop Services -> Remote Desktop Session Host -> Profiles -> Set path for Remote Desktop Services Roaming Profiles
It works for new profiles but older ones need to be moved as follow:
– Migrate user folder from C: to the new drive and assign proper rights.
If older profiles connect with a temporary profile, you should also:
– Remove the entry in Control Panel -> User accounts -> Configure user profiles advanced properties.
– Remove the registry entry HKLM\Software\Microsoft\Windows NT\CurrentVerison\ProfileList\S-1-5-21… with a ProfileImagePath key matching the user profile path
Advantage: All new profiles will be in this new location
 
or
 
Edit the path in the above registry key, move the profile folder to the new location and assign user’s rights.
New profiles will still be created on C: but you can migrate some on D: as you wish, and spread data on 2 volumes.
 
The best is clearly to define a GPO right from the start on a dedicated volume other than C:

 

No responses yet

Oct 06 2016

Processing CSV Files with Perl and Bash

Published by under Linux




Olivier, a friend of mine, had to parse a CSV file and took the opportunity to benchmark the performance of 3 programming languages.
 
The file contains server names and disks he needs to add up into a hash table in order to get the total disk space for each server. He assumes on his blog Perl, Python and Golang are much faster than Bash. He is definitely right but, how much faster?
 
The following (slightly modified) Perl script processed 600k lines in less than a second. Not bad knowing Perl is an interpreted language.

#!/usr/bin/perl
my $file = 'sample.csv';
my %data;
open(my $fh, '<', $file) or die "Can't read file '$file' [$!]\n";
while ( my ($server,$value)=split(/,/,<$fh>)) {
    $data{$server} += $value;
}
close ($file);

 
Now, here’s a similar code in bash

#!/bin/bash
file=sample.csv
declare -A data
while read -r line; do
  values=($(echo $line|awk -F, '{print $1" "$2}'))
  (( data[${values[0]}] += ${values[1]} ))
done < "$file"

The file was processed in over 19 minutes, or in other words, around 1200 times slower!
 
Let's see if we can improve the script's performance.
The read command man page states something of interest:
"The characters in IFS are used to split the line into words".
Setting comma as the default separator allows to build the $line variable as an array, saving the hassle of parsing each line and using a temporary variable.

#!/bin/bash
IFS=','
file=sample.csv
declare -A data
while read -a line; do
  (( data[${line[0]}] += ${line[1]} ))
done < "$file"

This new version runs in the smooth time of... 17s! This is 17 times slower than Perl, but 70 times faster than the original version.
 
No doubt Perl and Python are much faster than the shell family languages, but one needs to pay attention to small details when it comes to performance issues.

 

No responses yet

Next »