• Powershell and writing files (how fast can you write to a file? )

    Hi there,

    I’ve been working for some time on a tool similar to PAL from mike lagase in order to automate the analysis of loadgen runs.

    While doing this I had to write large files with PowerShell and was not impressed with the result.

    I thought I’d share the problem and solution with the community.

    There are at least four ways of writing files with PSH:

    ·         use the ‘>>’' alias that calls into the out-file cmd-let

    ·         use export-csv

    ·         use .Net

    So let’s say I want to write 10000 lines into a file.

    Method 1: use the >> operator

    $s=”some text”

    1..10000 | % {
         $s >> ".\test.txt"
    }

     

    This way you actually open and close the file 1000 times.

    Method 2: use out-file to write an Array

    1..10000 | % {
        $a+=[Array] $s
    }

    $a | out-file ".\test.txt"

    This way actually writes once, using powershell.

    Method 3: use export-csv to write an Array

    $a=@()

    1..10000 | % {

          $a+=[Array] $s

    }

    $a | export-csv "t.txt"

    Export-csv can also write the array for you

     

    Method 4: use .Net StreamWriter to write the file

     

    $stream = [System.IO.StreamWriter] "t.txt"

    1..10000 | % {

          $stream.WriteLine($s)

    }

    $stream.close()

    The StreamWriter object from .Net also does the work nicely.

    Conclusion: how fast ?

    I tried all methods on my laptop, and here is what I got:

     Method

    Time to completion

    ‘>>’

    29 s

    Outfile and [Array]

    27 s

    export-csv

    22 s

    StreamWriter

    1.5 s

     

    Well results speak by themselves, if you are in a hurry, use .Net StreamWriter !

    Cheers,

    Next, check that with Powerhell v2!

    Guillaume

     

  • Create a CAB file for Windows Mobile from a certificate chain with Powershell

    At his blog Scott Yost treated us with a very cool powershell script to create xml files reqiured to build a .cab and install root certificates on windows mobile.

    Since a customer just asked me for it, I thought I'll finish his script to allow several certificates to be inserted and actually create the .cab file (if you have cabarc.exe somewhere on your path)

    use :
    makecab.ps1 <file>
    or
    makecab.ps1 <folder>

    To create a cab file with the same name.

    Enjoy

    Guillaume

  • Exchange Server 2007 rollups nightmares - automate the .config file modification

    Most of you Microsoft Exchange Server 2007 admins or Loadgen users as have been confronted with the problem described here : (Exchange 2007 managed code services do not start after you install an update rollup for Exchange 2007).

    Those who read the above technote probably felt as I did that every solution given where stupid. You know what? They are ! Who wants to configure Internet access on an Exchange server?  

    A very good description and the 'right" solution is given by Nino and the team on the EHLO blog : http://msexchangeteam.com/archive/2008/07/08/449159.aspx

    I've taken the liberty to automate the .config file modification for you, just run the script on your server and this will do the job:  

    ChangeExchangeConfigFile.PS1 will create, modify any .config file for :

    • Exchange services only (checking them against the service database)
    • Any exchange .exe file in the Exchange installation folder
    • or any path you will want to give

    Click here for the script itself.

    Edit : small bug correction on the script.
    Edit2: (version 1.2) let's home is is the last bug !

     

     

  • Where did my disk space go ? [very long directory names, system directories, symbolink and other hard links ...], meet xdir.exe

      [EDIT : posted a statically linked version to remove dependency over VCRT 9.0]

      Hi geeks,

    The first thing I do when I receive a new Microsoft-issued laptop is : buy the largest hard disk I can find to replace the one that shipped with it.

    But time goes ... and soon I wonder where all that disk space went.

    The other day one of my customers called me with that stance : "Guillaume, I've got a LUN on my SAN with 350GB I cannot account for".

    Where can there be content that is hidden from the system administrator himself ?

    1. System Volume Information is a folder with SYSTEM only  permissions (so administrators do not have access) that can contains many things from Single Instance storage data, recovery points, snapshots ...
    2. very long (> 256 chars) directories are not displayed by either explorer  or the command line because one needs to use the Unicode syntax when accessing those and for some reason our developpers did not.

    To make the story short my customer had VSS shadow storage defined on that LUN for snapshot of a different LUN, so using vssadmin delete shadows or vssadmin delete shadowstorage got his content back.

    But this incident convinced me to launch my could old C++ compiler to build a tool that deals with those kind of nightmares: meet XDIR.EXE

    XDIR is a combination of DIRUSE.EXE (the close parent of DU from Windows 2000 resource kit), RD (remove directory from the nt shell), and dir.

    BUT XDIR.EXE handles directories of any length, or permissions , it does try to follow symbolink links and you can use it to:

    • reclaim disk space : calculate the size of that particular directory and any subdirectory :
      try XDIR c:\
    • read a very long (or any) directory :
      try XDIR <your long directory> /DIR
    • delete a very long directory :
      try XDIR <your long directory> /RMDIR

    Options are :

    /MIN:xxx[GB|MB|KB|B] Minimum size to report (default is 500 MB)
    /LEV:nn Number of directory levels to display, default is 2, 0 will display all directories
    /FORCE known to the user of FILEACL, this will let you use your  SeBackupPrivilege and  SeRestorePrivilege to bypass the NTDS permissions (you need to be backup operator obviously)

    There are also a few other options that let you create an ANSI or UNICODE output file that came directly from FILEACL.

    Let’s try this: where is there content that weight more than 500MB on my C drive ?

    image

    What are those 2 GB in my System Volume Information ?

    image

    Well, that seems like a bunch of restore points to me. Wait, I’m running Windows Server 2008 and restore points are not a server feature ! Where are those kids from ? ohh, maybe from my old Windows XP dual boot !

    Let’s see

    image

    Files from last november? sounds like it’s the last time I booted my XP boot to flash my Windows Mobile, ok, let’s get rid of those (I’ll use the supported method of rebooting to XP and ask nicely that it delete those useless recovery points)

    let’s use another example:

    See that dir ?

    imageimage 

    Empty folder, big deal ?

    image

     

     

    why the heck, does xdir.exe think there is 1.167 GB in that dir ?

    image

    Well somebody twisted (me) has put 1 Gig into this stupidly long directory.

    Side Note : it is just impossible to go there with explorer so what can I do ? :

    • Create a JUNCTION point
      mklink  /J e:\temp\mount "e:\temp\01234567890123456789012345678901234567890123456789\01234567890123456789012345678901234567890123456789\01234567890123456789……
    • Create a DIRECTORY SYMBOLIC LINK
      mklink  /D e:\temp\mount "e:\temp\01234567890123456789012345678901234567890123456789\01234567890123456789012345678901234567890123456789\01234567890123456789……
    • use the good old SUBST from DOS :)

    if you just want to get rid of it use :

    XDIR <your long directory> /RMDIR

    like this:

    image

    add the /FORCE option if you do not have the rights on all folders (like with System Volume Information)

    Guillaume

  • PublisherEvidence strikes again : create .config files everywhere !

    My previous post about Exchange 2007 SP1 RU5 at () explained why Exchange Services where so slow to start after RU5 was applied : .Net code checks for the Microsoft Certificate Revocation List at each startup.

    As explained in http://support.microsoft.com/?kbid=944752, a .config file with

    <configuration>
     <runtime>
               <generatePublisherEvidence enabled="false" />
     </runtime>
    </configuration>

    saves the day.

    Now you can also need this magically .config file for virtually any .NET based code that run in a non-internet connected environment,

    • My powershell startup, create a powershell.exe.config file
    • Exchange System Manager takes 45 seconds to start ?, create an mmc.exe.config file !
    • Outlook is slowww since we installed that latest plugin ? create an outlook.exe.config file !

    Those files have to be created in the same directory the .exe is and that's it.

    So you get the picture, create .config files when .NET application OR good old native win32 app that call into .Net dlls are slow to start.

    Guillaume