Category Archives: Ramblings

C# Folder Size, Or Maybe I Am Ignorant

I apologize for the lack of posting recently – since it is Fall registration we are in a freeze which means no changes to anything until it’s over.  Always a good time to research something that I might need later.  I’ve been paging through Advanced Windows Debugging but I’m not enough of a programmer to understand everything.

Additionally, my site was under some sort of attack which ended up with my host disabling the log-in page for WordPress.  Good for them, I was glad they did.  Not sure what caused that, but you can be assured none of your personal information was stolen, since I don’t keep any, and the system wasn’t compromised anyway.

In any case, I was looking at a question on Reddit concerning PowerShell and getting folder size, create, and modify times from a script.  Problem being, the pathing could be more than 260 characters.  If you’ve never run in to the problem, here is the background.  While the poster did finally manage to get the file size using robocopy, I suggested he use .Net to get creation and modified dates.  This is fairly straightforward:

$CreateTime = [System.IO.File]::GetCreationTime($i)
$LastModified = [System.IO.File]::GetLastWriteTime($i)

where $i is the file or folder name.

But then I got to thinking – surely there must be a better way than robocopy to get file sizes for long paths?  Let’s take a look at .Net for this too…

Since GetCreationTime and GetLastWriteTime seem to work on folders as well despite the library being named “File”, I thought the following code would work too:

$files = [System.IO.Directory]::GetFiles($i)
foreach ($file in $files)
$size += $file.Length;

But, for some reason, this will only get the size of the files inside the directory, which doesn’t include any folders that might be under it.  You actually need to iterate through the whole source directory to get everything that might be there in order to get the total size.  It appears that this is a limitation of the Windows API.

Now, that being said, I am not a C# developer by any means, so there might be an easy way to do this that I don’t know.  It’s just that it seems inconsistent to me given the naming conventions between the two libraries.  I can get folder modified times and creation times using a File call, but not the sizes of a root folder.  Why?

If Get-ChildItem could lose its 260 character limitation that would probably help out a lot.  I suppose you could use PSDrives to shrink the path, but sometimes that wouldn’t be an option given the structure of a file system.

System Center Orchestrator And VM Creation Rant

I’ve recently got the chance to begin working with System Center Orchestrator 2012 R2, which is looking like it is going to be awesome for really getting our environment to the automated level.  That is, if I ever figure out how to use the thing.

I have been looking through the book System Center 2012 Orchestrator Unleashed on Safari Books which has given me a decent basic introduction to functionality and features, but there’s a lot that can be done with this product, especially when you start adding Integration Packs to interface with other System Center products and anything else in your environment with a provided IP.

Right now, I am trying to automate the VM creation process through Orchestrator.  The goal is to make the creation of one or many VMs a single-click operation from a webpage.  This has been much more of a challenge than I anticipated.

When you aren’t using PowerShell or a scripting editor such as PowerCLI, it is hard to keep track of all the operations that need to happen before the next step can take place.  For example, the next step in your procedure might require a VM ID instead of a VM Name, which might not be published data from the activity that just finished.  So you get lost trying to figure out why the operation just failed, when you are passing information the next step can’t use.

I’ve taken to saving the VM specifications in a database and using Orchestrator to query the information.  Sadly, Orchestrator cannot return information from a database query except as a semicolon-delimited string, which means you need a script to parse that output and then build arrays for your activities.

Looping is another complaint I have.  It doesn’t seem as flexible as what is available in PowerShell.  I was attempting to allow a runbook to create multiple hard disks, but it took a long time for me to figure out how to get the loop to pick out a different bus for each disk.  Actually, that one was kind of embarrassing because you can count the number of times a loop has executed, but it’s not obvious.

Maybe I should just be chalking all this up to the learning phase, but it seems like there is a real lack of documentation for Orchestrator itself.  Don’t get me wrong though, I think it is going to be a great thing once we get some runbooks built and working the way we want.

I hope to have some runbooks that I’ll be able to post to GitHub once I get something of use done, so maybe someone can learn from my mistakes.