Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if configuration section is locked IIS 7

Tags:

iis

batch-file

The command:

appcmd lock config -section:...

Will lock the specified config section. But I need to check if the config section is locked or not.

Is there any possible way to do that? Thanks for any reply!

like image 660
Tran Ngu Dang Avatar asked Oct 22 '25 21:10

Tran Ngu Dang


1 Answers

I know this is a few years old now but I was searching for this for Puppet so here's what I came up with.

The command Get-WebConfigurationLock shows if any locks exist on the config section.

E.g. to check the IIS global config section for system.webServer/security/ipSecurity:

Get-WebConfigurationLock -PSPath "IIS:\" -Filter //system.webServer/security/ipSecurity

If the section is unlocked the above command will return null. e.g. to "unlock if locked":

if (Get-WebConfigurationLock -PSPath "IIS:\" -Filter //system.webServer/security/ipSecurity)  {
    %windir%/System32/inetsrv/appcmd.exe unlock config -section:system.webServer/security/ipSecurity
} else {
    Write-Host "Already unlocked"
}

Remove-WebConfigurationLock can be used instead of appcmd, I favoured appcmd because you can pass lock/unlock as a parameter rather than changing the command itself.

Not sure what Chef looks like though here's the puppet define I've created to unlock/lock sections for reference: https://gist.github.com/krutisfood/d59b3a5c62f6123bf553

For reference I found this Stack Overflow link Programmatically unlocking IIS configuration sections in Powershell though found that Get-WebConfigurationLock more accurately matched the locked state, even reloading the dll or the powershell window getting the attribute value often show IsLocked false when it was locked. ymmv.

like image 193
krutisfood Avatar answered Oct 26 '25 07:10

krutisfood



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!