PowerShell

Microsoft Windows PowerShell

Creating Team Foundation Work Items With PowerShell

Here is the simple function I added to my powershell profile: function create-tfstask ($desc) { tfpt workitem /new $workItemType /collection:$serverUrl /fields:"Title=$($desc);Ass... To=$assignee;AreaId=$areaId... } To find the AreaId and IterationId for your project/versions, just look at a bug you know is setup correctly: PS C:\> tfpt query /collection:http://3bs001vs... /wiql:"select AreaId,IterationId from workitems where ID = 879" Next I hooked this into ......

Posted On Wednesday, January 26, 2011 10:58 AM | Comments (1)

PowerShell – Recycle All IIS App Pools

With a little help from Shay Levy’s post on Stack Overflow and the MSDN documentation, I added this handy function to my profile to automatically recycle all IIS app pools. function Recycle-AppPools { param( [string] $server = "3bhs001", [int] $mode = 1, # ManagedPipelineModes: 0 = integrated, 1 = classic ) $iis = [adsi]"IIS://$server/W3SVC/... $iis.psbase.children | %{ $pool = [adsi]($_.psbase.path); if ($pool.AppPoolState -eq 2 -and $pool.ManagedPipelineMode -eq $mode) { # AppPoolStates: ......

Posted On Thursday, December 16, 2010 3:59 PM | Comments (0)

PowerShell book

Bruce Payette's Windows Powershell in Action is now available on Amazon ......

Posted On Tuesday, February 27, 2007 11:46 AM | Comments (0)

Quick PowerShell “Todo” Gmail Emailer

I put the following function in my PowerShell profile so that I can just bring up my PowerShell console and type myself a quick “todo” note that automatically gets emailed to me for later. There are several things of note in this tiny script: 1. Note the use of the $args variable to effectively allow the user to enter $msg parameter without quotes even if it contains whitespace. 2. In this particular example I’m sending the email through Gmail. smtp.gmail.com, port 587, SSL and authentication required. ......

Posted On Tuesday, October 26, 2010 1:27 PM | Comments (4)

PowerShell TruncateAtWhitespace Function

Here’s a TruncateAtWhitespace function that takes an incoming parameter value and an incoming max length, and returns a substring broken at a whitespace position. This way if you have “Lance has a blog” and you need to truncate it to 8 characters or less, you get “Lance” instead of “Lance ha”. function TruncateAtWhitespace{ param( [string]$value, [int]$maxlength=200 ) $maxlength-=3; #allow for "..." suffix if ($value.Length -le $maxlength) { return ($value + "..."); } $closestwhitespaceindex = [int]$value.Substring(0, ......

Posted On Monday, August 16, 2010 3:30 PM | Comments (0)

Exception setting "ThreadOptions" in SharePoint 2010 Management Shell

Right after you install SP2010 (Foundation or Server), even on a completely updated Windows Server 2008 SP2, when you go to start the SharePoint 2010 Management Shell (the recommended PowerShell interface for working with SharePoint), you get a real ugly looking error: Exception setting "ThreadOptions": "This property cannot be changed after the Runspace has been opened." At C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONF IG\POWERSHELL\Registration\... char:48 ......

Posted On Wednesday, February 17, 2010 1:56 PM | Comments (1)

PowerShell: Twitter Auto UN-follow

Anybody who uses Twitter has heard of auto-follow services for Twitter. Below is a PowerShell script I used to auto UNfollow. The script goes through the people you're following and eliminates "idle" or "spam" friends. An idle friend is determined by a low friend count or low status count on an account that has been in existance for at least a couple months (configurable). A spam friend is determined by a high friend count but a low follower count (by default, the script uses a 15-1 ratio as the ......

Posted On Tuesday, January 12, 2010 1:06 PM | Comments (4)

Program Files Environment Variable in PowerShell

Today I saw this on Twitter from Julie Blender (@#juneb_get_help): Need to reference the "ProgramFiles(x86)" environment variable in #PowerShell? Use ${env:ProgramFiles(x86)}. I use this in a lot of my scripts, but I want a single variable that contains the right value whether I'm in a 64 bit shell or not. Here's my solution: First, I have a function in my testing profile called is64bit, that looks like this: function is64bit() { return ([IntPtr]::Size -eq 8) } Next, I have a get-programfilesdir ......

Posted On Tuesday, December 29, 2009 7:27 AM | Comments (2)

10 PowerShell One Liners

Here are a few one-liners that use NetCmdlets. Some of these I've blogged about before, some are new. Let me know if you have questions, which ones you find useful, or how you altered these to suit your own needs. Send email to a list of recipient addresses: import-csv users.csv | % { send-email -to $_.email -from lance@nsoftware.com -subject "Important Email" –message "Hello World!" -server 10.0.1.1 } Show the access control list for a specific Exchange folder: get-imap -server $mymailserver -cred ......

Posted On Tuesday, December 29, 2009 4:36 AM | Comments (1)

PowerShell: Smart Parameter Expressions

A while back I posted about how to mark a script parameter as required in PowerShell. Shortly afterwards, there was some chatter on it that I think makes it worthwhile to add another post on the subject of PowerShell script parameters. Let’s say you have the question: “Can I do <X> with a parameter?” The answer is probably “Yes”. :) Jim and an anonymous emailer both asked: Is there any way you can specify a parameter based on the value of another parameter? The answer is yes. You can put pretty ......

Posted On Tuesday, December 8, 2009 1:49 AM | Comments (0)

PowerShell Command Line Emailer

I’ve talked about this before, but it came up again today so I thought I’d do a quick post about it. Here’s a function I use in my PowerShell profile that uses NetCmdlets to send quick “todo” emails: function todo($target="work", $msg) { switch ($target) { "work" { $target = "lancer@nsoftware.com" } "home" { $target = "lmrobins@gmail.com" } } send-email -from lancer@nsoftware.com -to $target -subject ("Todo: " + $msg) -message $msg -server 10.0.1.1} During the day, when I need to send myself a reminder, ......

Posted On Tuesday, October 20, 2009 8:07 AM | Comments (0)

5 PowerShell Oneliners to Manage IMAP Permissions

You can manage user permissions in Exchange and other mail servers with the same commands by using the IMAP protocol instead of server-specific tools. Here is how! Jumping right in, these all use get-imap and set-imap from NetCmdlets: #1 – Get ACL Here’s a one-liner to get the ACL for a specific folder in an account. In this particular case, my main INBOX has a subfolder named RESUMES, and I want to see who has what permissions to this folder. PS C:\> get-imap -server $mymailserver -cred $mycred ......

Posted On Thursday, June 4, 2009 11:11 AM | Comments (0)

Get the number of current SharePoint connections

Here is a PowerScript function to automate the process of getting/logging the current number of connections to a particular Web Server. It will tell you the number of connections for each site on the server, so you can see the number of SharePoint connections, SharePoint Central Admin connections, etc. Thanks to MOW and Lee Holmes for their examples of using the PerformanceCounter class in PowerShell. function Get-WebServiceConnections(){ $results = @{} $perfmon = new-object System.Diagnostics.Performa... ......

Posted On Wednesday, June 3, 2009 10:29 AM | Comments (1)

Troubleshooting FTP connections with PowerShell

I find that helping people troubleshoot FTP connectivity issues is much easier if they have PowerShell installed. I can just have them run the NetCmdlets ftp cmdlets with the –debug switch, which displays a trail of information about the communication over the wire. In this way you can see every command send to the server and every response coming back from the server, including what ports a server is attempting to use for ftp data connections. Very often ftp transfer connectivity errors are the ......

Posted On Thursday, May 21, 2009 1:22 PM | Comments (0)

Managing MS Exchange Remotely From Unix, Phone, Windows, etc.

Recently I saw a question come to my friend Spencer here at /n software about whether or not it was possible to use the Microsoft Exchange 2007 SP1 PowerShell cmdlets through the /n software PowerShell Server. The person wanted to be able to manage Exchange from a remote Unix machine. Of course, as I’ve mentioned before, it could be done from anywhere that supports SSH – like an Apple iPhone or a Blackberry. The PowerShell server uses impersonation - it impersonates the user that the SSH client authenticated ......

Posted On Wednesday, May 6, 2009 12:55 PM | Comments (1)

PowerShellRSS Automatically Converts PSObjects to RSS Items

In a previous post, I talked about how PowerShellASP can be used to generate an RSS feed with PowerShell. Now I’ll show how the same feed can be generated more easily with PowerShellRSS. Again, the dir command in PowerShell (get-childitem) is used to get a listing of files to use as enclosures in the feed. # This example demonstrates how to generate an RSS feed from a call to the Get-ChildItem cmdlet (dir). # To tailor to your specific needs, set the following values: $mediadir = 'C:\Testing\media';$virtual... ......

Posted On Tuesday, May 5, 2009 5:42 PM | Comments (1)

PowerShell Prompt inside SharePoint

Using the PowerShellToys PowerShellWebPart, I was able to insert a PowerShell command prompt inside my SharePoint page with only a few lines of code and some html. Here is the PowerShellWebPart template that I used: <div style="background-color:Dar... color:white; height:400px; width:650px"> <form method="GET" action="" enctype="multipart/form-dat... PS> <input type=text name="query" size="40" value="<%=$request['quer... /> <input type=submit value="Execute" /> ......

Posted On Tuesday, May 5, 2009 3:47 PM | Comments (0)

New Toys for PowerShell!

At PowerShellToys.com, you’ll find a bunch of new toys for PowerShell, including the first full release of PowerShellASP + free community license. The new toys? PowerShellADO, PowerShellPanel, PowerShellWebPart, and PowerShellRSS. Here’s a brief description of each, in my own words. For the official description and examples, check out PowerShellToys.com. PowerShellASP – write ASP pages with PowerShell scripting instead of vbscript. PowerShellADO – execute PowerShell cmdlets and return the results ......

Posted On Monday, May 4, 2009 5:00 PM | Comments (1)

PowerShellASP – Generate an RSS feed from PowerShell cmdlets

PowerShellASP is a powerful tool for generating web pages with PowerShell. It allows you to insert PowerShell script directly into a webpage (a .ps1x page), that code will be executed and the resulting powershell objects are renderable in your page. It also exposes familiar $response, $request, $server, etc. for accessing response, request, and other information about the HTTP context. This example shows how to use PowerShellASP to generate an RSS podcast feed. The dir command in PowerShell (get-childitem) ......

Posted On Tuesday, April 21, 2009 3:08 PM | Comments (2)

Get-Ldap NetCmdlet in PowerShell

I saw Jeff Hicks’ great Get-LocalMember post this morning, in which he has extensive demonstration of retrieving information about AD group members. I thought it might be a good time to show some of the power of the get-ldap cmdlet. Yes, using the get-ldap cmdlet does require familiarity with the LDAP protocol itself, so in this way it is for more advanced users who just need to do quick LDAP operations without a lot of required coding and with just one universal cmdlet. So, how do I list the group ......

Posted On Thursday, April 9, 2009 11:29 AM | Comments (2)

Full PowerShell Archive

Copyright © Lance Robinson

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski