parameterized function

by powerpotion81 at 2013-01-27 18:27:50

Ok, I am a newbie at powershell and I have been reading "Learn powershell in a month of lunches." I was doing good until I got to the scripting portion probably because I have no background in scripting. Anyway, I am having trouble with the parameterized function method, in particular in chapter 19, where I am supposed to be able to put in any computername and it will display those results for that particular machine. Here is the script I am using:

function mycomputerinfo {
param (
$computername = ‘localhost’
$os=gwmi win32_operatingsystem -ComputerName $computername
$bios=gwmi win32_bios -comp $computername
$domain=gwmi win32_computersystem -comp $computername
$obj= New-Object -TypeName psobject
$obj|Add-Member -MemberType noteproperty <br> -name computername -value $computername<br>$obj|Add-Member -MemberType noteproperty
-name VERSION -value ($os.caption)
$obj|Add-Member -MemberType noteproperty <br> -name BIOS -value ($bios.serialnumber)<br>$obj|Add-Member -MemberType noteproperty
-name DOMAIN -value ($domain.domain)
$obj|Add-Member -MemberType noteproperty `
-name HOSTNAME -value($domain.dnshostname)
Write-Output $obj

When I go to run the script i do my .\filename -computername any computername and all I keep getting is localhost for computername. I know what is happening, the $computername variable is keeping the localhost name, so why isn’t it changing like it said it would in the book? This is really frustrating me and help would be great thanks.
by nohandle at 2013-01-28 02:50:35
function mycomputerinfo {
param (
$computername = 'localhost'
mycomputerinfo -computername 'someComputerName'

Works for me if I run it as function.

If you save the script with the function in it, and then you call the script with parameter the parameter is not sent to the function call. Therefore the computername parameter returns the default value. you need to pass the script parameter values to the function parameter values.
if you call the script like this .\filename.ps1 -computername 'csc0001'
You need to call the function within the script like this: mycomputerinfo -computername $Computername
by powerpotion81 at 2013-01-28 05:20:27
aaah, that makes sense, thanks for the response, works now.