Showing posts with label powershell. Show all posts
Showing posts with label powershell. Show all posts

Tuesday, December 16, 2008

"which" command for Windows using Powershell

"Code Assassin" has given a great way to use 'which' command (very popular in Unix and absent in Windows CMD prompt) in Windows using Powershell. If you are too lazy to go to his blog, here is the command,

($Env:Path).Split(";") | Get-ChildItem -filter sqlwb*

Here sqlwb is the command for which you want to know 'which' one will be used when executed, in short, location(s) of the command.

Surely there are better queries than this exists at the same place but above one simply works... rather works simply.
(at occations it gives more result than acutally it should give.. but you can easily figure out unwanted)

For other direct commands see: http://blog.stevex.net/powershell-cheatsheet/
It has nice collections for DOS commands and equivalent Powershell command.

Sunday, November 30, 2008

Powershell Advocacy for consuming .NET Programs

Powershell is more than a shell. Programs initially not written to be used using shell can also be involved into Powershell script, or cmdlets. Example is my “Get List of File Associated with multiple Changesets” which could also be written in a simple c# program, that too in more user kissing manner with GUI. But compromising on user interface brings more advantages which a developer loves to have for its program, especially for the ones used by developers. Here is why:

  1. Faster to write: A power shell script written in 4-5 line in above example may take around 20-100 lines or so to write in C# program. Once you are familiar with the syntax of cmdlet scripting, I think development is very much faster.
  2. Faster to modify: If you want to change output in any manner, it’s damn easy to understand the script and change it.
  3. Easy to Run: Once u have Powershell setup, it’s just like running ipconfig or ping!! 
  4. Easy to extend the output: Output formatting provided inside Powershell is a great deal. Also, easy to write output in files and so easy to use your favorite text editor (mine is scintilla based SciTe). 
  5. Easy to Reuse: Pipe and reuse the script which u already wrote for some purpose. Or just use script inside script.

Wednesday, September 17, 2008

Get List of File Associated with multiple Changesets

I wrote a cmdlet for powershell by which you can get list of file associated with list of changesets.
param(
[string] $changesets = $(throw 'Usage: Get-Changeset-Files 'changesets')
)

process
{
# get TFS object
$tfs = get-tfs ;

# build up list of file items
$fileItems = @();

$changesets.Split(',') | foreach {
$tfs.vcs.GetChangeset($_).Changes | foreach {
if($fileItems -notcontains $_.Item.ServerItem) {
$fileItems += $_.Item.ServerItem;
}
}
}
return $fileItems | Sort-Object;
}


Steps:
1. Create a file Get-Changeset-Files.ps1 and copy above code in the file (preferably where get-tfs resides). Also add this directory into your $path
2. Open Powershell, go to Directory where above file recides
3. Run 'Set-ExecutionPolicy unrestricted' so that you can run above cmdlet
4. Everything is ready.. just run for example: Get-Changeset-Files('10, 20, 30')

See here for get-tfs.ps1

not-my get-tfs

#This is very popular file you may get at many places. Change $serverName's default value

param(
[string] $serverName = 'http://tfs-server:port/'
)

begin
{
# load the required dll
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client")

$propertiesToAdd = (
('VCS', 'Microsoft.TeamFoundation.VersionControl.Client', 'Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer'),
('WIT', 'Microsoft.TeamFoundation.WorkItemTracking.Client', 'Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore'),
('CSS', 'Microsoft.TeamFoundation', 'Microsoft.TeamFoundation.Server.ICommonStructureService'),
('GSS', 'Microsoft.TeamFoundation', 'Microsoft.TeamFoundation.Server.IGroupSecurityService')
)
}

process
{
# fetch the TFS instance, but add some useful properties to make life easier
# Make sure to "promote" it to a psobject now to make later modification easier
[psobject] $tfs = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($serverName)
foreach ($entry in $propertiesToAdd) {
$scriptBlock = '
[System.Reflection.Assembly]::LoadWithPartialName("{0}") > $null
$this.GetService([{1}])
' -f $entry[1],$entry[2]
$tfs | add-member scriptproperty $entry[0] $ExecutionContext.InvokeCommand.NewScriptBlock($scriptBlock)
}
return $tfs
}