Discussion:
typeof() in PowerShell?
(too old to reply)
Alan Stevens
2006-05-10 18:12:02 UTC
Permalink
I am attempting to use PowerShell to make batch updates to workitems in team
foundation server. I have converted a C# program for the task, and have
gotten this far:

$key = gp HKLM:\SOFTWARE\Microsoft\VisualStudio\8.0

$dir = [string] (gp $key.InstallDir)

$dir+="PrivateAssemblies\"

$lib = $dir+"Microsoft.TeamFoundation.WorkItemTracking.Client.dll"

[Reflection.Assembly]::LoadFrom($lib)

$lib = $dir+"Microsoft.TeamFoundation.Client.dll"

[Reflection.Assembly]::LoadFrom($lib)

[Console]::WriteLine("Please enter your Team Foundation Server Name:")
$server = [Console]::ReadLine()
$server = $server.Trim()

"Connecting to " + $server + "..."
$tfs =
[Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($server)

The next line in the C# program is:
WorkItemStore store = (WorkItemStore)tfs.GetService(typeof(WorkItemStore));

I have so far been unsuccessful in passing the type object to the
GetService() method. How do I perform the equivalent of typeof() in
PowerShell? Any guidance would be greatly appreciated.

++Alan
Kevin Loo [MSFT]
2006-05-10 18:27:46 UTC
Permalink
Try the [] operator. For example, System.Reflection.Assembly.GetAssembly
takes a Type variable. It could be invoked as follows.

PS C:\PS> [System.Reflection.Assembly]::GetAssembly([string])

GAC Version Location
--- ------- --------
True v2.0.50727
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorlib...
--
Kevin Loo [MSFT]
Windows PowerShell Development
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.
Post by Alan Stevens
I am attempting to use PowerShell to make batch updates to workitems in team
foundation server. I have converted a C# program for the task, and have
$key = gp HKLM:\SOFTWARE\Microsoft\VisualStudio\8.0
$dir = [string] (gp $key.InstallDir)
$dir+="PrivateAssemblies\"
$lib = $dir+"Microsoft.TeamFoundation.WorkItemTracking.Client.dll"
[Reflection.Assembly]::LoadFrom($lib)
$lib = $dir+"Microsoft.TeamFoundation.Client.dll"
[Reflection.Assembly]::LoadFrom($lib)
[Console]::WriteLine("Please enter your Team Foundation Server Name:")
$server = [Console]::ReadLine()
$server = $server.Trim()
"Connecting to " + $server + "..."
$tfs =
[Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($server)
WorkItemStore store =
(WorkItemStore)tfs.GetService(typeof(WorkItemStore));
I have so far been unsuccessful in passing the type object to the
GetService() method. How do I perform the equivalent of typeof() in
PowerShell? Any guidance would be greatly appreciated.
++Alan
Bruce Payette [MSFT]
2006-05-10 18:45:50 UTC
Permalink
Assuming that the full type name is Microsoft.TeamFoundation.WorkItemStore
the following should do the trick:

$store = $tfs.GetService([Microsoft.TeamFoundation.WorkItemStore])

There is no separate typeof operator in PowerShell. The name of a type in
square brackets is a type literal. Since type objects are just objects, you
could rewrite the above this way:

$type = [Microsoft.TeamFoundation.WorkItemStore]
$store = $tfs.GetService($type)

and finally, if you need to get a type object dynamically...

$root = "Microsoft.TeamFoundation" # base namespace of type
$type = "$root.WorkItemStore" -as [type] # expand the string and cast the
resulting string into a type object
$store = $tfs.GetService($type) # now get the service...

-bruce
--
Bruce Payette [MSFT]
Windows PowerShell Technical Lead
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.
Post by Alan Stevens
I am attempting to use PowerShell to make batch updates to workitems in team
foundation server. I have converted a C# program for the task, and have
$key = gp HKLM:\SOFTWARE\Microsoft\VisualStudio\8.0
$dir = [string] (gp $key.InstallDir)
$dir+="PrivateAssemblies\"
$lib = $dir+"Microsoft.TeamFoundation.WorkItemTracking.Client.dll"
[Reflection.Assembly]::LoadFrom($lib)
$lib = $dir+"Microsoft.TeamFoundation.Client.dll"
[Reflection.Assembly]::LoadFrom($lib)
[Console]::WriteLine("Please enter your Team Foundation Server Name:")
$server = [Console]::ReadLine()
$server = $server.Trim()
"Connecting to " + $server + "..."
$tfs =
[Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($server)
WorkItemStore store =
(WorkItemStore)tfs.GetService(typeof(WorkItemStore));
I have so far been unsuccessful in passing the type object to the
GetService() method. How do I perform the equivalent of typeof() in
PowerShell? Any guidance would be greatly appreciated.
++Alan
Alan Stevens
2006-05-10 20:39:02 UTC
Permalink
Thanks for the helpful responses. I have definately learned more about type
objects in PowerShell. With your help, I was able to determine that the
namespace had changed for the type I needed since the C# example was
published in the SDK.

My solution looks like this:

$type = [Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore]

$store = [Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore]
$tfs.GetService($type)

++Alan
Post by Bruce Payette [MSFT]
Assuming that the full type name is Microsoft.TeamFoundation.WorkItemStore
$store = $tfs.GetService([Microsoft.TeamFoundation.WorkItemStore])
There is no separate typeof operator in PowerShell. The name of a type in
square brackets is a type literal. Since type objects are just objects, you
$type = [Microsoft.TeamFoundation.WorkItemStore]
$store = $tfs.GetService($type)
and finally, if you need to get a type object dynamically...
$root = "Microsoft.TeamFoundation" # base namespace of type
$type = "$root.WorkItemStore" -as [type] # expand the string and cast the
resulting string into a type object
$store = $tfs.GetService($type) # now get the service...
-bruce
--
Bruce Payette [MSFT]
Windows PowerShell Technical Lead
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.
Post by Alan Stevens
I am attempting to use PowerShell to make batch updates to workitems in team
foundation server. I have converted a C# program for the task, and have
$key = gp HKLM:\SOFTWARE\Microsoft\VisualStudio\8.0
$dir = [string] (gp $key.InstallDir)
$dir+="PrivateAssemblies\"
$lib = $dir+"Microsoft.TeamFoundation.WorkItemTracking.Client.dll"
[Reflection.Assembly]::LoadFrom($lib)
$lib = $dir+"Microsoft.TeamFoundation.Client.dll"
[Reflection.Assembly]::LoadFrom($lib)
[Console]::WriteLine("Please enter your Team Foundation Server Name:")
$server = [Console]::ReadLine()
$server = $server.Trim()
"Connecting to " + $server + "..."
$tfs =
[Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($server)
WorkItemStore store =
(WorkItemStore)tfs.GetService(typeof(WorkItemStore));
I have so far been unsuccessful in passing the type object to the
GetService() method. How do I perform the equivalent of typeof() in
PowerShell? Any guidance would be greatly appreciated.
++Alan
Loading...