Answered by:
Is my foreach loop superfluous? - Powershell
-
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!
Question
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.
- Edited by RiffyRiotMicrosoft community contributor Tuesday, March 12, 2013 12:15 AM
- Marked as answer by R Jason Morgan Tuesday, March 12, 2013 1:03 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.
- Edited by RiffyRiotMicrosoft community contributor Tuesday, March 12, 2013 12:15 AM
- Marked as answer by R Jason Morgan Tuesday, March 12, 2013 1:03 AM
-
-

