I have a PowerShell script that interacts with Team Foundation Server. When I run it in PowerShell, it works fine. This is good for testing, but I want to run it by double-clicking on it, either in a batch file or something like that. I even agreed to right-click on it and select "Run with PowerShell".
But when I do this, I get an error message. "Run using PowerShell" closes the window too quickly to find out what the error is. Someone really thought when they developed it, maybe Ballmer was involved. I can also run it in cmd.exe, for example:
PowerShell -File dostufftocheckouts.ps1
When I do this, I see an error message, and I assume that it can be the same:
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 PowerShell interactive shell and run the script in this, everything works fine.
UPDATE
I get the same error with any of the following PowerShell executables (since it seems like I remember that the snapin TFS was only 32-bit):
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 wrote 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 does not give me an error. But adding a snap does not make any of the binding cmdlets visible to the rest of the script.
Presumably, the Add-PsSnapin cmdlet adds snap to the current session:
Add-PSSnapin Windows PowerShell . , .
"" , ? " ? , .
?