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 NetCmdlets -debug

Its easy to forget about the –debug parameter, but a lot of times it can be very helpful, especially with NetCmdlets where in many cases –debug will output the protocol interface (the “PITrail” as we call it at /n software) of the connection. The PITrail includes a trail of communication between the cmdlet and the remote host it is talking to. Rob emailed me to ask for help diagnosing a 425 error he was getting when using the NetCmdlets get-ftp cmdlet. As I described to Rob, in a nutshell the 425 ......

Posted On Thursday, September 11, 2008 4:41 PM | Comments (0)

PowerShell: tweet-im

Mark Schill was another of the winners of the PowerScripting Podcast’s NetCmdlets scripting contest. Mark submitted a tweet-im script, which sends an instant message about new tweets found in the Twitter RSS feed. They explained on the podcast that Twitter used to have an IM-bot feature which went away, so Mark created this script himself to meet this need. The script simply sits and fetches your Twitter friends timeline feed every x seconds, checks to see if the items it finds are newer than the ......

Posted On Thursday, September 11, 2008 10:56 AM | Comments (0)

PowerShell: send-gmail

Over at the PowerScripting Podcast, the winners of the NetCmdlets scripting contest were announced. Meanwhile I was having some awesome vacation time in the beautiful Outer Banks of North Carolina. Winners for the /n software Netcmdlets contest are: Steve Hiner - Wrote a wrapper for Send-Email script to send email using Gmail's SMTP server Mark Schill - Sent in two entries, the one we've chosen to highlight is his Twitter IM client bot thingy. It uses Send-IM and Get-Http Quintus - A script to download ......

Posted On Wednesday, September 10, 2008 5:28 PM | Comments (0)

PowerShell – Base64 Encode/Decode a string

An easy way to convert data to/from base64 (or uuencode/uudecode, quoted-printable, url encode/url decode, JIS, Yenc, hex, or even creating md5 or sha1 hashes) is to use the NetCmdlets convert-data cmdlet. To encode: PS C:\> convert-data -data "Hello, World!" -to base64 Data Size Format ---- ---- ------ SGVsbG8sIFdvcmxkIQ== 20 base64 To decode: PS C:\> convert-data -data $encoded.Data data -from base64 Data Size Format ---- ---- ------ Hello, World! 13 Plain Technorati Tags: PowerShell, NetCmdlets, ......

Posted On Friday, August 29, 2008 1:24 PM | Comments (0)

Full PowerShell Archive

Copyright © Lance Robinson

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski