PowerShell: The Warm Up

Hi Folks,

This article is going to give a brief demonstration on how useful PowerShell can be, not only for administrators but also for developers. One of the really cool things with PowerShell is the ability to call static classes in .NET and also instantiate objects from the command console. Sometime if I need to find something deep in the object model of BizTalk and lately even with SharePoint, PowerShell can then be used to quickly check out the hidden agenda’s!

Recently, I wanted to run a test case, where I needed to migrate documents from MOSS 2003 to MOSS 2007. The problem is that I needed to keep the original file names of the migrated documents and do test runs on my virtual machine for all 80000 documents. I decided the best way forward to generate the test data, was use SQL to get a list of the filenames and then use PowerShell to recreate the files with the same filenames but just have small garbage in it, so that my VM will not run out of space when I do a dry run of the migration tool.

So, to start with, I have a text file with the following in it:

—— MyTextFile.txt ——

FileNamea.txt
FileNameb.txt
FileNamec.txt

—————————–

If you feel like using an old dos command to make a file with text:

image

In the above, you use F6 to save the file, which is the ^Z symbol.

What we are going to do, is use PowerShell to loop through the text file and create 1kb files for me with the same name.

First you need to download PowerShell from here:

http://www.microsoft.com/windowsserver2003/technologies/management/powershell/download.mspx

You can then open the console, by navigating to the shortcut in the start menu.

image

Then you run the following command:

type MyTextFile.txt | foreach($_){New-Item -name $_ -itemType file ; "romiko" > $_ }

Here we use the type command to output the contents of the file to standard output in the console. Then we PIPE the output into a foreach statement, where $_ is the standard Console.ReadLine. We then have an action to create a New Item of type File. Once the file is created we redirect a string called "romiko" into the newly create file > $_. This is similar to using this command dir /w > directorylist.txt

Here is the output

image

Notice it says the file is 0 Length, do not believe it, run another dir command:

image

This is the first of many blogs that I will be writing in regards to PowerShell. When I have a moment I will demonstrate the use of PowerShell for checking things out in the SharePoint and BizTalk object model, until then, enjoy the world of powerful scripting.

Now, who said that Windows Shell was not as Powerful as Unix? Thanks to Joe Capka for introducing me to this wonderful tool! You can read Joe Capka’s blogs here:

http://jcapka.blogspot.com/

Regards

Romiko

Advertisements
Uncategorized

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s