PowerShell v3 – new Verbs!

September 15, 2011 Leave a comment

I’ve had a chance to dive into PowerShell v3 in the Windows 8 preview release, and noticed something fairly small – there are 2 new verbs in the official list: “Optimize” and “Resize”. Of course the obvious next step was to see which commands these were for. And the answer is:

  • Optimize-Volume
  • Resize-Partition
  • Resize-VirtualDisk

It’s only a quick note – but hopefully the start of a new set of posts about the new v3!

Categories: PowerShell Tags:

Finding duplicate MAC addresses on VM clusters

June 22, 2011 Leave a comment

A little known fact is that you can use the group filter in PowerShell to very easily find duplicates in a set of objects based on a given object property. I had reason to use this today as our IT team was trying to validate their  virtual machines to ensure there were no conflicts due to duplication of the virtual MAC addresses. As they have a large set of machines this would require a script and PowerShell was perfect for the task. Read more…

Scripting Games 2011 start today

April 4, 2011 Leave a comment

It’s that time of year again! The Scripting Guy Ed Wilson has set up the next annual scripting games challenge and today the first event kicks off…

If you’re interested in PowerShell I highly recommend you head over to the Script Center and have a look at both the official rules and the uploading information.

Each event has both a basic and an advanced version, and today’s events are as follows:

I highly recommend you have a go, or at very least have a read of the other submissions for the events – there’s always some useful things to learn as a lot of the top PowerShell MVPs and Microsoft gurus in the field will attend and comment.

Enjoy!

Categories: PowerShell Tags:

Validating folder permissions

October 22, 2010 Leave a comment

After a short discussion with my excellent IT guys here in the office we came up with a requirement for a script which would allow us to check a set of folders for certain permissions. Specifically we needed to check if a certain account had explicit permissions set on a series of shared folders.

After I wrote the script I realized that – with some small tweaks – this script could be fairly flexible and quite useful. I wrote the script as Test-FolderACL.ps1 and the code is below:

param
(
   $path,
   $checkType = "",
   $recurse = $false
)

dir $path -Recurse:$recurse | ? {$_.PSIsContainer} | % {
   $folderName = $_.FullName

   try
   {
      $acl = Get-Acl $folderName -ErrorAction SilentlyContinue

      $acl.Access | % {
         if (($checkType -eq "") -or ($checkType -eq $_.IdentityReference))
         {
            "'$folderName' has $($_.IdentityReference) access at level: $($_.FileSystemRights)"
         }
      }
   }
   catch
   {
   }
}

The script takes three parameters: a path where it should start to look for folders, an optional account to look for in the permissions and an optional flag to allow for folder recursion. Have a play with it – it only reports what it finds, so there is no chance that it could damage your machine. As always, here are a couple of hints based on the code in the script:

Filtering collections

To ensure the selection only uses folders, I use a Where-Object command (shortened to it’s “?” alias) to filter the results so it only returns objects which have PowerShell’s PSIsContainer flag set. This flag is available in most, if not all, PowerShell providers such as file system and registry to indicate that the current item can contain subitems.

Getting security rights

Get-ACL is a command which allows you to see the Access Control List on the current file or folder, in the form of a .Net v3.5 System.Security.AccessControl.DirectorySecurity object. It contains a lot of useful information, but the data I particularly required for this script is in the Access property which allows me to see which accounts have specifically been given and denied access. This property returns a list of System.Security.AccessControl.FileSystemAccessRule objects, where the IdentityReference property gives you the account name, and the FileSystemRights property lists all the set security rights.

Boolean operators and “short-circuiting”

Remember that PowerShell, like some other languages, “short-circuits” boolean tests. Basically this means that once it can guarantee that the result of a comparison test is known, it stops evaluating the rest of the test. I use this feature to ensure that the script returns the output if you haven’t passed in an account to test for, as well as when the actual account matches one you have passed in. To see short-circuiting at work, try the following snippet of code:

$true -or $(Write-Host "foo";$false)
$false -or $(Write-Host "foo";$false)

You’ll see that if the -or operator receives a true it has to continue checking and “foo” is printed. If the first variable is false however, it stops as the output of the -or can now only be false.

Enjoy!
Chris.

Replacing file content

October 13, 2010 3 comments

One of my co-workers asked me if I knew an easy way to replace all the instances of a specific string inside a text file a little while ago. After 10 minutes of thinking about it I wrote him a very short PowerShell script which does the job nicely. At the time I didn’t think it worth writing about but I’ve decided to as it does highligh a few useful tips. So here’s the code of my Update-Content.ps1 script:
param
(
 $fileFilter,
 $find,
 $replace
)

Get-ChildItem * -filter $fileFilter -Recurse | ForEach-Object {
 (Get-Content $_.FullName) | ForEach-Object {$_ -replace $find, $replace} | Out-File $_.FullName -Force
}
It seems fairly self-explanatory but there are a few things that need to be born in mind here:
  • When using Get-Content in a pipeline, you need to remember that it will hold the file open for the duration of the pipeline process, as it is reading a line at a time. The fix is simple but not particularly obvious: put the Get-Content call in brackets. As this tells the parser to process the contents of the brackets prior to continuing, it will read the entire file contents in one go before passing the contents further down the pipeline.

 

  • Remember your context! I have two ForEach-Object loops inside each other in this example, so the $_ variable derives two local context settings with differing values. It is a common issue for subtle errors when people forget that the $_ variable from the outer loop is not accessible inside the inner loop. If you do need the value, place it in a new variable and use this in the inner loop.

 

  • The -replace operator uses global matching which means it replaces all matched entries from the input in a single call. Although this is probably what you wanted anyway, some language implementations of replace functionality default to replacing the first entry only. It is worth remembering if you use multiple languages on a regular basis.
Hopefully this is of use to you!
Chris.

Blog moved!

October 1, 2010 Leave a comment

Hi all,

After running my blog for quite some time at http://cjoprey.blog.com I finally decided that the provider simply wasn’t up to my requirements. So here we are – a fresh start, a fresh theme and – hopefully – some fresh content.

As to the articles from the previous site, I will be moving over the most interesting ones as time permits. The export/import didn’t work, even though they’re both WordPress based, so I will pull the better articles back in to this site as custom archive pages.

Here’s to a new start!
Chris.

Categories: Ramblings
Follow

Get every new post delivered to your Inbox.