by Cloud81918 at 2012-11-09 15:57:35
New to PShell. I looked at a couple of places and this seem to be a good place to set roots.by DonJ at 2012-11-09 16:03:33
I’m wanting to use Powershell to make some admin tasks easier. One of those tasks is restarting some Com+ objects that commonly need restarted. I made the following function.
$comObj = New-Object -comobject COMAdmin.COMAdminCatalog
I call it with.
The string $servername is valid when sent to the function. When it goes to the function it writes out normally and looks fine, but it will not work. If I set the string in the function to my test machine, it works. I have compared the string the function receives and one set in the function with the built in string compare function and it tells me they are not equal. Something gets messed up sending the string to function.
The test machine I’m using is called “prod-int2”. I’m not sure but perhaps the “-” is causing the issue? How would I pass it in a way that wouldn’t?
Thanks in advance for any ideas.
You’re calling it wrong. Functions in Powershell behave like commands, not like functions in programming languages.by DonJ at 2012-11-09 16:04:14
ShutdownComObject -RemoteServer $servername -COMPlusAppName $ComPlusAppName
Your use of parentheses and commas is creating a two-element array which is being passed to the first parameter, -RemoteServer, positionally.
Oh, and Stop-ComObject would be a more PowerShell-compliant name for the function.
BTW, totally weird that this exact question came up twice today viewtopic.php?f=2&t=710 <grin>by Cloud81918 at 2012-11-09 16:29:30
Wow, thanks for the lighting response. I’ll try that out. I copied the function call as I saw in some guides, but but they may have been old. I’ll start using the style shown.by DonJ at 2012-11-09 16:36:10
Sorry if the question ended up being a duplicate. I looked for a bit to see if anyone had a similar issue. But I was assuming it was an issue with the machine name as it has a special charictor in it.
Nah, no worries. It’s tough to come up with a search string for something like that.
This has been a big “gotcha” since v1 of PowerShell. It’s one of those cases where a little programming experience actually sets you back instead of helping.
What you may have seen - and just in case someone else runs across this and is wondering - is actually an alternate function declaration syntax.
Declares parameters -this and -that without using an explicit Param block - although internally, PowerShell creates the Param block anyway. One of PowerShell’s oddities is that a comma-separated list is always an array in any other context, which makes for neat ways of specifying lists:
Get-WmiObject -class Win32_BIOS -computername one,two,three
But it’s a deadly trip-up if you’ve been writing functions in pretty much any other language ever invented :).