About Me

My photo
Northglenn, Colorado, United States
I'm primarily a BI Developer on the Microsoft stack. I do sometimes touch upon other Microsoft stacks ( web development, application development, and sql server development).

Monday, January 28, 2008

Some Unix commands for PowerShell

I've been playing with PowerShell a little and decided to make some Unix commands that were not ported over.

 

Command Function
whoami

function whoami
{  [System.Security.Principal.WindowsIdentity]::GetCurrent().Name       
}

~ (works better if it was not an alias)

function ~
{
   $home
}

mkdir

function mkdir([string] $name)
{
   New-Item $name -type directory
}

touch

function touch([string] $path = ".\", [string] $name)
{
   $file = $path + $name
   $result = test-path $file
   if($result -eq $true)
   {
      ##"Changing LastWriteTime"
      gci $file | foreach{$_.lastwritetime = $(get-date)}
   }
   else
   {
      ##"Creating new file"
      new-item $file -type file
   }
}

find (still currently working on this one. I need to implement wild cards)

function find([string] $name = "", [string] $ext = "", [string] $drive = "C:")
{
   $computer = "."
   if($name -ne "")
   {
      $files = get-wmiobject -computer $computer cim_datafile -filter "FileName = '$name'"
   }
   if($ext -ne "")
   {
      $files = get-wmiobject -computer $computer cim_datafile -filter "Extension = '$ext'"
   }

   $files | add-member --type PropertySet "FindInfo" ([string[]]("Name","FileType","Extension","CreationDate")) -force
   $files | select FindInfo | ft -wrap
}

 

I figured re-writing Unix commands might help me learn powershell. I'm going to use http://en.wikibooks.org/wiki/Guide_to_UNIX/Commands as source of basic Unix commands to implement.

Wednesday, January 23, 2008

LINQ's "Outer Variables in a Loop"

Started using LINQPad and going through the "C# 3.0 in a Nutshell" tutorial that came with the program.

Then I ran the following script from "Chapter 8 - LINQ Queries/Deferred Execution/Outer Variables in a Loop" :

IEnumerable<char> query = "Not what you might expect";

 

query = query.Where (c => c != 'a');

query = query.Where (c => c != 'e');

query = query.Where (c => c != 'i');

query = query.Where (c => c != 'o');

query = query.Where (c => c != 'u');

 

new string (query.ToArray()).Dump ("All vowels are stripped, as you'd expect.");

 

query = "Not what you might expect";

 

foreach (char vowel in "aeiou")

{

    query = query.Where (c => c != vowel);

}

 

new string (query.ToArray()).Dump ("Notice that only the 'u' is stripped!");

 

with the following results:

▪ All vowels are stripped, as you'd expect.

Nt wht y mght xpct

▪ Notice that only the 'u' is stripped!

Not what yo might expect

 

 

Very confusing hmmm..., here is the solution (add a temp variable):

foreach (char vowel in "aeiou")

{

   char temp = vowel;

   query = query.Where (c => c != temp);

}

 

Reason:

"The temporary variable in the loop is required to avoid the outer variable trap, where the same variable is captured for each iteration of the foreach loop." -- C# 3.0 IN A NUTSHELL http://www.albahari.com/nutshell/predicatebuilder.html

 

I would like a more detail explanation of this.

I'm guessing that the variable vowel goes through some sort of reset each time the foreach loop is ran, so in the end you will be actually checking

query = query.Where (c => c != 'u');

query = query.Where (c => c != 'u');

query = query.Where (c => c != 'u');

query = query.Where (c => c != 'u');

query = query.Where (c => c != 'u');

 

instead of

query = query.Where (c => c != 'a');

query = query.Where (c => c != 'e');

query = query.Where (c => c != 'i');

query = query.Where (c => c != 'o');

query = query.Where (c => c != 'u');

Wednesday, January 16, 2008

How to make a new command in Windows PowerShell

Lets say you have a idea for a new function that you would like to implement in Window's PowerShell. In this case I made a Unix like whoami command:

function whoami
{
     [System.Security.Principal.WindowsIdentity]::GetCurrent().Name       
}

when in PowerShell it returned for me: mss/wandrus. Simple enough. Now to get the command to be usable every time an new PowerShell is opened.

 

Type within PowerShell:  Get-Variable profile | Format-List

This should give you the information on what file and directory path are needed to place your custom scripts. In this case I was given:

C:\Document and Settings\wandrus\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

 

Which meant that I had to create a new folder (WindowsPowerShell) and a new script file in notepad (Microsoft.PowerShell_profile.ps1) where I've place my function.

Monday, January 14, 2008

Installing SQL Server 2008 - Code name "Katmai"

Well, I finally got around to installing SQL Server 2008. Ran into a little problem when it came to the Server Configuration screen. I got this error

"Invalid or missing username or password. To continue, provide valid credentials for the SQL Server Agent service".

The solution was to use my credentials for the login with username of mss/wandrus and my login password.

 


UPDATED: http://www.sql-articles.com/articles/SQL2008/ins_2008.html
http://www.sql-articles.com/index.php?page=articles/SQL2008/ins_2008.html does a nice job of showing how to step through the installation process.