[英]How to Wrap Powershell around IIS AppCmd
我正在尝试将 Powershell 包裹在 AppCmd 周围以执行一些安全合规性检查。 我决定这样做而不是使用 powershell 的 Get-WebConfiguration 命令,因为对于所有这些检查,安全策略已经提供了相应的 AppCmd 命令。 So, rather than spending too much time trying to workout the equivalent Get-WebConfiguration commands, I have decided to write a function that takes the provided AppCmd commands and arguments in form of variables, runs them in powershell and potentially passes the results to another function .
我在将变量值传递给 AppCmd 时遇到了很多问题。 以下代码有效:
$appCmd = "C:\Windows\system32\inetsrv\appcmd.exe"
& $appCmd list config /section:system.web/authentication /text:forms.requireSSL
到目前为止,一切都很好。 现在,以下代码会导致错误:
$appCmd = "C:\Windows\system32\inetsrv\appcmd.exe"
$appcmd_args = "list config /section:system.web/authentication /text:forms.requireSSL"
& $appCmd $appcmd_args
错误内容如下:
Object 'LIST CONFIG /SECTION:SYSTEM.WEB/AUTHENTICATION /TEXT:FORMS.REQUIRESSL' is not supported. Run 'appcmd.exe /?' to display supported objects.
我读过一篇建议在将变量传递给 AppCmd 时使用${}
的帖子。 所以,尝试了这个:
$appCmd = "C:\Windows\system32\inetsrv\appcmd.exe"
$appcmd_args = "list config /section:system.web/authentication /text:forms.requireSSL"
& $appCmd ${appcmd_args}
我可能做错了,所以我得到了与上面相同的错误。 我还注意到以下代码出现了同样的错误:
$appCmd = "C:\Windows\system32\inetsrv\appcmd.exe"
& $appCmd "list config /section:system.web/authentication /text:forms.requireSSL"
也许需要进行某种类型的转换或修整?
所有 AppCmd 命令和 arguments 都将通过变量提供,因此,如果这种技术不起作用,我的计划就会落空。 我显然错过了一些东西。 您能就解决方案提出建议吗?
由于appcmd.exe
期望 arguments 以空格分隔,因此您不能将其全部作为一个字符串发送。 我会采用其中一种方法。
用逗号分隔每个参数,然后将它们分解
$appcmd_args = "list", "config", "/section:system.web/authentication", "/text:forms.requireSSL"
& $appCmd $appcmd_args
或者您可以像这样拆分 arguments 内联
$appcmd_args = "list config /section:system.web/authentication /text:forms.requireSSL"
& $appCmd (-split $appcmd_args)
经过数小时的反复试验,我发现这行得通。
$appCmd = "C:\Windows\system32\inetsrv\appcmd.exe"
$appcmd_args = "list config /section:system.web/authentication /text:forms.requireSSL"
$AppCmd_Command = [string]::Format("{0} {1}", $appCmd, $appcmd_args)
iex $AppCmd_Command
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.