none
Odd error with foreach loop - Missing statement body in foreach loop.

    Question

  • I have an array

    $array_ServerManager_WebOptions = @(
    	<#"Web-Server",							#[ ] Web Server (IIS)                  
    	"Web-WebServer",					#  [ ] Web Server                   
    	"Web-Common-Http",				#    [ ] Common HTTP Features            
    	"Web-Static-Content",			#      [ ] Static Content             
    	"Web-Default-Doc",				#      [ ] Default Document            
    	"Web-Dir-Browsing",				#      [ ] Directory Browsing           
    	"Web-Http-Errors",				#	      [ ] HTTP Errors               
    	"Web-Http-Redirect",			#      [ ] HTTP Redirection            
    	"Web-DAV-Publishing",			#      [ ] WebDAV Publishing            
    	"Web-App-Dev",						#    [ ] Application Development           
    	"Web-Asp-Net",						#      [ ] ASP.NET                 
    	"Web-Net-Ext",						#      [ ] .NET Extensibility           
    	"Web-ASP",								#      [ ] ASP                   
    	"Web-CGI",								#      [ ] CGI                   
    	"Web-ISAPI-Ext",					#      [ ] ISAPI Extensions            
    	"Web-ISAPI-Filter",				#      [ ] ISAPI Filters              
    	"Web-Includes",						#      [ ] Server Side Includes          
    	"Web-Health",							#    [ ] Health and Diagnostics           
    	"Web-Http-Logging",				#      [ ] HTTP Logging              
    	"Web-Log-Libraries",			#      [ ] Logging Tools              
    	"Web-Request-Monitor",		#      [ ] Request Monitor             
    	"Web-Http-Tracing",				#      [ ] Tracing                 
    	"Web-Custom-Logging",			#      [ ] Custom Logging             
    	"Web-ODBC-Logging",				#      [ ] ODBC Logging              
    	"Web-Security",						#    [ ] Security                  
    	"Web-Basic-Auth",					#      [ ] Basic Authentication          
    	"Web-Windows-Auth",				#      [ ] Windows Authentication         
    	"Web-Digest-Auth",				#      [ ] Digest Authentication          
    	"Web-Client-Auth",				#      [ ] Client Certificate Mapping Authentic... 
    	"Web-Cert-Auth",					#      [ ] IIS Client Certificate Mapping Authe... 
    	"Web-Url-Auth",						#      [ ] URL Authorization            
    	"Web-Filtering",					#      [ ] Request Filtering            
    	"Web-IP-Security",				#      [ ] IP and Domain Restrictions       
    	"Web-Performance",				#    [ ] Performance                 
    	"Web-Stat-Compression",		#      [ ] Static Content Compression       
    	"Web-Dyn-Compression",		#      [ ] Dynamic Content Compression       
    	"Web-Mgmt-Tools",					#  [ ] Management Tools                
    	"Web-Mgmt-Console",				#    [ ] IIS Management Console           
    	"Web-Scripting-Tools",		#    [ ] IIS Management Scripts and Tools      
    	"Web-Mgmt-Service",				#    [ ] Management Service             
    	"Web-Mgmt-Compat",				#    [ ] IIS 6 Management Compatibility       
    	"Web-Metabase",						#      [ ] IIS 6 Metabase Compatibility      
    	"Web-WMI",								#      [ ] IIS 6 WMI Compatibility         
    	"Web-Lgcy-Scripting",			#      [ ] IIS 6 Scripting Tools          
    	"Web-Lgcy-Mgmt-Console",	#      [ ] IIS 6 Management Console        
    	"Web-Ftp-Server",					#  [ ] FTP Server                   
    	"Web-Ftp-Service",				#    [ ] FTP Service                 
    	"Web-Ftp-Ext",						#    [ ] FTP Extensibility              
    	"Web-WHC",							#  [ ] IIS Hostable Web Core 
    	#>
    	$null											# $null to terminate array but leave comma pattern
    );
    

    that I enumerate to log (u.wl is just an alias):

    # Log $array_ServerManager_WebOptions
    foreach($option in $array_ServerManager_WebOption)
    {
    	"`$array_ServerManager_WebOptions: $option" | u.wl -Path C:\test\test.txt;
    }
    " " | u.wl -Path C:\test\test.txt;
    

    When I execute I get this in the $Error object:

    Missing statement body in foreach loop.
    Missing closing ')' in subexpression.
    
    I know that my logging function is clean.  When I run it by itself ("test" | u.wl -Path C:\test\test.txt) the $Error object remains empty.  Why would a foreach loop generate this error but a regular statement does not?  I get the same thing when I iterate through hash tables.

    Friday, July 29, 2011 5:00 AM

Answers

  • That leading < in the first array element in your example is causing the array to be null
    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    • Marked as answer by Will Steele Friday, July 29, 2011 7:16 PM
    Friday, July 29, 2011 1:04 PM

All replies

  • Hi Will,

    I think it will do what you want if you remove the comment from the array elements and add an "s" in front of the ")" :)

    Cheers

      Klaus

    Friday, July 29, 2011 9:02 AM
  • That leading < in the first array element in your example is causing the array to be null
    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    • Marked as answer by Will Steele Friday, July 29, 2011 7:16 PM
    Friday, July 29, 2011 1:04 PM
  • Yeah, I was trying to neutralize the array for testing purposes.  Didn't think it would have quite that effect.
    Friday, July 29, 2011 7:17 PM