none
Is my foreach loop superfluous? - Powershell

    Question

  • Hello,

    I've written a bunch of functions that accept multiple computernames.  I always put in a foreach ($computer in $Computername) in order to deal with the possibility of multiple computernames.  is this unnecessary or is it useful?

    Function Trust-Site
    {
    <#
    .SYNOPSIS
    Trust-Site adds a registry to the a machine trusting a internet site.
    .DESCRIPTION
    Trusted site will add a site to the trusted site list using the registry.
    .PARAMETER ComputerName
    Enter Computer Name here.
    .PARAMETER Site
    Enter Site to be trusted here.
    .PARAMETER Type
    Enter Http Or Https. 
    .EXAMPLE
    Trust-site -computername DIG123456 -site fakesite.abc -type http.
    .EXAMPLE
    Trust-site -computername DIG123456 -site fakesite.abc -type https.
    #>
    [CmdletBinding()]
    Param( 
    [Parameter(
    Mandatory=$true,
    ValueFromPipeline=$True,
    HelpMessage= 'Enter the target computer name')][String[]]$ComputerName,
    [Parameter(
    Mandatory=$true,
    HelpMessage= 'Please enter the name of the site to be trusted'
    )][String]$Site,
    [Parameter(
    Mandatory=$true,
    HelpMessage= 'Please put in either HTTP or HTTPS'
    )][String]$Type  
    )
    Begin
        {
        if ($type -notlike 'http' -or 'https') {Throw 'Value for type was not correct, please enter http or https'}
        }
    Process
        {
        Foreach ($computer in $ComputerName)
            {
            if (-not($computer -eq "$env:COMPUTERNAME"))
                {
                Invoke-Command -ComputerName $computer -scriptblock {
                    New-Item "hklm:\SOFTWARE\Wow6432Node\Policies\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains\$Site"
                    New-ItemProperty "hklm:\SOFTWARE\Wow6432Node\Policies\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains\$Site" -name $type -value 2 -PropertyType DWORD
                    }
                }
            Else
                {
                New-Item "hklm:\SOFTWARE\Wow6432Node\Policies\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains\$Site"
                New-ItemProperty "hklm:\SOFTWARE\Wow6432Node\Policies\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains\$Site" -name $type -value 2 -PropertyType DWORD
                }
            }    
        }
    End{}
    } 

    Also if anyone knows a better way to handle identifying if I'm targetting the local computer I'd be grateful for the tip.  Thanks in advance!

    Monday, March 11, 2013 11:50 PM

Answers

  • Not necessary if your using a 'Process' block to iterate through the objects in the pipeline...

    Function Test{ [CmdletBinding()] Param( [Parameter( Mandatory=$true, ValueFromPipeline=$True, HelpMessage= 'Enter the target computer name')][String[]]$ComputerName) Process { write-host $ComputerName } #No ForEach required!

    } 'riffy1','riffy2' | Test


    Would write to the screen each of the pipelined objects:

    riffy1

    riffy2

    Have a look at Get-Help about_functions advanced.

    if (-not($computer -eq "$env:COMPUTERNAME"))

    ...looks ideal to me.


    Inspired by Carlsberg.


    Tuesday, March 12, 2013 12:14 AM

All replies

  • Not necessary if your using a 'Process' block to iterate through the objects in the pipeline...

    Function Test{ [CmdletBinding()] Param( [Parameter( Mandatory=$true, ValueFromPipeline=$True, HelpMessage= 'Enter the target computer name')][String[]]$ComputerName) Process { write-host $ComputerName } #No ForEach required!

    } 'riffy1','riffy2' | Test


    Would write to the screen each of the pipelined objects:

    riffy1

    riffy2

    Have a look at Get-Help about_functions advanced.

    if (-not($computer -eq "$env:COMPUTERNAME"))

    ...looks ideal to me.


    Inspired by Carlsberg.


    Tuesday, March 12, 2013 12:14 AM
  • Sweet thanks, I'm trying to cut out inefficiencies in my code, that works really well!
    Tuesday, March 12, 2013 1:03 AM
  • If you use the declared array then you must enumerate the computername array in some way.   If you do not intend to allow an array then remove the array notation to prevent errors.


    ¯\_(ツ)_/¯

    Tuesday, March 12, 2013 1:08 AM