Quick tip: Validating powershell parameters

Although powershell offers a great way of validating the parameters using advanced functions we can extend this functionality to make simple scripts robust as they can be

http://technet.microsoft.com/en-us/library/hh847743.aspx

for example here in this snipet we can validate the input string using a pattern and length

1
2
3
4
5
6
7
8
9
 Param
          (
            [parameter(Mandatory=$true)]
            [ValidateLength(5,10)]
            [String]
            $str
          ) 
$str

Above script will make sure input string length is at least 5 charcters upt 10 characters max

validate input
input string test less than 5 charecters script throws an exception

validate input 1

[ValidatePattern(“[A-Z][A-Z][A-Z][A-Z][A-Z][A-Z]”)]

adding this as a validation attriubute this will make sure string only contains letters a to z

 Param
          (
            [parameter(Mandatory=$true)]
            [ValidateLength(5,10)]
	    [ValidatePattern("[A-Z][A-Z][A-Z][A-Z][A-Z][A-Z]")]
            [String]
            $str
          ) 

$str

validate pattern

 

we can also validate from a script this what I am really interested

below is a simple function that checks if a parameter is null or empty if its not empty does it has any whitespaces similar a static method .Net 4 exposes IsNullOrWhiteSpace

 

function Isnullorempty ($str) 
{
IF( $str -eq $null ) 
 {  $true }  
elseif ( $str.ToString().Trim().Length -eq  0 ){ $true}
else {$false}
}

We are going to embed this script in validation script

 Param
          (
            [parameter(Mandatory=$true)]
            [ValidateLength(5,10)]
 	    [ValidateScript({ IF( $_ -eq $null) 
		{$false }  
	    elseif ($_.ToString().Trim().Length -eq  0 )
		{$false}
	    else {$true}} )]
            [String]$str
          ) 

$str

 

If run with blank white sure it throws an error

PS C:\Powershell> .\powershell.ps1 ” ”
C:\Powershell\powershell.ps1 : Cannot validate argument on parameter ‘str’.
The ” IF( $_ -eq $null)
{$false }
elseif ($_.ToString().Trim().Length -eq 0 )
{$false}
else {$true}” validation script for the argument with value ”
” did not return true. Determine why the validation script
failed and then try the command again.
At line:1 char:19
+ .\powershell.ps1 ” ”
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [powershell.ps1], ParameterBind
ingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,powershell.ps1

PS C:\Powershell> .\powershell.ps1  test1
test1

Advertisements

PowerShell and TFS integration

Team foundation power tools provide set of cmdlets to manage and deploy TFS content

Pre req

  1. Team Foundation – Team explorer
  2. Team Foundation – Power tools

 

I found some very use full must read basics links before we kick start

http://blogs.technet.com/b/heyscriptingguy/archive/2014/04/21/powershell-and-tfs-the-basics-and-beyond.aspx

http://blogs.technet.com/b/heyscriptingguy/archive/2012/12/28/protect-your-powershell-scripts-with-version-control.aspx

http://mscodingblog.blogspot.com/2014/02/introducing-tfsmanager-some-powershell.html

http://www.sds-consulting.com/tfs-training-webcast-automating-team-foundation-server-powershell-slides-and-code-link

http://www.sds-consulting.com/sites/default/files/TFS%20Training%20from%20SDS%20-%20Powershell%20Slides%20and%20Code.pdf

http://sdscon.codeplex.com/releases/view/60931

 

 

How to Fix : Script cannot be loaded because the execution of scripts is disabled on this system.

You can fix this issue in lot of different ways depending on your environment by running below command in powershell

1.

Set-ExecutionPolicy RemoteSigned

RemoteSigned  – all scripts you created yourself will be run, all scripts downloaded from the internet will need to be signed by a trusted publisher

2.

Set-ExecutionPolicy unrestricted

Unrestricted – Executes any script

3.

ExecutionPolicy ByPass -File “Script name”

Bypass a particular script

If you are in controlled or restricted environment

4.

Set-ExecutionPolicy -Scope “CurrentUser” -ExecutionPolicy “RemoteSigned”

or

Set-ExecutionPolicy -Scope “CurrentUser” -ExecutionPolicy “Unrestricted”