Monthly Archives: August 2008

W2KSG: Drive is Ready

One last look at drives and then we are on to folders.

Script Center Home > Microsoft Windows 2000 Scripting Guide > Scripting Concepts and Technologies for System Administration > Script Runtime Primer > FileSystemObject > Managing Disk Drives Ensuring That a Drive is Ready

Listing 4.4 uses the Filesystem object to test if drives are ready.  If you examine the script the majority of it, like a lot of VBScripts is taken up with display.  Using PowerShell's ability to substitute variables into a string we can cut that down and make a much simpler script.

#Listing 4.4
$fso = New-Object -ComObject "Scripting.FileSystemObject"
$fso.Drives | Foreach {
    if ($_.IsReady){"DriveLetter $($_.DriveLetter) has $($_.FreeSpace) of freespace" }
     else {"DriveLetter $($_.DriveLetter) is not ready"   }
}

Get the filesystemobject as before. Pipe the drives into a foreach cmdlet and test on the IsReady parameter.  If the drive is ready it displays the freespace otherwise it gives a message that the drive is not ready.  It does pick up USB drives and memory sticks.

 

Share this post :

 

Technorati Tags: ,

W2KSG: Drive Properties

One of the most used objects in VBScript is the FileSystemObject.  While much of its functionality is available through other means in PowerShell as we will see in future posts there is still much we can do with it.

Script Center Home > Microsoft Windows 2000 Scripting Guide > Scripting Concepts and Technologies for System Administration > Script Runtime Primer > FileSystemObject > Managing Disk Drives  Enumerating Disk Drive Properties

FileSystemObject is a COM object so we can access it through New-Object

Listing 4.3

$fso = New-Object -ComObject "Scripting.FileSystemObject"
$fso.Drives | Format-List

Emulates the VBScript.  Only draw back to it is that the RootFolder isn't reported in PowerShell.

A lot of this information is available via WMI e.g.

Get-WmiObject -Class Win32_LogicalDisk
Get-WmiObject -Class Win32_DiskDrive
Get-WmiObject -Class Win32_LogicalDiskRootDirectory

Pipe them into get-member to see the wealth of information available.

 

Share this post :

 

Technorati Tags: ,

 

PowerShell in Practice: Chapters 3&4 in MEAP

Chapters 3 and 4 are now available through the Manning Early Access Program

- http://www.manning.com/siddaway/

Chapter 3 is concerned with using some of the essential technologies with PowerShell - ADSI, .NET, COM and WMI. The new features in PowerShell V2 CTP2 are briefly covered

Chapter 4 covers development for administrators, especially the ad hoc type of development that PowerShell excels at. Some scripting guidelines and best practices together with a look at some of the additions for PowerShell such as PowerGUI, PowerShell+ and the AD cmdlets are included. Script signing rounds out the chapter.

Next up will be working with user accounts locally, in AD and in ADAM

Enjoy

 

Share this post :

 

W2KSG: Enumerate Disk Drives

I'm skipping Chapter 3 of the guide for now as its all about the Windows Script Host.  Jumping to chapter 4 Listing 4.1 enumerates the disk drives

Script Center Home > Microsoft Windows 2000 Scripting Guide > Scripting Concepts and Technologies for System Administration > Script Runtime Primer > FileSystemObject > Managing Disk Drives  Returning a Collection of Disk Drives

Get-PSDrive

is all we need to use in Powershell

 

Share this post :

 

Technorati Tags:

Date Additions

In http://richardsiddaway.spaces.live.com/blog/cns!43CFA46A74CF3E96!1658.entry we saw how to get the difference between two dates. Let's look at working out a day in the future.

Script Center Home > Microsoft Windows 2000 Scripting Guide > Scripting Concepts and Technologies for System Administration > VBScript Primer > VBScript Reference > Working with Dates and Times  Date Arithmetic

Listing 2.17

$now = Get-Date
"60 days from now: $($now.AddDays(60).DateTime)"
"60 days ago:      $($now.AddDays(-60).DateTime)"
 

Set a variable to todays date using get-date.  We could use get-date directly but the times will creep out as we experiment - use a variable for consistency.  We then use AddDays to add the number of days we want to project forward and backwards - notice the use of negative number of days for getting dates in the past!!  The datetime property forces the display in the format I want - see http://richardsiddaway.spaces.live.com/blog/cns!43CFA46A74CF3E96!1657.entry

As well as days there are methods to add other time periods:

Add
AddDays
AddHours
AddMilliseconds
AddMinutes
AddMonths
AddSeconds
AddTicks
AddYears

Add - adds a time span object. A tick is 1/10000th of a second. The rest are self explanatory and used in the same way as AddDays.

 

Share this post :

 

Technorati Tags: ,

Powershell event: Sweden

If you are going to be in Stockholm on 16 September don't forget the PowerShell event and the new PowerShell UG meeting

http://www.microsoft.com/sverige/technet/events/powershell/

 

Share this post :

 

Technorati Tags: ,

W2KSG: Date differences

The number of days between two dates has been covered before so we'll keep this one quick.

Script Center Home > Microsoft Windows 2000 Scripting Guide > Scripting Concepts and Technologies for System Administration > VBScript Primer > VBScript Reference > Working with Dates and Times  Date Arithmetic

Listing 2.16

$test = [datetime]"10/10/2008"
$span = $test - $(Get-Date)
"Days until 10 October = $($span.days)"

Create a test date and then subtract today's date from it to create a timespan object. Display the number of days.  Check out the other properties of timespan with get-member

 

 

 

Technorati Tags:

Date Display

While playing round with the dates for the previous post I made an odd discovery.

PS> Get-Date

27 August 2008 22:09:50

PS> $now = Get-Date
PS> $now

27 August 2008 22:09:59

PS> "Now: $($now)"
Now: 08/27/2008 22:09:59
PS>
PS> "Now: $(get-date)"
Now: 08/27/2008 22:10:37

Notice how the date and time are displayed in get-date and when setting it to  a variable.  If I now try to substitute that into a string notice how it changes.

To get it to display the way I want I need to specifically format the date and time

PS> "Now: $(get-date -format F)"
Now: 27 August 2008 22:18:10

$now = get-date -format F

PS> "Now: $($now)"
Now: 27 August 2008 22:18:35

An oddity but one I'd not seen before and something to be aware of.

 

Share this post :

 

Technorati Tags:

W2KSG: Date Parts

I am going to skip a bit of the guide as the input and error handling are quite different between VBScript and PowerShell.  We will see both of those concepts later.  For now we will jump forward to looking at dates

Script Center Home > Microsoft Windows 2000 Scripting Guide > Scripting Concepts and Technologies for System Administration > VBScript Primer > VBScript Reference > Working with Dates and Times  Retrieving Specific Portions of a Date and Time Value

$now = Get-Date
"Now: $now"
"Year: $($now.Year)"
"Month: $($now.Month)"
"DayOfYear: $($now.DayOfYear)"
"Day: $($now.Day)"
"DayOfWeek: $($now.DayOfWeek)"
"Hour: $($now.Hour)"
"Minute: $($now.Minute)"
"Second: $($now.Second)"

Use get-date to retrieve the date and then pick off the parts as shown. .NET doesn't provide for Quarter or Week of Year so we can't do those. Notice the $($now.xxx) structure.  This forces the expression to be evaluated and then substituted.

 

Share this post :

 

Technorati Tags:

W2KSG: Using Array

If we want to extend our disk checking to multiple machines we can input the computer names via an array.

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

## listing 2.12
cls

$convert = 1MB
$computers = "pcrs2", "pcrs3", "pcrs4"
$threshold = 100

foreach ($computer in $computers) {
    $disks = Get-WmiObject -ComputerName $computer -Class Win32_LogicalDisk
    foreach ($disk in $disks) {
        $free = [int]($disk.Freespace/$convert)
        if ($free -lt $threshold){Write-Host "$computer $($disk.DeviceID) is low on diskspace"}
    }
}

We then do a simple foreach loop for each computer in the array and substitute its name into Get-WMIObject.  Note that the -Computername parameter of Get-WMIObject does not accept pipeline input so we need a foreach of some kind

 

Share this post :

 

Technorati Tags: