I have a PowerShell script which interacts with Team Foundation Server. When I run it in the PowerShell console, it works perfectly. This is nice for testing it, but I want to run it by double-clicking on it, or on a batch file or something. I'd even settle for right-clicking on it and selecting "Run with PowerShell".
But when I do that, I get an error. "Run with PowerShell" closes the window too fast to see what the error is. Somebody was really thinking when they designed that, maybe Ballmer was involved. I can also run it in cmd.exe, like so:
PowerShell -File dostufftocheckouts.ps1
When I do that, I get to see an error message, and I'm guessing it might be the same one:
Get-PSSnapin : No Windows PowerShell snap-ins matching the pattern
'Microsoft.TeamFoundation.PowerShell' were found. Check the pattern and then
try the command again.
The following code is included in the script before anything else:
if ((Get-PSSnapin -Name Microsoft.TeamFoundation.PowerShell -ErrorAction SilentlyContinue) -eq $null )
{
Add-PsSnapin Microsoft.TeamFoundation.PowerShell -ErrorAction SilentlyContinue
}
When I start a new instance of the interactive PowerShell shell and run the script in that, everything works perfectly.
UPDATE
I get the same error with either of the following PowerShell executables (since I seem to recall the TFS snapin was 32-bit only):
C:\windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe
C:\windows\System32\WindowsPowerShell\v1.0\powershell.exe
In cmd.exe, the following command produces the following output:
c:\ powershell -Command "get-pssnapin -registered | where { $_.Name -eq 'TfsBPAPowerShellSnapIn' }"
Name : TfsBPAPowerShellSnapIn
PSVersion : 2.0
Description : This is a PowerShell snap-in that includes Team Foundation Server cmdlets.
So, I've written a very minimal script, joke.ps1:
Add-PsSnapin TfsBPAPowerShellSnapIn
$server = Get-TfsServer tfsserver/DefaultCollection
And I run it:
c:\ powershell -File .\joke.ps1
The term 'Get-TfsServer' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Users\jmcnamara\PowerShell\broken.ps1:3 char:24
+ $server = Get-TfsServer <<<< gearys/DefaultCollection
+ CategoryInfo : ObjectNotFound: (Get-TfsServer:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Add-PsSnapin doesn't give me an error. But adding the snap-in doesn't make any of the snap-in's cmdlets visible to the rest of the script.
Allegedly, Add-PsSnapin adds a snap-in to the current session:
The Add-PSSnapin cmdlet adds registered Windows PowerShell snap-ins to the current session. After the snap-ins are added, you can use the cmdlets and providers that the snap-ins support in the current session.
"You" can, eh? "You" who? Yeah, sure you can.
But how?