Skip to main content


Showing posts from May, 2012

Fix: The number of ads you can show is limited

Anytime you can get a good new from Google that they have updated their policy and keeping track of your blog traffic quality. And you never know when your account gets ban or suspended which can incur a heavy loss of your hard-earned dollars. Like many of us including me got the message “Number of ads you can show has been limited” and suddenly ads stop showing on the blog. I know this is not good news for anyone & obviously not all Google updates are good news for bloggers.  For more information, you can check the policy center link provided with the message. As per Google AdSense, the main reason for the temporary serving limit placed on your AdSense account might be Your site traffic is not organicYou are clicking on your own adsYou are using untrusted or low-quality service to increase your traffic. Fig 1. “Number of ads you can show has been limited” message Although many bloggers reported that their traffic is organic, and they don’t use untrusted service or do any unwanted …

PowerShell (v2) : Get-WinEvent Filtering Performance Testing and Analysis

Having been on an optimization and performance testing kick for the past week or so I stumbled across one I hadn't seen yet. Get-WinEvent can be a hog at times, so, I avoid it unless I must, particularly when hunting for specific conditions. To date I have used Get-WinEvent with a pipelined Where approach. Slow to say the least. While reading Get-Help I found an example that highlighted some filtering approaches that can significantly improve performance:
-FilterHashTable-FilterXML-FilterXPathNot being familiar with any of these approaches I ran example 14 in the v2 Get-Help for Get-WinEvent and did some testing. I did make a minor modification to the on-the-box help examples to have data I could use to get results. Here is my approach: Clear-Host

$test1= { # Use the Where-Object cmdlet $yesterday= (get-date) - (new-timespan-day 1); $where=get-winevent-logname"application" | where {$_.timecreated -ge$yesterday} }

$test2= { # Uses FilterHashTable $yesterday= (get-date) - (new-time…

PowerShell (v2) - You Can Omit Get- Here's Why

So, I saw this post fly across the Twittersphere earlier today:
Ifiok Moses @mifiok
RT @bradwilson: Just discovered today that in PowerShell, you can leave off "get-". Seriously, try it: type "childitem" or "location". true
Not having heard of this, I tried it out and sure enough, it works. What I wanted to know was how. After pinging Justin about it he reminded me of the great and glorious Trace-Command cmdlet to check it out if I was really curious. Knowing we were looking at a pretty specific command to trigger this in Tracing I tried a few options and hit paydirt with the commandiscovery tracesource. Using this command:
Trace-Command -Name *commanddiscovery* -Expression {acl C:\test} -PSHostI got a good bit of general chattiness indicating it could not find any command in the usual places, i.e., the $env:path folders one by one. This is essentially the same pattern I outlined in this post:
Powershell (v3) - Tab Completion Takes Forever sometimes. Here&#…

PowerShell (v2) - Function: Set-IISProcessModelLoadUserProfileBool

In a hurry, so not much time to do anything other than throw this out there. The real challenge for this one was finding the right way to find and set the global value. Using my favorite IIS link I tracked it down. For some reason the indexing starts at 1 instead of 0. Another mystery for another day.
<#        .NOTES               Author: Will Steele (               Last Editted Date: 05/23/2012        .EXAMPLE               Set-IISProcessModelLoadUserProfileBool 1               This example demonstrates how to set the global LoadUserProfile value to $true.        .EXAMPLE               Set-IISProcessModelLoadUserProfileBool 0               This example demonstrates how to set the global LoadUserProfile value to $false. #>
functionSet-IISProcessModelLoadUserProfileBool { param(               [Parameter(                      Mandatory =$false               )]               [Bool] $Bool=$true        ) $LoadUserProfile= (Get-WebConfigurationProperty-Filter /system.applicationHost/A…

PowerShell (v2) - PSDebug: Free Backstage Limited Pass to the Show

This going a little old school, back to the MSH prefxies, but, I'll bring it up to speed shortly. While looking around the other day I stumbled across Jeffrey Snover's post:
Debugging Monad Scripts, Part 4: set-mshdebug In the post we see the roots of the current cmdlet Set-PSDebug. If you have never used this cmdlet it can be very helpful in pointing out issues that may be hampering sucessfully running some command or script. Being the ever vigilant tinkerer I am I decided to play with this and found some cool things. Aside from the realization that $ErrorView could be played with and was tested in the process of running PSDebug I saw a new ability to peek under the hood of just what all was going on when things ran. Now, I am coming to look at things in a variety of layers.

 First, you can code your scripts with [CmdletBinding()] and Write-Verbose to give you two modes: regular or verbose. This adds a lot of overhead, however, as you have to write a good bit of extra code, …

PowerShell (v2) - $ErrorView = "CategoryView"

Tiny comment, but, a useful one. Let's say you work in shell a lot and you get sick of having your coworkers harass you about all that red on your screen. You could change the colors, but, that would be too easy. Instead, you want a less chatty error message. Well, then, my friend, it sounds like you need 'CategoryView'.  From an old PowerShell blog post:
$ErrorView = "CategoryView"Hmmm, new to me. Exactly what does that terse little command do. Let's break something and find out.
1/0Yeah, that worked.
Attempted to divide by zero.
At line:1 char:3
+ 1/ <<<< 0
    + CategoryInfo          : NotSpecified: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : RuntimeExceptionNow, let's change our $ErrorView to "CategoryView".
$ErrorView = 'CategoryView'and blow up more stuff:
1/0Well, much less shrapnel:
NotSpecified: (:) [], ParentContainsErrorRecordExceptionIn essen…

PowerShell (v2) - Create a Centralized Folder with Desktop Management Icons

Again, another hard one to title, but, a useful trick for someone wanting to set up a new box or reimaged machine. One of our techs mentioned the God mode trick:
How to Activate GodMode in Windows 7Thinking, "That's a cool trick, let me PowerShell it." I came up with this intitially:
Explorer (md 'Test.{ED7BA470-8E54-465E-825C-99712043E01C}')Okay. Stupid pet trick, but, fun. Thinking of how I could make this more useful, however, I came up with this approach to create a new folder to house a bunch of these sorts of utility links:

# This script creates several regularly used interfaces in one folder for ease of use.
# New folder location
$storagefolder = 'C:\test\myhappyplace'
# Links to create
$folderstocreate = @(
'All Tasks.{ED7BA470-8E54-465E-825C-99712043E01C}',
'Computer and Devices.{F02C1A0D-BE21-4350-88B0-7367FC96EF3C}',

PowerShell (v2) - -Replace Does not Like FilePaths with Slashes

In a recent script I needed to swap out a portion of a filepath with another. "Great", I thought, "-replace to the rescue." Wrongo. After I tried this out
'c:\test' -replace 'c:\test','D:\test'and got no changes,
c:\testI recalled some issues with slashes and replace which led me to run this test:

'c:\test' -replace 'c:\\test','D:\test' successfully I might add:

D:\test So, I started looking for a workaround.
In case you are wondering why this is the case, -replace works with the regular expression engine and \, without any other characters following it, is an escape character. To properly deal with this you need a \\ in each place where a \ would normally be to be handled by -replace.

Thankfully, the .NET class

String.Replace Method (String, String)allows me to play much more nicely than the -replace operator.  For instance, if I do this:
$string = 'C:\test\test.txt'

PowerShell (v2) - Function: Show-HiddenFolder

As a corollary to my Hide-Folder function this simply switches a folder item's Attibutes to Directory.functionShow-HiddenFolder { param( $foldername       ) if((Test-Path$foldername) -and ((Get-Item$folderName).PSIsContainer))       {             $(Get-Item$foldername-Force).Attributes ='Directory'       } else       { Write-Error"The folder ($($foldername)) was not found."       } } I am sure there are a hundred better ways to do this, but, this is what I came up with for a quick fix.

PowerShell (v2) - Sort Folders by Name as Versions Objects

Working with developers can pose challenges at times. In their world objects are just obvious, intuitive structures you are expected to deal with. Ok. No problem. Adapt = survive. I can live with that. So, sometimes the laissez faire attitude of adaptation can be fun. In this case, I needed to find the last two build folders in a directory.  So, for non-developer types the System.Version class gives you an easy way to pattern Visual Studio builds incrementally without any effort.  Ok. I can work with that.

Where it gets cool, in my case, is that you can compare Version objects. Here is a post talking about the basics of that operation:
PowerShell: Comparing Version numbersCool enough. But, I did not want to build my own sort algorithm to work with custom object types for a simple folder sort. I looked at appending a Version object as a property with Add-Member via PowerShell, but, that didn't go very far anyway. Windows, when you parse the name as a Version object, still uses the Ex…

PowerShell (v2) - Gathering Details for Updating IIS

Over the past year I have accumulated a lot of scripts working with IIS. To help explain how I come up with some of my settings I have decided to document my approach. The information here pertains to PowerShell v2 and IIS 7/7.5.  To begin the process open up an instance of IIS Server Manager.  In this example, I will be updating the server level SMTP from address.  To demonstrate the tranditional, GUI-based task I will show screenshots of how you would do this in IIS first.

First, highlight the server level object:

In this case I have updated the E-mail address value to a dummy setting.  Next, I switch to the Configuration Editor (the icon with the red arrow below it in the image below).

Once in the configuration editor I navigate to the node I need to update.  In this case, it is  Note, the tree levels correlate to slashs in PSPaths.  So, as I have it listed is how you will use it in PowerShell, but, we will get to that.  Below is a screenshot of how to fi…

PowerShell (v2) - Function: Hide-Folder

While working on a project I came up with a quick need to hide a folder, so, I wrote this canned function:
functionHide-Folder { param( $foldername       ) if(Test-Path$foldername)       {             $(Get-Item$foldername).Attributes ='Hidden'       } else       { Write-Error"The folder ($($foldername)) was not found."       } } The source of the .Attributes trick was:
Get or set file\directory attributes using Powershell