Monthly Archives: May 2009

Hidden Files

The last post got me thinking about hidden files and file attributes and how we can manipulate them in PowerShell.  Lets start by creating a PowerShell script.

PS> "'hello world'" > test.ps1
PS> ./test.ps1

We can see the file in a directory listing

PS> Get-ChildItem test.ps1

    Directory: C:\scripts

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---        30/05/2009     13:57         32 test.ps1

 

We can view the file attributes

PS> Get-ItemProperty -Path test.ps1 -Name Attributes

PSPath       : Microsoft.PowerShell.Core\FileSystem::C:\scripts\test.ps1
PSParentPath : Microsoft.PowerShell.Core\FileSystem::C:\scripts
PSChildName  : test.ps1
PSDrive      : C
PSProvider   : Microsoft.PowerShell.Core\FileSystem
Attributes   : Archive

We can set the attributes so the file becomes hidden using the Fileattributes enumeration

PS> Set-ItemProperty -Path test.ps1 -Name Attributes -Value ([System.IO.FileAttributes]::Hidden)

as shown previously we have to use the force to get a listing

PS> Get-ChildItem test.ps1 -Force

    Directory: C:\scripts

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
---h-        30/05/2009     13:57         32 test.ps1

But we have over written the existing attributes – oops.  Need to recreate the file and try again.  This time we will add the attribute.

PS> Set-ItemProperty -Path test.ps1 -Name Attributes -Value ((Get-ItemProperty -Path test.ps1).Attributes -bxor [System.IO.FileAttributes]::Hidden)

and we now see the existing attributes are preserved


PS> Get-ChildItem test.ps1 -Force

    Directory: C:\scripts

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a-h-        30/05/2009     14:11         32 test.ps1

We can clear all the attributes or just rest to those we require

PS> Set-ItemProperty -Path test.ps1 -Name Attributes -Value ([System.IO.FileAttributes]::Normal) -Force
PS> Get-ChildItem test.ps1 -Force

    Directory: C:\scripts

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-----        30/05/2009     14:11         32 test.ps1

The full range of attributes can be seen

PS> [enum]::GetNames([System.IO.FileAttributes])
ReadOnly
Hidden
System
Directory
Archive
Device
Normal
Temporary
SparseFile
ReparsePoint
Compressed
Offline
NotContentIndexed
Encrypted

Normal rules still apply so we can’t compress an encrypted file

Removing Hidden files

I used the OneNote 2007 Side Note utility this morning and for some reason it created a table of contents file ( .onetoc2 ) file in each of the folders in which I keep my PowerShell. Strange behaviour.  Any way there are more of them than I want to delete by hand.

Get-ChildItem doesn’t show them – they are hidden files so we need to use

Get-ChildItem -Filter "*.onetoc2" -Force –Recurse

then we can pipe that into Remove-Item

Get-ChildItem -Filter "*.onetoc2" -Force -Recurse | Remove-Item –Force

again using the Force.  All gone.

And that young Luke is how you get rid of hidden files. Pity I didn’t post this on 4th May

Technorati Tags: ,

Live Search on Windows 7 & IE 8

In this post http://richardsiddaway.spaces.live.com/default.aspx?_c01_BlogPart=blogentry&_c=BlogPart&handle=cns!43CFA46A74CF3E96!2349 I was complaining that Live Search had stopped working on my Windows 7 build.  I had a Vista machine that I had upgraded and post the upgrade Live Search wouldn’t work.  I’d also noticed that Live Search had stopped working on a Vista machine that had been upgraded to IE8.

I finally got round to trying to sort out the problem.  Going for the easy option first I went into Control Panel – Programs & Features, selected Windows Live Essentials and clicked uninstall\change and then picked the Repair option.

One thing I noticed was that during the repair process the tool bar was fixed as was the Search Enhancement pack.  Also there is a lot of stuff associated with the Live applications.

One reboot later and Live Search is working properly for me on Windows 7 & IE 8.  Next stop the Vista machine. One suggestion is that the upgrade program needs to pick up on the Live Search issue.  I will probably do a complete rebuild when Windows 7 goes to RTM.  Might avoid some of these issues

Technorati Tags: ,,

Friday 19 June 2009

Where are you going to be on Friday 19 June?

Glued to your computer – thats where.

Why?

Because it is the TechNet Virtual Conference – that’s why.  Lots of great sessions on all sorts of new Microsoft technologies.

First Important point

Yes there is a PowerShell session.  I am presenting so of the great stuff that can be done with PowerShell v2 and a quick look at PowerShell in Windows 2008 R2 as a bonus.

Other important bits

1. Live date is Friday June 19th

2. View full agenda and register here www.microsoft.com/uk/technet/govirtual

3. Competition – Enter the free prize draw to win one of 500 goody bags, which is a snazzy laptop bag with some great event swag, terms and conditions apply.

And there’s more

An exclusive Keynote featuring Mark Russinovich, Microsoft Technical Fellow specialising in the Windows platform.

You also get chance to send questions to the presenters – just like a normal event

Definitely the place to be on 19th June.  See you there

Chapter 12 MEAP – PowerShell in Practice

Chapter 12 - dealing with Exchange servers is available on MEAP - http://www.manning.com/siddaway/

 

Chapter 13 (IIS) is coming along nicely and should be available soon

 

Enjoy

Unix to PowerShell

PowerShell already uses some Unix commands as aliases e.g. ls is alias for Get-ChildItem.

Joe Pruitt is starting a series of posts that will create PowerShell equivalents for some of the most popular Unix tools.  First up is md5

http://devcentral.f5.com/weblogs/Joe/archive/2009/05/18/unix-to-powershell---md5.aspx

Technorati Tags:

A real windows 7 annoyance

One thing that is really driving me nuts with windows 7 is the way it keeps deciding to maximise Windows just because I happen to move them to a particular point on screen. if I want a Window maximised I will maximise it. Stop making decisions on my behalf.

I’ve had to use Windows XP recently and it is sorely tempting to junk Windows 7 and Vista and go back to XP.  At least it didn’t keep trying to tell me what to do.

I am really getting to the disappointed stage with Windows 7.  The beta I thought was good. RC is not good & the fact that Live Search doesn’t work with IE 8 in appalling

Technorati Tags: ,

Scripting Games 2009

The last few years have seen Microsoft running the annual Scripting Games – usually early in the year.  Don’t worry if you think you’ve missed them as this year they will be in the Summer – June 16-26.

See - http://www.microsoft.com/technet/scriptcenter/funzone/games/games09/announcement.mspx for the announcement and

see http://www.microsoft.com/technet/scriptcenter/funzone/games/default.mspx for the previous years events.

while ($true) {“I will enter the PowerShell Scripting Games”}

Technorati Tags: ,

Combining Output

A recent question of the PowerShell forum asked how the output of two scripts could be combined.  The scripts in question were using Get-ChildItem and Get-Acl to pull back two sets of information related to the file.

PowerShell produces objects. .NET objects with a PowerShell wrapper. The questioner was looking for something like the UNION operator in TSQL. Unfortunately, you can’t combine objects like that. Trying to match up the properties and methods would be a nightmare.

So we need to combine the way we produce the information to create a single object with all of the data we require

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
$data = @()
Get-ChildItem -Path "C:\scripts" -Recurse | foreach {
    $file = New-Object -TypeName System.Management.Automation.PSObject
Add-Member -InputObject $file -MemberType NoteProperty -Name "FullName" -Value $_.Fullname
Add-Member -InputObject $file -MemberType NoteProperty -Name "LastAccessTime" -Value $_.LastAccessTime
    Add-Member -InputObject $file -MemberType NoteProperty -Name "LastWriteTime" -Value $_.LastWriteTime
   
    $acl = Get-Acl -Path $_.FullName
    Add-Member -InputObject $file -MemberType NoteProperty -Name "Owner" -Value $acl.Owner
    Add-Member -InputObject $file -MemberType NoteProperty -Name "AccessToString" -Value $acl.AccessToString
    Add-Member -InputObject $file -MemberType NoteProperty -Name "Group" -Value $acl.Group
   
    $data += $file
}
$data | Export-Csv -Path c:\scripts\acl.csv -NoTypeInformation

 

Start by using Get-ChildItem to read the directory lists.  Then foreach object we create a new object and use Add-Member to create some properties on it. In this case we pick off the full name of the object and the last times it was accessed & written to.

We also need to get some ACL information. We can use Get-Acl within our loop – we only read the file directories once this way. And then we add a few more properties.  Add the object to our array and loop back for the next file.

At the end we can dump the information to a csv file for future use.

Technorati Tags: ,,

NetworkLoadBalancingClusters

Looking at the module list available when you install the Windows 2008 R2 RSAT pack on Windows 7  I noticed one called NetworkLoadBalancingClusters.  Well really its called NetworkLoadBalancingCl... in most displays. 

 

NetworkLoadBalancingClusters must the longest of module names!

 

Still when we import it we get a long list of cmdlets - (Get-Command -Module NetworkLoadBalancingClusters).count gives an answer of 35

 

Add-NlbClusterNode
Add-NlbClusterNodeDip
Add-NlbClusterPortRule
Add-NlbClusterVip
Disable-NlbClusterPortRule
Enable-NlbClusterPortRule
Get-NlbCluster
Get-NlbClusterDriverInfo
Get-NlbClusterNode
Get-NlbClusterNodeDip
Get-NlbClusterNodeNetworkInterface
Get-NlbClusterPortRule
Get-NlbClusterVip
New-NlbCluster
New-NlbClusterIpv6Address
Remove-NlbCluster
Remove-NlbClusterNode
Remove-NlbClusterNodeDip
Remove-NlbClusterPortRule
Remove-NlbClusterVip
Resume-NlbCluster
Resume-NlbClusterNode
Set-NlbCluster
Set-NlbClusterNode
Set-NlbClusterNodeDip
Set-NlbClusterPortRule
Set-NlbClusterPortRuleNodeHandlingPriority
Set-NlbClusterPortRuleNodeWeight
Set-NlbClusterVip
Start-NlbCluster
Start-NlbClusterNode
Stop-NlbCluster
Stop-NlbClusterNode
Suspend-NlbCluster
Suspend-NlbClusterNode

 

Looking at this list seems we can do just about everything we need around NLB with this module.

 

I'm busy writing the IIS chapter of my book at the moment and this complements the IIS provider very well.   So I can add a new node to my NLB cluster, create the web site and copy the application onto it  - all from one script.