Clarification on computer name parameter

I just want to make sure that I understand this correctly and am not missing anything.

<span style=“font-size: 13px;”>

</span>

param(

[string]$computername = ‘localhost’

)

$a = get-WmiObject -Class win32_operatingsystem -ComputerName $computername

$b = Get-WmiObject -Class win32_computersystem -ComputerName $computername

Obviously the above code is not complete. Â Once complete I could run the script and pass in the computername parameter and have it run against remote computers. Â Is it correct that I can only pass the computername parameter into this script because the commands the script utilizes depend on the -computername parameter?

For example, this would not work:

param(

[string]$computername = 'localhost'

)

$a = set-itemproperty -path c:\GroupFiles\final.doc -name IsReadOnly -value $true  -ComputerName $computername

$b = New-Item c:\hello -ComputerName $computername

If I ran the above ./script1.ps1 -computername win8 , it would not work because the commands in the script do not accept the -computername parameter. Â Is that right?

You gotta avoid pasting out of “rich” apps, which includes the ISE I think. :slight_smile: They end up pasting HTML. That’s where your extra SPAN tag came from in your post. LMK what you pasted from - I’d like to post a clearer warning about that for other folks. You can also paste into the TEXT tab of the editor, which makes it a lot clearer what’s happening, instead of the VISUAL editor tab.

So, you may have things a bit mixed up. I’m not sure, so I’ll just cover all the bits.

 

First, YOUR parameter could be $fred. It doesn’t NEED to be $ComputerName. You only use $ComputerName because that’s consistent with the way the shell works. I just want to make sure you’re clear on that. This is also legal:

param([string]$purple)

get-wmiobject -class win32_bios -computername $purple

There doesn’t need to be a “match.”

 

Also, your code isn’t quite set up well to work against multiple computers - it will, but it’ll be harder to capture errors.

param([string[]]$computername)

foreach ($bozo in $computername)

$a = Get-WmiObject Win32_OperatingSystem -ComputerName $bozo

}

Would be a better pattern.

 

You cannot just tack on a -ComputerName parameter to a command - like Set-ItemProperty - which does not natively support that parameter. You’re correct on that. Just because YOUR script has a -ComputerName parameter doesn’t confer the ability to any command contained within the script.

Just as a slight digression I’d add that using ‘localhost’ as a default value can sometime cause problems. I’d recommend $env:COMPUTERNAME which picks the name of the local machine out of the environmental variables. Same effect but a bit more robust

Yes it was the ISE. Â Good to know for future.

[quote=7979]

So, you may have things a bit mixed up. I’m not sure, so I’ll just cover all the bits. First, YOUR parameter could be $fred. It doesn’t NEED to be $ComputerName. You only use $ComputerName because that’s consistent with the way the shell works. I just want to make sure you’re clear on that. This is also legal:
<pre class=“d4pbbc-pre”>param([string]$purple)

get-wmiobject -class win32_bios -computername $purple
There doesn’t need to be a “match.” Also, your code isn’t quite set up well to work against multiple computers – it will, but it’ll be harder to capture errors.
<pre class=“d4pbbc-pre”>param([string]$computername)

foreach ($bozo in $computername)

$a = Get-WmiObject Win32_OperatingSystem -ComputerName $bozo

}
Would be a better pattern. You cannot just tack on a -ComputerName parameter to a command – like Set-ItemProperty – which does not natively support that parameter. You’re correct on that. Just because YOUR script has a -ComputerName parameter doesn’t confer the ability to any command contained within the script. [/quote]

Got it thanks Don. Â This clarifies things for me nicely.