'Powershell: Calling function inside Foreach doesnt work, but outside does
Sorry if this is already replied somewhere, tried looking but couldn't found anything related.
Im doing a Powershell parser for firewall rules and have this weird behavior: Created a function to check some service port for rules. There's a main ForEach that checks every rule (brought through API query and resulting in XML data) and when I try to execute the function inside the ForEach it wont do anything. If I place the function call outside the ForEach it will trigger.
Some code snipet:
'''
# -----------------------------
# Bucle to check each use case.
# -----------------------------
Write-Host (get-date -format 'HH:mm:ss'), "- [INFO] - Starting rule review."
# This will trigger
Test-ServiceFW -TestService_PortsFromRule $nul -TestService_PortToCheck "8843"
foreach ($rule in $firewallRuleParse)
{
# This will NOT trigger
Test-ServiceFW -TestService_PortsFromRule $null -TestService_PortToCheck "8843"
Write-Host (get-date -format 'HH:mm:ss'), "- [INFO] - ---------------------"
Write-Host (get-date -format 'HH:mm:ss'), "- [INFO] - Processing rule -> $($rule.name)"
'''
The function itself its at the top of the script:
# --------------------------------------------------------------------------------------------------
# Functions
# --------------------------------------------------------------------------------------------------
function Test-ServiceFW {
[CmdletBinding()]
param ($TestService_PortsFromRule,$TestService_PortToCheck)
Write-Output "dentro de funcion"
if ($TestService_PortsFromRule.count -gt 1)
{
Write-Output "dentro de funcion 2"
foreach ($TestService_ServicePort in $TestService_PortsFromRule)
{
Write-Output "dentro de funcion 2"
$ResultServiceValidation = Test-ServiceGeneral -GeneralService_PortsFromRule $MainService_PortsFromRule -GeneralService_PortToCheck $MainService_PortToCheck
return $ResultServiceValidation
}
} else {
Write-Output "dentro de funcion 3"
$ResultServiceValidation = Test-ServiceGeneral -GeneralService_PortsFromRule $MainService_PortsFromRule -GeneralService_PortToCheck $MainService_PortToCheck
return $ResultServiceValidation
}
}
If I try to run the function as mentioned before outside the ForEach, i will get the Write-Outputs knowing im in the function, but if I called inside the ForEach, it will do nothing.
Any ideas? If you need more data, just let me know.
Thanks (sorry for the bad english, not my main language)
Additional info:
$firewallRuleParse its an API Request answer from the firewall in this fashion (xml object):
<response status="success" code="19">
<result total-count="1" count="1">
<rules>
<entry name="VPN Access" uuid="xxxxxxxx-xxxx-xxxxxxxxx-348763b88547">
<to>
<member>any</member>
</to>
<from>
<member>Internet</member>
</from>
<source>
<member>any</member>
</source>
<destination>
<member>any</member>
</destination>
<source-user>
<member>any</member>
</source-user>
<category>
<member>any</member>
</category>
<application>
<member>panos-global-protect</member>
<member>panos-web-interface</member>
<member>ssl</member>
</application>
<service>
<member>application-default</member>
</service>
<action>allow</action>
</entry>
$rule is basically every entry for firewall rule ( element)
Solution 1:[1]
After playing with the script, not sure why but had some variables with css and html code on them, like:
$html_footer = @{'BLA BLA bla'}
But very big. After moving those to the bottom (after the foreach), the function started working. No idea why but I'll take it.
Thanks for the concern and help!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ouflak |
