Stupid Question? Cant execute method.

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
jeremyg
Posts: 22
Joined: Sun Mar 23, 2008 2:04 pm

Stupid Question? Cant execute method.

Post by jeremyg »

I kind of figured the "b" was a typo, but I didnt want to doubt someone who obviously knows 10x more than I. hehe


very cool! I think i'm going to end up using this little trick many times!

Is there any particular reason you used the {} around the code? I tried it with "quotes" and seems to work equally well.

User avatar
jhicks
Posts: 1789
Joined: Sun Jan 21, 2007 11:31 pm

Stupid Question? Cant execute method.

Post by jhicks »

You need to use the {} so PowerShell knows you are using a script block. Otherwise you have a string:

Code: Select all

PS C:> $s={get-service wsearch}
PS C:> $s1="get-service wsearch"
PS C:> $s.getType()

IsPublic IsSerial Name                           BaseType
-------- -------- ----                           --------
True     False    ScriptBlock                    System.ObjectPS C:> $s1.getType()

IsPublic IsSerial Name                           BaseType
-------- -------- ----                           --------
True     True     String                         System.Object
PS C:> &$s

Status   Name               DisplayName
------   ----               -----------
Running  WSearch            Windows Search
PS C:> &$s1
The term 'get-service wsearch' is not recognized as a cmdlet, function,
rm and try again.
At line:1 char:2
+ &$ <<<< s1
It shouldn't work with quotes.

User avatar
jeremyg
Posts: 22
Joined: Sun Mar 23, 2008 2:04 pm

Stupid Question? Cant execute method.

Post by jeremyg »

Ok i see now. I took my own liberties and it "seemed" like it was working....but I was just doing $xyz = "get service" and then running &$xyz spooler

I understand now how that is not the same thing.

Locked