1

I have the following Powershell script which uses WMI to update the HTTP custom headers on a website in IIS 6.

The script seems to work fine, no errors generated, and the resultant $website object correctly shows the updated HttpCustomHeaders value. When I look at that site in IIS, though, the values have not been updated. This is also verified by visiting the site and looking at the headers in Firebug - the values are not saved.

Any ideas what I'm doing wrong?

$website = Get-WmiObject -Class IIsWebServerSetting -Namespace "root\microsoftiisv2" -ComputerName $computerName -filter "ServerComment = '$websiteName'" -Authentication 6

$headers = $webSite.HttpCustomHeaders
$headers[0].Keyname = 'P3P: policyref="http://somewebsite.com/p3p.xml", CP="IDC DSP COR CUR DEV PSA IVA IVD CONo HIS TELo OUR DEL UNRo BUS UNI"'
$headers[0].Value = $null
$website.HttpCustomHeaders = $headers
$website.Put()

I'm more than open to an alternative script that provides a better way of setting this value as above assumes there is already at least one header present.

I've also tried Set-WmiInstance -InputObject $website instead of $website.Put() but that made no difference.

Duffman
  • 123
  • 1
  • 6

2 Answers2

2

Your code example saves the value to the metabase but it saves to the root object (w3svc/1) instead of the root vdir (w3svc/1/root).

Here's what will work in place of what you have:

$website = Get-WmiObject -Class IIsWebServerSetting -Namespace "root\microsoftiisv2" -ComputerName $computerName -filter "ServerComment = '$websiteName'" -Authentication 6

$path = $website.name + '/root'
$vdir = [wmi]"root\MicrosoftIISv2:IIsWebVirtualDirSetting='$path'"

$headers = $vdir.HttpCustomHeaders
$headers[0].Keyname = 'P3P: policyref="http://somewebsite.com/p3p.xml", CP="IDC DSP COR CUR DEV PSA IVA IVD CONo HIS TELo OUR DEL UNRo BUS UNI"'
$headers[0].Value = $null
$vdir.HttpCustomHeaders = $headers
$vdir.Put()

However, one more thing to watch for is that that replaces the first value rather than adding a new value. I'm out of time today to figure out how to do with with my limited PowerShell expertise, but it's going to be something along the lines of:

$bindingClass = [wmiclass]'root\MicrosoftIISv2:HttpCustomHeader'
$newheader = $bindingClass.CreateInstance()
$newheader.KeyName = "New Value"
$newheader.Value = $null

$headers += $header

There's a casting error so it's not quite right, but hopefully that points you in the right direction.

Scott Forsyth
  • 16,339
  • 3
  • 36
  • 55
  • Brilliant - thanks Scott that works. Adding headers doesn't work as you've specified - let me know if you come up with something, I'll keep working on it too. – Duffman May 05 '11 at 10:01
0

Try outputting the current value first while you're testing. Then you can know that you properly bound to the site that you expected. For example, for the line after $headers=, do a Write-Host $headers[0].Keyname. Or do a Write-Host $webSite.ServerComment.

Basically, narrow it down to make sure that you have a handle on the correct website.

Also, I don't now PowerShell as well as I should at this point, but can you edit [0] and have it 'add' an entry? I bet that you need to do something else to actually add a value to an array.

Scott Forsyth
  • 16,339
  • 3
  • 36
  • 55
  • Hi Scott - I've outputted values and ensured I'm on the correct site. Also verified output values after the fact and they state it has changed, however it hasn't. Looked all over for ways of adding HttpCustomHeaders but haven't found a way with ADSI or WMI in PowerShell. – Duffman May 04 '11 at 19:17
  • I found the answer after working on it for a while. I'll reply as a new answer so I can paste the code example. – Scott Forsyth May 04 '11 at 22:19