Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - invoking .net classes in powershell

Reply
 
Old 01-03-2007   #1 (permalink)
Jim B


 
 

invoking .net classes in powershell

I am trying to figure out why these lines work:

PS C:\> $test = [system.net.webrequest]
PS C:\> $test2 = [system.net.webclient]

and this one generares an error:

PS C:\> $testftp = [System.Net.WebRequestMethods.Ftp]
Unable to find type [System.Net.WebRequestMethods.Ftp]: make sure that
the assembly containing this type is loaded.
At line:1 char:45
+ $testftp = [System.Net.WebRequestMethods.Ftp] <<<<


My System SpecsSystem Spec
Old 01-03-2007   #2 (permalink)
/\/\o\/\/ [MVP]


 
 

RE: invoking .net classes in powershell

$WC = NEW-OBJECT System.Net.WebClienT
$wc
$wc | gm


"Jim B" wrote:

> I am trying to figure out why these lines work:
>
> PS C:\> $test = [system.net.webrequest]
> PS C:\> $test2 = [system.net.webclient]
>
> and this one generares an error:
>
> PS C:\> $testftp = [System.Net.WebRequestMethods.Ftp]
> Unable to find type [System.Net.WebRequestMethods.Ftp]: make sure that
> the assembly containing this type is loaded.
> At line:1 char:45
> + $testftp = [System.Net.WebRequestMethods.Ftp] <<<<
>
>

My System SpecsSystem Spec
Old 01-03-2007   #3 (permalink)
Jacques Barathon [MS]


 
 

Re: invoking .net classes in powershell

"Jim B" <jim.butts@gmail.com> wrote in message
news:1167805768.483223.168000@s34g2000cwa.googlegroups.com...
>I am trying to figure out why these lines work:
>
> PS C:\> $test = [system.net.webrequest]
> PS C:\> $test2 = [system.net.webclient]
>
> and this one generares an error:
>
> PS C:\> $testftp = [System.Net.WebRequestMethods.Ftp]
> Unable to find type [System.Net.WebRequestMethods.Ftp]: make sure that
> the assembly containing this type is loaded.
> At line:1 char:45
> + $testftp = [System.Net.WebRequestMethods.Ftp] <<<<


"WebRequestMethods.Ftp" is derived from the class container
"WebRequestMethods", so the right syntax in PowerShell is
"WebRequestMethods+Ftp".

Hope that helps,
Jacques

My System SpecsSystem Spec
Old 01-04-2007   #4 (permalink)
Jim B


 
 

Re: invoking .net classes in powershell

This absolutely helps. Thank you. Is there someplace that the "+"
notation is documented? Was there some way for me to determine this on
my own?

On Jan 3, 11:27 am, "Jacques Barathon [MS]"
<jbara...@online.microsoft.com> wrote:
> "Jim B" <jim.bu...@gmail.com> wrote in messagenews:1167805768.483223.168000@s34g2000cwa.googlegroups.com...
>
> >I am trying to figure out why these lines work:

>
> > PS C:\> $test = [system.net.webrequest]
> > PS C:\> $test2 = [system.net.webclient]

>
> > and this one generares an error:

>
> > PS C:\> $testftp = [System.Net.WebRequestMethods.Ftp]
> > Unable to find type [System.Net.WebRequestMethods.Ftp]: make sure that
> > the assembly containing this type is loaded.
> > At line:1 char:45
> > + $testftp = [System.Net.WebRequestMethods.Ftp] <<<<"WebRequestMethods.Ftp" is derived from the class container

> "WebRequestMethods", so the right syntax in PowerShell is
> "WebRequestMethods+Ftp".
>
> Hope that helps,
> Jacques


My System SpecsSystem Spec
Old 01-05-2007   #5 (permalink)
Jacques Barathon [MS]


 
 

Re: invoking .net classes in powershell

"Jim B" <jim.butts@gmail.com> wrote in message
news:1167952249.416317.180550@v33g2000cwv.googlegroups.com...
> This absolutely helps. Thank you. Is there someplace that the "+"
> notation is documented? Was there some way for me to determine this on
> my own?


I am not sure about the documentation. As for determining it on your own, I
figured it out thanks to the tab expansion feature (I use MOW's version
called PowerTab so you may want to install it before reading further:
http://thepowershellguy.com/blogs/po.../default.aspx).

Now that you have installed PowerTab, you can explore .NET types from your
console:

PS> [system.net.web # press Tab
PS> [System.Net.WebRequest] # press Tab again
PS> [System.Net.WebRequestMethods] # here you can see that there is a
closing bracket, which means that there is no known subtype

From there, if you know that there is a sub-class called "FTP", there is a
high chance that the parent class is a generic container class and that you
have to use the "+" notation.

That's a completely empirical approach, probably not 100% reliable but it
has worked for me the rare times I was confronted to this issue.

Jacques

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Invoking a certain section of a powershell file. PowerShell
Right-button on folder invoking PowerShell PowerShell
Invoking PowerShell functions with parameters from .NET: issues. PowerShell
Invoking PowerShell from WScript via COM PowerShell
Invoking Powershell from C# PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46