我正在尝试将 Powershell 包裹在 AppCmd 周围以执行一些安全合规性检查。我决定这样做而不是使用 powershell 的 Get-WebConfiguration 命令,因为对于所有这些检查,安全策略已经提供了相应的 AppCmd 命令。因此,与其花太多时间尝试锻炼等效的 Get-WebConfiguration 命令,我决定编写一个函数,以变量形式获取提供的 AppCmd 命令和参数,在 powershell 中运行它们,并可能将结果传递给另一个函数.
我在将变量值传递给 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 命令和参数都将通过变量提供,因此,如果这种技术不起作用,我的计划就会落空。我显然错过了一些东西。您能就解决方案提出建议吗?