Parameter Question


hi all,

quick question on i've come across recently. in example below( , in other similar ones), 'computername' parameter has been set being fed '$computername.' after testing, noticed script work if use '$computername,' '$computer,' , '$computers' values parameter, nothing else. assuming latter 2 work because hold values(although, not sure how or why $computers works, since foreach enumerating it); why $computername works, not sure. i'm guessing there built in functionality when declare parameter, when use name value, represents whatever current value gets placed there?

i'm not being confusing :) code below:

$computers = 'testbox1' , 'testbox2' , 'testbox3'

function rebootwork {
param([string]$computername)
foreach($computer in $computers) {
get-wmiobject win32_operatingsystem -computer $computername |
invoke-wmimethod -name reboot | out-null
}
}
rebootwork -computername $computers

perhaps changing variable names will make less confusing

$computers = 'testbox1' , 'testbox2' , 'testbox3' function rebootwork { param([array]$sausage) foreach($watermellon in $sausage) { $watermellon

#get-wmiobject win32_operatingsystem -computer $watermellon | #invoke-wmimethod -name reboot | out-null } $computers } rebootwork -sausage $computers

within function:

$sausage contains array of computers defined in $computers

in foreeach loop, each individual element of $sausage assigned $watermellon

get-wmiobject win32_operatingsystem -computer $computername

 in new powershell session, $computername return nothing, because it's not defined anywhere... i'm guessing in environment $computername defined somewhere, script perhaps? profile?

in case of example, use:

get-wmiobject win32_operatingsystem -computer $watermellon

the result:

testbox1 testbox2 testbox3 testbox1 testbox2 testbox3 

you computer names once each foreach loop

then again, because $computers returns $computers array defined @ beginning of script.

i hope clears little?


admiral ackbar says...



Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

Error: 0x80073701 when trying to add Print Services Role in Windows 2012 Standard

Disconnecting from a Windows Server 2012 R2 file sharing session on a Windows 7,8,10 machine

Event ID 64,77,1008 Certificates Events Windows Server 2008, 2008R2