IIS 7 and Above
Extract return code from IIS powershell command
Last post Feb 13, 2018 06:49 AM by matanlevy
Feb 12, 2018 07:02 AM|matanlevy|LINK
let's say I run this command :
Set-WebConfigurationProperty system.web/sessionState -pspath MACHINE/WEBROOT -name regenerateExpiredSessionID -value WrongValue
I will get the following error :
Set-WebConfigurationProperty : String was not recognized as a valid Boolean.
At line:1 char:1
+ Set-WebConfigurationProperty system.web/sessionState -pspath MACHINE/WEBROOT -na ...
+ CategoryInfo : NotSpecified: (:) [Set-WebConfigurationProperty], FormatException
+ FullyQualifiedErrorId : System.FormatException,Microsoft.IIs.PowerShell.Provider.SetConfigurationPropertyCommand
but $lastExitCode returns 0.
Is there any way to extract more informative return code to ensure that operation succeed/failed?
and if failed, to get the exact return code? (as appcmd with the same command return 13,which is ERROR_INVALID_DATA)
Feb 13, 2018 01:37 AM|Yuk Ding|LINK
The error message has displayed error that the data type of regenerateExpiredSessionID is Boolean, but you were trying to grant a int or string value to it. The value should be "true" or "false". So the powershell command should looks just like this:
Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/mysite' -filter "system.web/sessionState" -name "regenerateExpiredSessionId" -value "False"
If you need set it in global level, then you should try this
Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT' -filter "system.web/sessionState" -name "regenerateExpiredSessionId" -value "False"
Feb 13, 2018 06:49 AM|matanlevy|LINK
Thanks for you answer, but that was my purpose, to cause an error.
my problem is that the return value from $lastExitCode is 0 although the operation failed.
the return code with the same command from appcmd is 13 as I wrote,which is much more informative error code (ERROR_INVALID_DATA).
So just to make clear, my question is not how to fix the error, but how to get an informative error code from PowerShell command?