November 2008 - Posts

Open a CSV

PowerShell has great functionality for working with csv files.  They make an excellent way of saving information generated by PowerShell - for instance

Get-Process | Export-Csv data.csv -NoTypeInformation

Having created a csv file how can we read it?  It is possible to use Notepad but then the columns are not padded to the same width so the data can appear jumbled.  Ideally we would want to use an application that generated a grid view - like excel.

Excel is very easy to use for this

$xl = New-Object -comobject "excel.application"
$xl.WorkBooks.Open("C:\Scripts\Office\data.csv")
$xl.visible = $true

Create an object for the Excel application.  Open the csv file in a workbook and make it visible.

 

Share this post :

 

Technorati Tags: ,,

Posted by Richard Siddaway's Blog
Filed under:

Scripting and automation

Is there a difference between scripting and automation.  According to Dan http://blogs.msdn.com/dtjones/archive/2008/11/23/scripting-dba-actions.aspx  the answer is yes.   Scripting means you perform a task programmatically using a scripting language (PowerShell I hope).  Automation means that the script is automatically initiated as a scheduled task or a SQL Server job etc.

A quick search shows a number of definitions for automation:

"The replacement of manual operations by computerized methods."

"The automatic operation or control of equipment, a process, or a system."

"a system in which a workplace or process has been converted to one that replaces or minimizes human labor with mechanical or electronic equipment"

I would argue that there is a spectrum of activity ranging over:

  • perform task manually (GUI or command line)
  • script task
  • run script automatically

Creating 7000 users and mailboxes in AD\Exchange is something you would do with a script.  Would you have that script automatically initiated - I don't think so. But I would argue that you are still automating that task.  The machine is doing the work - quicker and potentially (hopefully) more accurately.

My stance is that scripting is automation and whether you run the script manually or automatically in response to a task you are still automating.

There are some useful guidelines on scripting in the post that are applicable to any language.

 

Share this post :

 

Technorati Tags: ,

Posted by Richard Siddaway's Blog
Filed under:

get-scripting podcast 5

Want to learn more about how PowerGUI came about and where its going - then listen to the latest get-scripting podcast at http://get-scripting.blogspot.com/2008/11/get-scripting-podcast-episode-5-dmitry.html

 

 

Share this post :

 

Technorati Tags: ,
Posted by Richard Siddaway's Blog
Filed under:

Hexadecimal

I was working with an Exchange 2007 system today - using Get-StorageGroupCopyStatus.  One of the things I was interested in was the logs that had been generated, replicated and replayed.  Exchange 2007 uses 1 MB log files and the file names are based on incrementing numbers - hexadecimal numbers (base 16). Unfortunately Get-StorageCopyStatus reports the log numbers in decimal (base 10).

I can't convert numbers in the thousands, or greater, easily between decimal and hexadecimal. So I use a conversion routine that is based on this:

PS> $x=6904
PS> [convert]::ToString($x,16).ToUpper()
1AF8

Convert the integer to a string and at the same time convert to base 16 (hexadecimal).  At the same time convert to upper case to make it match the log numbers.

This can be used as the basis of the expression in a calculated field used in format-table or a select statement.  That bit I will leave as an exercise for the interested student of PowerShell   :-)

 

Share this post :

 

Technorati Tags: ,,

Posted by Richard Siddaway's Blog
Filed under:

Testing AD existence

Another question from the webcast concerned testing for the existence of an OU if you know its distinguished name.  It turns out that the System.DirectoryServices.DirectoryEntry class has an Exits method that can be used for testing in this way.

One thing to be careful of is that it is a static method so you don't actually create an instance of the object.  You can use it as shown below.

PS> [System.DirectoryServices.DirectoryEntry]::Exists("LDAP://ou=thunderbirds,dc=manticore,dc=org")
True
PS> [System.DirectoryServices.DirectoryEntry]::Exists("LDAP://ou=thunderbirds2,dc=manticore,dc=org")
False
PS> [adsi]::Exists("LDAP://ou=thunderbirds,dc=manticore,dc=org")
True
PS> [adsi]::Exists("LDAP://ou=thunderbirds2,dc=manticore,dc=org")
False

It even works with the [adsi] shortcut.

Another useful piece of AD scripting functionality.  Makes testing for the existence of objects much easier when creating users etc.

 

Share this post :

 

Technorati Tags: ,

PowerShell and DHCP

One of the questions on the AD webcast I've just finished was about administering DHCP with PowerShell.  As far as I am aware there isn't an interface available through .NET, WMI or COM that enables us to administer DHCP servers through PowerShell.

If you know differently I would be grateful for some examples and pointers to the documentation.

 

Share this post :

 

Technorati Tags: ,

Posted by Richard Siddaway's Blog
Filed under:

Tee-Object

In my post on PowerShell Objects -  http://richardsiddaway.spaces.live.com/blog/cns!43CFA46A74CF3E96!1875.entry - I mentioned Tee-Object and gave an example.  Mats left me a message asking why Tee and asking did it mean temporary.

I had always looked at it as a branching - much like a T join in pipes. The help file says "The Tee-Object cmdlet send the output of a command in two directions (like the letter T). "

Think of it as a dead end branch off the pipeline.

 

Share this post :

 

Technorati Tags:

Posted by Richard Siddaway's Blog
Filed under:

Correction

The regular expressions in my previous post should have been

PS> "Richard" -match "R.*
True

PS> "Richard" -match "[A-Z].*"
True

PS> "Richard" -match "[R][ic]+.*"
True

The * of a wildcard has to be replaced by .* which means any character except a new line any number of times including zero.

Thanks to Jaykul for pointing out the error

 

Share this post :

 

Posted by Richard Siddaway's Blog
Filed under:

Regular Expressions

Lets start with confession time. Regular expressions are something I have tended to avoid like the plague.  Why? I suppose its because I've never taken the time to understand them. Having seen some of the powerful things that can be done with them (and the fact that I've promised to talk about them at a UG meeting :-) ) its time to dig into them. They become incredibly useful with select-string and switch statements as we will see later.

From the conversations I've had there are a lot of people who feel the same way about regular expressions. Hopefully we can uncover some of the mystery.

We all seen or used something like this

PS> "Richard" -like "r*"
True

This is using a -like operator on a wildcard match. The PowerShell wildcards are * for zero or more characters, ? for a single character and [] to match a range or specified characters for example:

PS> "Richard" -like "[A-Z]*"
True

PS> "Richard" -like "R[ic][ic]*"
True

While not counted as regular expressions wildcard matches can be thought of as the lead in to regular expressions. The first difference is that we use -match instead of -like.

So to duplicate what we have seen so far we would use

PS> "Richard" -match "R."
True

PS> "Richard" -match "[A-Z]."
True

PS> "Richard" -match "[R][ic]+."
True

In the first example we are just replacing the * of the wildcard with the regular expression . which means any character except a new line. The second example has a match against a letter in the range A-Z and the third is looking for an "R" then either an i or a c twice (the + sign means one or more matches)

We have seen how regular expressions build on wildcard matching.  Next time we'll dive a bit deeper in regular expressions.

 

Share this post :

 

Posted by Richard Siddaway's Blog
Filed under:

Discovering PowerShell

When I'm talking, or writing, about PowerShell I often refer to your four friends when you are first learning PowerShell.  These are:

  • get-help
  • get-member
  • get-command
  • get-psdrive

Between them you can discover a vast amount of information about how PowerShell works.  if you are new to PowerShell I would strongly recommend getting to know these four cmdlets very well. The time will be amply repaid.

There may be a new kid on the block to join these four.  Jeffrey Snover has posted a script on the PowerShell team blog to display the cmdlets in a particular snapin as a chart based on verb and noun.  http://blogs.msdn.com/powershell/archive/2008/11/22/verbs-vs-nouns-by-snapin.aspx

This is very useful.

 

Share this post :

 

Technorati Tags: ,

Posted by Richard Siddaway's Blog
Filed under:

PowerShell User Group

For last nights meeting I was asked if we could cover objects in PowerShell and using Get-Member. This we did and it sparked the usual conversations.

We have decided that a part of each meeting will cover an introductory PowerShell topic as a help to newcomers to PowerShell and a reminder for the more experienced.

In January we will cover error handling and in March we will look at Regular Expressions.

Also coming up in March is a session on using PowerShell with WSUS and a session on using the VMWare PowerShell toolkit.

As for January which is our second anniversary meeting - that hasn't been finalised yet.  Watch this space.

It does seem amazing that its two years since:

  • PowerShell version 1 shipped
  • the UK user group was formed
  • I got involved in all this

Time really does fly when you are enjoying yourself.

If there is a topic you would like to see covered either as a session at a meeting or by a webcast let me know.

 

Share this post :

 

Technorati Tags: ,

Loading SQL Server Provider

Last night at the User Group meeting I showed a script to load the required assemblies and snapins that enabled the SQL Server 2008 PowerShell provider to be accessible through a normal PowerShell session.

In response to requests at the meeting the script can be obtained from http://blogs.msdn.com/mwories/archive/2008/06/14/SQL2008_5F00_Powershell.aspx

Enjoy

 

Share this post :

 

Technorati Tags: ,

Prof PowerShell

Jeffrey Hicks, PowerShell MVP and author, has a column on all things PowerShell related at http://mcpmag.com/columns/columnist.asp?columnistsid=81

There are a good number of PowerShell related articles in the archives for this column.  If you aren't aware of the articles check them out.

You can subscribe to get the columns delivered to your in box as part of mcp mag's newsletter.

Highly recommended.

 

Share this post :

 

Technorati Tags:

Posted by Richard Siddaway's Blog
Filed under:

Group Policy

The news regarding wall to wall PowerShell in Windows 7\Windows 2008 R2 is great news. The fact that PowerShell will be on by default in both of those is even better (except server core where it is an option).

The reach of PowerShell is further extended by its use with Group Policy.  Details are beginning to appear http://blogs.technet.com/grouppolicy/archive/2008/11/19/group-policy-in-windows-7.aspx

This means:

  • PowerShell logon scripts
  • Scripting GPO linkage\creation etc (can do already with SDM cmdlets)
  • Configure GPO registry settings from PowerShell

The more I look at this the more its going to be a 1:36:86 coloured world.

 

Share this post :

 

Posted by Richard Siddaway's Blog
Filed under:

PowerGUI 1.5.3

PowerGUI 1.5.3 is now available for download from www.powergui.org

Full details of the improvements at http://dmitrysotnikov.wordpress.com/2008/11/19/powergui-153-is-out/

 

Share this post :

 

Technorati Tags: ,

Posted by Richard Siddaway's Blog
Filed under:

Powershell and Objects

PowerShell is .NET based.  .NET is object orientated. Therefore PowerShell is object based.  One of the first things we learn about PowerShell is that we are working with .NET objects.  Just what does that mean?

Look at the list of cmdlets specifically designed to work directly with objects:

Compare-Object
ForEach-Object
Group-Object
Measure-Object
New-Object
Select-Object
Sort-Object
Tee-Object
Where-Object

Commonly the aliases foreach, group, select, sort and where are used for their respective cmdlets.  These are the ones that are used all of the time and everyone is happy with.  What about the others? When was the last time you used Tee-Object?

Tee-Object enables you to store output in  a file or variable and display to the console (if tee-object terminates pipeline) or pass along the pipeline

Consider

Get-Process | Tee-Object -FilePath ptest.txt | Select name, handles

Get-Content ptest.txt

Will get the current processes, save the information in a file and then display the name and handles of the processes.  We can then view the contents of the file.

Even more useful is this

Get-Process | Tee-Object -Variable procs | select name, handles
$procs
$procs | select name, handles

This time we put the information into a variable.  Note we don't use a $ on the variable name.

We can then input $procs to the pipeline for further analysis.

Any time you may need to dig further into some data think about using tee-object in your pipeline.  Could save a bit of effort.

 

Share this post :

 

Technorati Tags: ,

Posted by Richard Siddaway's Blog
Filed under:

PowerShell certifications

One of the questions that came up on the PowerShell panel discussion at TechEd was should there be a certification in PowerShell. The general consensus of the panel was that no there shouldn't be a specific PowerShell certification. I agreed with that consensus at the time and still do. The more I have thought about it the more convinced I am that there shouldn't be a general certification in PowerShell.

My reasoning is simple.  PowerShell itself is not important. Bet you didn't expect to see that :-)

PowerShell is a tool for administering Windows based systems. Yes its brilliant. Yes it is the most powerful way to administer your systems both local and remote and Yes it is the future of Windows administration. So why do I say its not important?

PowerShell is a means to an end. The end is administering your systems.  PowerShell can do it faster and easier but it is the systems that are being administered that are important. The systems that support the business applications that bring revenue in your companies. Keeping those systems running is why we are in business as administrators and that is what we should be certified on.

PowerShell is the greatest thing to hit IT since 1s and 0s but I still don't think there should be a separate certification in it.  Look at the programming languages - the certs aren't in the languages they are in building web or windows based applications.

Learn PowerShell - you won't be able to be a top level Windows admin without in a few years time! Use PowerShell to administer your systems. Expect to see PowerShell come up in exam questions but don't shout for a PowerShell cert.

 

Share this post :

 

Technorati Tags: ,

Posted by Richard Siddaway's Blog
Filed under:

PowerShell Live meeting

Tuesday 25 November 2008 -- 7pm-8pm GMT

This Live Meeting will cover:

- How to use PowerShell to work with Active Directory

    - Writing scripts

    - Using the AD cmdlets

- It will mainly cover working with OUs, Users and groups.

- If time permits there will be quick coverage of sites, site-links and subnets and the new features in Windows 2008 AD

Note that audio is only available through your computer speakers\head phones

Richard Siddaway has invited you to attend an online meeting using Live Meeting.
Join the meeting.
Audio Information
Computer Audio
To use computer audio, you need speakers and microphone, or a headset.
First Time Users:
To save time before the meeting,
check your system to make sure it is ready to use Microsoft Office Live Meeting.
Troubleshooting
Unable to join the meeting? Follow these steps:

  1. Copy this address and paste it into your web browser:
    https://www.livemeeting.com/cc/mvp/join
  2. Copy and paste the required information:
    Meeting ID: PMHBJ2
    Entry Code: 5b^W@Mk=z
    Location: https://www.livemeeting.com/cc/mvp

If you still cannot enter the meeting, contact support

Notice
Microsoft Office Live Meeting can be used to record meetings. By participating in this meeting, you agree that your communications may be monitored or recorded at any time during the meeting.

 

Share this post :

 

UG Meeting Last Call

The November meeting of the User Group will be on Thursday 20th November. 

If you haven't already  done so can you please let me know if you will be attending by Tuesday lunch time

Time: 6.30 - 9.30

Location: Memphis room, Building 3, Microsoft campus, Thames Valley Park, Reading

Agenda:

Powershell Introduction: Objects and get-member

PowerGUI SQL Server Reporting Services powerpack

PowerShell in SQL Server 2008

The usual refreshments break will occur

The PowerShell Introduction is a new feature for meetings where we will explain a particular feature of PowerShell that has caused someone a problem or maybe a slightly more difficult concept.

We will also look at particular commands\cmdlets and talk them through.

Do you have a question about a particular area of PowerShell?   Bring it along to the meeting or email me and will include it

 

Share this post :

 

Technorati Tags: ,

PowerShell Discussion video

The video of the PowerShell panel discussion at TechEd EMEA is available from http://technet.microsoft.com/en-us/events/teched/cc561184.aspx
 
Enjoy
Posted by Richard Siddaway's Blog
Filed under:
More Posts Next page »