Quantcast
Channel: PowerShell.org » All Posts
Viewing all articles
Browse latest Browse all 13067

Reply To: Script – PARAM – prompt for credentials?

$
0
0

I gave an example of that in the Process block of the code in my original reply. The only difference between that and what you’re doing now is the addition of the foreach ($computer in $ComputerName) loop (because you’ve changed $ComputerName from [string] to [string[]] ; a good change.)

    process
    {
        foreach ($computer in $ComputerName)
        {
            $params = @{
                ComputerName = $computer
                Class = 'Win32_OperatingSystem'
            }

            if ($Credential)
            {
                $params['Credential'] = $Credential
            }

            Get-WmiObject @params
        }
    }

I prefer to use splatting for this type of conditional parameter passing, as shown in the examples so far. The alternative would be to have a conditional and two different calls to Get-WmiObject:

    process
    {
        foreach ($computer in $ComputerName)
        {
            if ($Credential)
            {
                Get-WmiObject -ComputerName $computer -Class 'Win32_OperatingSystem' -Credential $Credential
            }
            else
            {
                Get-WmiObject -ComputerName $computer -Class 'Win32_OperatingSystem'
            }
        }
    }

Viewing all articles
Browse latest Browse all 13067

Trending Articles