September 2008 - Posts

TechEd ATE

No this isn't a TechEd ate my hamster post!!!

I will be on the Ask the Experts (ATE) stand at IT Pro TechEd EMEA in Barcelona.

If you have PowerShell questions or even if you just want to say hi and have a chat - stop by

 

Share this post :

 

Technorati Tags: ,,

Posted by Richard Siddaway's Blog
Filed under:

PowerShell Accelerators

PowerShell has a number of well known accelerators such as [adsi] and [wmi] that are in effect short cuts to creating .NET objects.  One problem with working with .NET in PowerShell is that the class names can get quite long eg System.DirectoryServices.ActiveDirectory.Domain.  This can get to be painful if you type it a lot.!!

MoW has come up with a way to create your own accelerators - http://thepowershellguy.com/blogs/posh/archive/2008/09/12/powershell-v2-add-acceleratortype-function.aspx#comments

I need to have a play around with this to see if it is as useful as it looks.  The alternative is putting some or all of the class name in a string.

I think I'll have to compare the ease of use of both methods.

 

Share this post :

 

Technorati Tags: ,

Posted by Richard Siddaway's Blog
Filed under:

Sweden II

The sessions this morning seemed to go really well - got some good feed back and good questions.  Nearly every one who registered turned up which is really excellent.  100+ people attending the event seemed pretty good to me.

The Scandinavian User group launched with its first meeting this afternoon.  From the discussions I heard there seems to be a lot of enthusiasm around for it.  If you are in Scandinavia and interested in PowerShell then join the group -  http://powershellug.com/

Next week is the UK PowerShell UG meeting.  Stockholm one week & Reading the next hmmm....

 

Share this post :

 

 

IIS PowerShell examples

I havre mentioned the PowerShell provider for IIS 7 several times over the alst few months.  One thing I had missed and should have picked up earlier is a post  http://blogs.iis.net/jeonghwan/archive/2008/07/30/iis-powershell-user-guide-comparing-representative-iis-ui-tasks.aspx showing many examples of using the PowerShell provider and the cmdlets.
 
If you are using PowerShell and IIS you need to look at this
 
 
Posted by Richard Siddaway's Blog
Filed under:

Sweden

Arrived in Sweden - flight wasn't too bad which is saying a lot for me - I do not like flying!!!
 
Looking forward to the first meeting of  Scandanavian PowerShell Group.   This is third group I've been at the first meeting and the 4th group I've seen start.  This is getting to be a tradition..
 
Please can someone start a PowerShell UG in the Carribean   :-)

The idiots are back

I tried re-enabling comments but the idiots returned and plastered yet more comments. Comments are permanently blocked.  It you wish to comment please send me a message or email

 

Share this post :

 

Technorati Tags:

Posted by Richard Siddaway's Blog
Filed under:

W2KSG: WMI Logical Disks

Listing 6.5 in the Scripting Guide returns to the Win32_Logical Disk class.

Get-WmiObject -Class Win32_LogicalDisk | Select DeviceID

All we need to do to display the drive letters is select the deviceid.  if you want to see the full information leave off the select.

 

Share this post :

 

Technorati Tags: ,

W2KSG: WMI Services

WMI gives us insight into most aspects of our system's operation.  Windows machines have lots of services installed. We can see the services very simply

Listing 6.3

Get-WmiObject -Class Win32_Service | Select DisplayName, State, StartMode

The results will be returned in a nicely formatted table - one of the great strengths of PowerShell is that displaying data is automatic and easy.  Even if you want to go beyond the defaults Format-List and Format-Table remove the major headaches of presenting data.

Note that we could use Get-Service but it doesn't return the Starting Mode.

 

Share this post :

 

Technorati Tags: ,,

ADSI - Put and attributes

A question came up about working with Active Directory objects regarding the difference between

$object.attribute = "value"

and

$object.Put("attribute", "value")

They both perform the same function in that the attribute is assigned a value. The question was whats the difference and which should you use. The question also noted that when creating objects they behave differently.

PS> $ou = [ADSI]"LDAP://ou=test,dc=Manticore,dc=org"

PS> $user1 = $ou.create("user","cn=test39")
PS> $user1.description = "test 39"
PS> $user1.setinfo()
Exception calling "setinfo" with "0" argument(s): "A constraint violation occurred.
(Exception from HRESULT: 0x8007202F)"
At line:1 char:15
+ $user1.setinfo <<<< ()

PS> $user2 = $ou.create("user","cn=test49")
PS> $user2.Put("description", "test 49")
PS> $user2.setinfo()
PS>

PS> $user1 = $ou.create("user","cn=test59")
PS> $user1.setinfo()
PS> $user1.description = "test 59"
PS> $user1.setinfo()

PS> $user1 = $ou.create("user","cn=test59")
PS> $user1.setinfo()
PS> $user1.description = "test 59"
PS> $user1.setinfo()

We start as normal by creating an object for the OU to contain the user. We then use the create method to create the user.  If we try to set the description (or any other attribute) and then call setinfo() we get the error shown.  However, if we use Put() to set the attribute immediately after creation and then call setinfo() - it works!!

If we want to set the attribute directly we have to call setinfo() before we do it - and then again afterwards.

If we want an example modifying an attribute

PS> $user1 = [ADSI]"LDAP://cn=test59,ou=test,dc=manticore,dc=org"
PS> $user2 = [ADSI]"LDAP://cn=test49,ou=test,dc=manticore,dc=org"

PS> $user1.description = "new description 59"
PS> $user1.setinfo()

PS> $user2.Put("description", "new description 49")
PS> $user2.setinfo()

So why the difference?

I think that this is all to do with the way the object is accessed.  Remember that [ADSI] is an accelerator for the .NET class System.DirectoryServices.DirectoryEntry (perform a get-member on an object).  System.DirectoryServices.DirectoryEntry is a wrapper for the ADSI COM based interface.

The situation is further complicated in that the PowerShell object is not a "pure" .NET System.DirectoryServices.DirectoryEntry  object.  Try a get-member on it you won't see any of the methods just the AD attributes of the object.  Dropping in to the base object using $user1.psbase | get-member will display the .NET methods. What you will never see is the ADSI methods such as Put and SetInfo - they are implemented but invisible (this caused a lot of discussion when it was introduced in the beta versions)

If you look at VBScript books such as the Windows 2000 Scripting Guide you will see them using Put between the create and the setinfo().

So whats happening?  I think its the .NET class that objects to modifying the object before it is written to AD.  The Put "bypasses" the .NET in that it seems to implement the ADSI methods in a more direct manner.

Which one should you use - personal preference but I would use the $object.attribute = "value" version because it is simpler to understand, uses the same approach as the rest of PowerShell and is a bit less typing.

Hope this helps.

 

Share this post :

 

Technorati Tags: ,

W2KSG: WMI TotalVirtualMemory size

Following on from the last post - the other thing we may be interested in is virtual memory.  Listing 6.2 gives us this

Get-WmiObject -Class Win32_OperatingSystem | Select TotalVirtualMemorySize

 

Share this post :

 

Technorati Tags: ,,

W2KSG: WMI Total Physical Memory

I have decided to jump to the WMI section.  The VBScript dictionary object is equivalent (ish) to the PowerShell hash table which has been covered in many places.  Chapter 5 is on ADSI and having just finished writing a big chapter on that for my book I wanted to look at something else.

Chapter 6 in the scripting guide covers WMI.  PowerShell has great WMI support so if we look at

Script Center Home > Microsoft Windows 2000 Scripting Guide > Scripting Concepts and Technologies for System Administration > WMI Scripting Primer  WMI Overview

Listing 6.1 shows us how to get the Total Physical Memory on a machine (10 lines of VBScript)

The listing uses the Win32_LogicalMemoryConfiguration class so we turn the script into

Get-WmiObject -Class Win32_LogicalMemoryConfiguration | Select TotalPhysicalMemory

However, XP and later don't support this so we use

Get-WmiObject -Class Win32_OperatingSystem | Select TotalVisibleMemorySize

This is one area where research pays off as WMI is evolving and changing with each version of Windows

 

Share this post :

 

Technorati Tags: ,,

Writing to text files

We have considered reading from text files in some detail. Now lets look at writing to text files.

Script Center Home > Microsoft Windows 2000 Scripting Guide > Scripting Concepts and Technologies for System Administration > Script Runtime Primer > FileSystemObject > Reading and Writing Text Files Writing to Text Files

Listing 4.42 overwrites data in a file

Set-Content -Path "c:\test\time.txt" -Value $(Get-Date)

In this case we are writing the current date and time to the file.  If the file doesn't exist it will be created. The file's contents can be checked with

Get-Content -Path "c:\test\time.txt"

To append data to an existing file we use Add-Content

Listing 4.43

Add-Content -Path "c:\test\time.txt" -Value $(Get-Date)

We can also work with CSV files but that is a story for another night.

 

Share this post :

 

Technorati Tags: ,

Posted by Richard Siddaway's Blog
Filed under:

IE 8 Beta

I've been using the IE8 beta for a week or so now and it works.  I've not had any problems with it and the new stuff works as advertised.

Its solid.  Not much of a post but when everything works there's not much else to say :-)

Download IE 8 from http://www.microsoft.com/windows/internet-explorer/beta/default.aspx

 

Share this post :

 

Technorati Tags:

Posted by Richard Siddaway's Blog
Filed under:

PowerShell scripts for SQL Server

I recently mentioned the PowerShell scripts for SQL Server on codeplex - http://richardsiddaway.spaces.live.com/blog/cns!43CFA46A74CF3E96!1602.entry

There has been a new release so now you can work with Tables, Stored Procedures, Views, User Defined Data Types,  User Defined Functions, Synonyms,Triggers, Statistics, Indexes, version and port information, various INFORMATION_SCHEMA view queries

The scripts can be found at  http://www.codeplex.com/SQLPSX

 

Share this post :

 

Technorati Tags: ,

Scandinavian PowerShell User Group

The Scandinavian PowerShell user Group web site has gone live at http://powershellug.com/default.aspx.  Please support them if you live in the area.

Welcome to the PowerShell community guys.

Their first meeting is Tuesday 16th September after the PowerShell event - http://www.microsoft.com/sverige/technet/events/powershell/.  Looking forward to meeting you all.

With groups in UK, Germany, Italy and now Scandinavia we are beginning to cover Europe quite nicely.  Lets see if we can fill some of the gaps in the next year.

 

Share this post :

 

Technorati Tags: ,

Reading Text Files III

Following on from - http://richardsiddaway.spaces.live.com/blog/cns!43CFA46A74CF3E96!1681.entry - the next scenario we might want to consider is only reading part of a line.  For example in a file like this

XXXXX1XXXXXXXXXXXXXX
XXXXX2XXXXXXXXXXXXXXXXXXX
XXXXX3XXXXXXXXXXXX
XXXXX4XXXXXXXXXXXXXXXXXXXXXXXXX

we might only want to read the numeric values

Listing 4.41

Get-Content "C:\Scripts\W2KSG\scriptlog.txt" | Foreach {$_.SubString(5,1)}

Use Get-Content to read the file. Remembering that it is read as collection of strings we can use foreach to iterate through the contents and use the substring method to pick out the 6th character of each line which is the numeric character.  Remember that we start counting at zero!

 

Share this post :

 

Technorati Tags: ,

Posted by Richard Siddaway's Blog
Filed under:

Exchange 2007 SP1

Couple of interesting points from installing an Exchange 2007 SP1 test machine yesterday.

1) You can't do the schema upgrade if the install routine can't identify the schema master FSMO holder. If replication is not up to date on the machine holding the schema master FSMO then the schema update will fail.

2) Setup requires PowerShell v1 even if you are only running the AD upgrade.  This one is unnecessary.  Looks like the check is just a blanket check rather than a specific check against what is happening

3) .NET 3.5 and PowerShell on Windows 2008 is quite acceptable to Exchange

 

Share this post :

 

Technorati Tags: ,,

Reading Text Files II

So far we have read a file from top to bottom.  Get-Content automatically does what we need to explicitly code in VBScript.

Script Center Home > Microsoft Windows 2000 Scripting Guide > Scripting Concepts and Technologies for System Administration > Script Runtime Primer > FileSystemObject > Reading and Writing Text Files  Reading Text Files

Lets have a look at a few special circumstances - for instance if you want to read the file from bottom to top  - Listing 4.40

$files = Get-Content "c:\test\file1.txt"
$j = $files.length-1
while ($j -ge 0){
$files[$j]
$j--
}

Read the file into an array and then work backwards through the array

 

Share this post :

 

Technorati Tags: ,

Posted by Richard Siddaway's Blog
Filed under:

Reading Text Files

A lot if admin scripting involves reading and writing text files.

Script Center Home > Microsoft Windows 2000 Scripting Guide > Scripting Concepts and Technologies for System Administration > Script Runtime Primer > FileSystemObject Reading and Writing Text Files

Listing 4.35-36

In VBScript we had to open a file specifically for reading (or writing) and then read it line by line. In PowerShell we just use Get-Content

Get-Content 2oudrmhe.txt

We can then pipe it into what commands we need to complete the processing - often a foreach

Listing 4.37

We can test is a file is empty before reading as follows

$file = "C:\test\file1.txt"
if ((Get-Item $file).Length -eq 0) {Write-Host "File is empty"}
else {Get-Content $file}

 

Share this post :

 

Technorati Tags: ,

Posted by Richard Siddaway's Blog
Filed under:

Copy-Item Update

In this post - http://richardsiddaway.spaces.live.com/blog/cns!43CFA46A74CF3E96!1666.entry - I was talking about the various actions you could perform on a folder. Shay pointed out that the example I gave of copying a folder does not copy the folder's contents.  If you want to copy the contents add the -recurse parameter.

Thanks Shay

 

Share this post :

 

Technorati Tags: ,

Posted by Richard Siddaway's Blog
Filed under:
More Posts « Previous page - Next page »