Page 2 of 2
Stupid Question? Cant execute method.
Posted: Thu Jul 17, 2008 4:01 am
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.
Stupid Question? Cant execute method.
Posted: Thu Jul 17, 2008 4:13 am
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.
Stupid Question? Cant execute method.
Posted: Thu Jul 17, 2008 4:56 am
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.