PowerShell

Microsoft Windows PowerShell

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)

Using PSCredentials without a prompt

You cannot use get-credential without some type of prompt (although you can do it without the pop-up dialog), however you can save your securestring password to a file, reload it for later, and manually create a credential without a prompt. Of course the problem with this is that your password will be exposed to anyone with access to the file, so do this at your own risk. First, choose your password and write it to a file: PS C:\> read-host -assecurestring | convertfrom-securestring | out-file ......

Posted On Friday, February 16, 2007 1:21 PM | Comments (12)

No Windows PowerShell Snap-ins are available for version 1

You'll get the error "No Windows PowerShell Snap-ins are available for version 1" if there are no snapins installed for PowerShell v1. But the tricky part is that more precisely you'll get this error if there are no snapins installed for v1 for the platform you're running on. So you'll get this error if you are in a 32bit powershell console and you try to call add-pssnapin on some snapin that builds or installs itself only for x64 (like the Exchange Management Snap-in). You'll get the same if the ......

Posted On Tuesday, January 30, 2007 5:06 PM | Comments (0)

PowerShell and NNTP

Recently Marco Shaw posted a little nntp-stats script in the microsoft.public.windows.po... newsgroup that I thought was pretty neato, and right up my alley since we were in the middle of adding get-nntp and send-nntp cmdlets to the /n software NetCmdlets product. So as a tip of the hat to Marco, here is my version. Poster Stats: Get-nntp returns a collection of objects containing the subject, articleid, sender, references, etc of articles posted in a particular newsgroup. I put together a few ......

Posted On Wednesday, January 24, 2007 5:56 PM | Comments (0)

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)

LDAP PowerShell - Search for Disabled User Accounts

PS C:\> get-ldap -server testboy -cred $mycred -dn dc=JUNGLE -searchscope wholesubtree -search "(&(objectclass=user)(o... Again, there's no need for dozens of LDAP cmdlets. The two LDAP cmdlets included in NetCmdlets, get-ldap and set-ldap, are all you need for most tasks. The above command shows how you would search for disabled user accounts with the get-ldap cmdlet. Technorati Tags: PowerShell, LDAP, Active directory, ......

Posted On Tuesday, August 14, 2007 10:51 AM | Comments (0)

PowerShell remote exec and secure shell

This is not a post about .Net remoting. And its not a post about accessing PowerShell through ssh. Here I'm talking about sending remote executions through rexec, rshell, and secure shell connections. Rexec and Rshell (rsh.exe) already exist as command line applications, but they do not exists in the PowerShell world (that is, they do not return objects). NetCmdlets send-rexec and send-rshell cmdlets take care of this problem. Each remote execution returns a collection of objects that contain the ......

Posted On Wednesday, January 24, 2007 1:46 PM | Comments (3)

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)

Full PowerShell Archive

Copyright © Lance Robinson

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski