header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

Attempting to add parameter option to script
Last Post 29 Sep 2008 08:10 AM by amargeson. 2 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
amargesonUser is Offline
New Member
New Member
Posts:4
Avatar

--
28 Sep 2008 08:59 AM  

Hello everyone!  This is my first time posting on this forum, and I'm trying to enhance a script I've been working on.  The script searches an array of computers for all services that are using a specific account, useful for checking before you change a service account's password or disable it.

Here's where the script now stands...

param(
    [string[]]$account=@(),
    [string[]]$computers=@(),
    [switch]$help
)

$account=@($Input)
$computers = @($input)

Get-WmiObject win32_service -ComputerName $computers | where-object {$_.startname -eq "$account"} | sort-object systemname | format-table systemname,displayname,startname | out-host

if($help){
@"
SYNTAX:
get-servicesbylogon -account "domain\user" -computers "computer1,computer2"

I just added the param options into the script, but they don't work.  When I try using the script with the parameters, I get no returns when I know I should.  Thoughts as to why?

Thanks in advance for any help!

halr9000User is Offline
PowerShell MVP, Site Admin
Basic Member
Basic Member
Posts:335
Avatar

--
28 Sep 2008 02:59 PM  
Strike these lines:

$account=@($Input)
$computers = @($input)

$Input is only used for accepting pipelined objects into a script (and it's not even my favorite way--I rarely use it). What you did here was zero out your parameters.

If your goal was to accept BOTH params and pipeline, then its a noble goal, but one that's pretty convoluted in V1. PowerShell V2 will be addressing this with the ability of script cmdlets to bind to the pipeline as a cmdlet does.

If that was your intent, I suggest for the sake of simplicity to drop it and just use one input method or the other. I'm sure others may point you to some existing techniques which can help you do both but that's not my style.

So just use the parameters by variable name and it'll work fine--provided the cmdlet or function you are passing them to will accept or understand arrays of values. For example, $computers will work fine because get-wmiobject's computer param will work with an array just fine.

However, this:

$_.startname -eq "$account"

Will not because the left side will never be an array (much less with equal items as the right side). What you want to do is:

$account -contains $_.startname

You may have to rework your logic to suit.
Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast
Author, TechProsaic
amargesonUser is Offline
New Member
New Member
Posts:4
Avatar

--
29 Sep 2008 08:10 AM  

I feel stupid.  I thought $input was needed for parameters to be typed in as you invoke the script...

LOL!

Thanks!  That clears things up for me.  I think the most useful point of this script is to streamline checking multiple computers, so this works fine by typing the computers in with the parameter, or setting an array to a variable, and setting the parameter using the variable you created.

Much appreciated.  This was driving me nuts!

You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer