Clarification on computer name parameter

This topic contains 3 replies, has 3 voices, and was last updated by  Lery 4 years, 2 months ago.

  • Author
    Posts
  • #7976

    Lery
    Participant

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

    
    

    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?

  • #7979

    Don Jones
    Keymaster

    You gotta avoid pasting out of "rich" apps, which includes the ISE I think. 🙂 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.

  • #7980

    Richard Siddaway
    Moderator

    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

  • #7982

    Lery
    Participant

    [quote=7979]You gotta avoid pasting out of “rich” apps, which includes the ISE I think. :) 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.[/quote]

    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:

    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. [/quote]

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

You must be logged in to reply to this topic.