Iis – How to verify that a custom IIS log field exists with PowerShell

iispowershellwindows-server-2016

I am deploying a pair of scripts via SCCM to set a wide variety of IIS configuration items – things like default log locations, truncate values, and so on. One of the things I add is a custom log file field:

Add-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' -filter "system.applicationHost/sites/$Sitename/logFile/customFields" -name "." -value @{logFieldName='Original-IP';sourceName='X-Forwarded-For';sourceType='RequestHeader'}

This works fine; I can manually browse to it with the Configuration Editor in IIS, and it shows in the logging settings GUI. However, I have a problem. Part of the SCCM deployment is to run a script that verifies the values are correct on each server before it runs the correction script. This will be run periodically against our environment (3000+ Windows servers), and the results of that verify script determine whether SCCM runs the remediation script that sets the values.

I want to avoid running the script when its not necessary (and my boss loves things that say 100%), but I can't figure the error I'm receiving. I know I'm polling the value wrong, so I can't tell it what to match. Can anyone help me figure this out?

$SiteLogFileCustom = Get-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' -filter "system.applicationHost/sites/$Sitename/logFile/customFields" -name Original-IP

if (($SiteLogFileCustom) -eq ('Original-IP'))
    {
        write-host "Match!"
    }
else
    {
        write-Error "Mismatched values!" -Category NotInstalled -ErrorId MisMatch

Returns:

\\tsclient\D\share\scripts\IIS-Settings\Check-iisSettings.ps1 : Mismatched values!
+ CategoryInfo          : NotInstalled: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : MisMatch,Check-iisSettings.ps1

Which is expected, as the value returns blank. How do I poll the collection customFields to get a list of the names of the custom fields, then match them against a pre-determined list?

PS – There should only be one field, Original-IP.

Best Answer

from sharkbite0141 via /r/PowerShell sent 19 hours ago:

It's a combination of issues with your Get-WebConfigurationProperty Filter and Name parameters, as well as an issue with your if statement:

$SiteLogFileCustom = Get-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' -filter "system.applicationHost/sites/site[@name='$Sitename']/logFile/customFields" -Name 'Collection'

if ($SiteLogFileCustom.logFieldName -match 'Original-IP')
{write-output "Match!"}
   else
{write-Error "Mismatched values!" -Category NotInstalled -ErrorId MisMatch}

To explain:

First in the Filter property, there was some weirdness with site name querying. The site[@name='$Sitename'] parameter does a better job of matching against the site name. Second, the -Name parameter needed to be "Collection" since the name of the configuration property you're looking for in the customFields configuration is a collection set of custom properties. Now, according to documentation a single period "." should have worked as a wildcard on a configuration that has a Collection property, but for whatever reason, it only worked for me when I specifically named the "Collection" property.

Then, in your if statement, you needed to also specify the property that you're matching a value against. In this instance, the Original-IP value is stored in the logFieldName property. So we tacked that onto the end of the $SiteLogFileCustom and voila!

And just a little side-note thing: Unless you need things like color formatting, it's better to use Write-Output than Write-Host as Write-Output writes out to the pipeline, meaning you can pipe it to another cmdlet or store it in a variable, whereas Write-Host outputs to the visual console. ​

Edit: formatting and an additional note