May 2009 - Posts

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: ,

Posted by Richard's space
Filed under: ,

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: ,,
Posted by Richard's space
Filed under: , ,

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
Posted by Richard's space
Filed under:

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:
Posted by Richard's space
Filed under:

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: ,
Posted by Richard's space
Filed under: ,

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: ,
Posted by Richard's space

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: ,,
Posted by Richard's space
Filed under: , ,

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.
 
Posted by Richard's space
Filed under:

Modules

One thing that has improved in the Windows 7 version of PowerShell is the listing of available modules.
 
PS> Get-Module -ListAvailable
ModuleType Name                      ExportedCommands
---------- ----                      ----------------
Manifest   ActiveDirectory           {}
Manifest   AppLocker                 {}
Manifest   BitsTransfer              {}
Manifest   FailoverClusters          {}
Manifest   GroupPolicy               {}
Manifest   NetworkLoadBalancingCl... {}
Manifest   PSDiagnostics             {}
Manifest   TroubleshootingPack       {}
 
Much neater and gives the information required to know whats available and can be loaded.
The full set of information on the module can still be found by using   Get-Module -ListAvailable | select *
 
 
Posted by Richard's space
Filed under:

Win 7 Event log

The PowerShell team like adding extras for us poor admins to play with so time to start poking into PowerShell on Windows 7 RC.  On the surface it seems to be about the same as Windows 7 beta \ CTP 3 but as the build number is higher
 
PS> $PSVersionTable
Name                           Value
----                           -----
CLRVersion                     2.0.50727.4918
BuildVersion                   6.1.7100.0
PSVersion                      2.0
WSManStackVersion              2.0
PSCompatibleVersions           {1.0, 2.0}
SerializationVersion           1.1.0.1
PSRemotingProtocolVersion      2.0
 
we might expect some changes.
 
One good place to start is the eventlog cmdlets.  This was available earlier but I do like the -before and -after parameters on get-eventlog
 
$now = Get-Date
$then = (Get-Date).AddDays(-5)
Get-EventLog -LogName system -Before $now -After $then -InstanceId 12
 
Id 12 is the first event my machine writes to the log when it starts up.  BTW do you know how many events get written to the log at startup.  Startup your machine. Open PowerShell
 
$date = [datetime]"05/12/2009"
(Get-EventLog -LogName system -After $date).count
 
I got 120 back. No wonder it takes a while to fully start!!
 
 
 
Posted by Richard's space
Filed under:

Import System Modules

Windows 7 has a really nice feature of combining the recently opened files\tasks with the program icon on the Start menu. This means we can go to Word and have immediate access to the most recently used files. Excellent for when I'm going back to the same file a number of times - such as writing another chapter in my book. These are also availble if you right click the icon on the task bar. As an aside combining the task bar and the quick launch bar is superb idea.
 
PowerShell has a task - Import System modules.  This will immediately start to import all modules that are located in
C:\Windows\System32\WindowsPowerShell\v1.0\Modules.
 
If you have loaded the Windows 2008 R2 RSAT pack into you Windows 7 machine - that where you will find the PowerShell modules for ActiveDirectory, Group Policy, clustering etc. And you can load them all in one go.
Posted by Richard's space
Filed under:

Messenger Annoyance

Why won’t Windows Messenger stay shut down on Windows 7?  If I shut it down its because I want it shut down.  I don’t want it to keep opening up just because the system thinks it knows better than me.

There is too much decision making happening in modern OS’s  Vista and now Windows 7 seem to be configured to do things irrespective of whether I want it to happen or not.

can’t we have a switch off all the junk mode and let me get on with it

Technorati Tags: ,
Posted by Richard's space
Filed under: ,

Live Search on Windows 7

Is there something broken in the combination of Live Search + IE 8 + Windows 7.  Searches take a ridiculous time – assuming they don’t time out.  I upgraded this machine from Vista.  Don’t remember this issue in Windows 7 beta.

Technorati Tags: ,
Posted by Richard's space
Filed under: ,

Get-PSDrive

Get-PsDrive has been one of those cmdlets that you use without really noticing – when you want to see if a provider is loaded for instance.  In the Windows 7 RC the display has had a makeover.

PS> Get-PSDrive

Name     Used (GB) Free (GB) Provider    Root               CurrentLocation
----     --------- --------- --------    ----               ---------------
Alias                        Alias
C            68.32    103.96 FileSystem  C:\                        scripts
cert                         Certificate \
D             8.82      1.79 FileSystem  D:\
E                            FileSystem  E:\
Env                          Environment
F             1.58       .37 FileSystem  F:\
Function                     Function
G                            FileSystem  G:\
HKCU                         Registry    HKEY_CURRENT_USER
HKLM                         Registry    HKEY_LOCAL_MACHINE
Variable                     Variable
WSMan                        WSMan

Notice the addition of the space that has been used and the free space on the file system drives.  Very useful

Technorati Tags: ,

PowerShell User Group - clarification

Sorry for the confusion regarding the User Group Meeting.

Yes – it is an in person event.   Microsoft Offices in Reading (TVP) Building 3.    6.30 – 9.00pm

Yes – it will be broadcast be Live Meeting – (assuming all works properly)

If you would like to try a meeting please come along.  Leave a comment here or send me an email to powershell-uk[at]hotmail[.]co[.]uk

Technorati Tags: ,

PowerShell User Group Meeting

The PowerShell User Group meeting will be 19 May

Microsoft Offices in Reading.  6.30-9.00pm

Speaker:

Dmitry Sotnikov of Quest and PowerGUI fame

We will broadcast the meeting over 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/usergroups/join
  2. Copy and paste the required information:
    Meeting ID: M8GFBD
    Entry Code: hP6]q!Rbk
    Location: https://www.livemeeting.com/cc/usergroups

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.

 

Technorati Tags: ,

MagicDisk & Windows 7

Looks like I spoke to soon – there is a Windows 7 version of MagicDisk available at http://www.magiciso.com/tutorials/miso-magicdisc-overview.htm

Installed and seems to work OK.

That’s one problem resolved quickly.

Technorati Tags: ,
Posted by Richard's space
Filed under: ,
More Posts Next page »