简体   繁体   English

Powershell中阵列的Appcmd语法

[英]Appcmd Syntax for Arrays in Powershell

I'm trying to run the following to set the handler mappings for each of our websites using appcmd in powershell: 我正在尝试运行以下命令以在Powershell中使用appcmd为我们的每个网站设置处理程序映射:

$websites = Get-ChildItem IIS:\Sites
foreach ($Site in $WebSites) {
C:\Windows\system32\inetsrv\appcmd set config "$site" /section:handlers -accessPolicy:"Read,Script,Execute"}

However I'm getting this error message for each site as it iterates through them: 但是,我在遍历它们的每个站点都收到此错误消息:

ERROR ( message:Cannot find SITE object with identifier "Microsoft.IIs.PowerShell.Framework.ConfigurationElement". )

What am I doing wrong? 我究竟做错了什么? I tried using a property of the $sites variable: 我尝试使用$ sites变量的属性:

$site.name

but even that doesn't work. 但即使那样也不行。 I'm at a loss. 我很茫然。 Thanks! 谢谢!

As you've encountered, "$site" doesn't expand to the name of the website, but to the type of object that $site refers to. 如您所见, "$site"不会扩展为网站的名称,而是扩展为$site引用的对象的类型 "$site.name" is not way off, but in fact: "$site.name"并不是"$site.name" ,但实际上:

"$site"      -eq "Microsoft.IIs.PowerShell.Framework.ConfigurationElement"
"$site.name" -eq "Microsoft.IIs.PowerShell.Framework.ConfigurationElement.name"

The PowerShell parser stops recognizing the variable name at . PowerShell解析器停止识别位于的变量名称. , and treats the rest ( ".name" ) as a string. ,并将其余的( ".name" )视为字符串。

You can use the $() sub-expression operator to escape an entire statement: 您可以使用$()子表达式运算符对整个语句进行转义:

"Name: $($site.name)"

You can do anything you like inside $() and nest them all you like: 您可以在$()内做任何您想做的事情,然后将它们全部嵌套:

"Random Site Name: $("$(Get-Random -Maximum ([int32]::MaxValue)){0}" -f $site.name)"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM