如何在Powershell Invoke-Command中使用appcmd来添加虚拟目录?

问题描述 投票:0回答:1

我想在Powershell中使用带有Invoke-Command的appcmd来添加虚拟目录。

我使用的参考来自:1)appcmd to create virtual directory 2)Powershell Invoke-Command

这是代码片段:

$appCmdCommand2 = [string]::Format(
{"appcmd.exe set config -section:system.applicationHost/sites /+"[name='Default Web Site'].[path='/'].[path='/MyWebsite/dev',physicalPath='{0}']" /commit:apphost"},$folderName)

Invoke-Command -ComputerName ComputerAA -ScriptBlock {$appCmdCommand2}

当我运行上面的代码时,我不断收到错误说:

Unexpected token 'name='Default Web Site'].[path='/'].[path='/MyWebsite/dev'' in expression or statement.

我是Powershell的新手,我一直在寻找如何解决这个问题。

如果有人能告诉我如何更正我的Powershell片段,那么我可以创建一个虚拟目录?谢谢

powershell virtual-directory invoke-command appcmd
1个回答
0
投票

看起来您可能会引用一些引用问题以及一些无关紧要的字符。 String.Format调用中的{}字符没有为您做任何事情。这里的双引号"appcmd.exe开始一个以/+"结尾的字符串,这使得一切都在它之后出错。你可以使用反引号`字符来转义字符串中的双引号。

[string]::Format("appcmd.exe set config -section:system.applicationHost/sites /+`"[name='Default Web Site'].[path='/'].[path='/MyWebsite/dev',physicalPath='{0}']`" /commit:apphost",$folderName)

Powershell还有-f运算符,它可以执行字符串格式化而无需调用string.format,但仍需要转义引号。

$appCmdCommand2 = "appcmd.exe set config -section:system.applicationHost/sites /+`"[name='Default Web Site'].[path='/'].[path='/MyWebsite/dev',physicalPath='{0}']`" /commit:apphost" -f $folderName

Invoke-Command -ComputerName ComputerAA -ScriptBlock {$appCmdCommand2}
© www.soinside.com 2019 - 2024. All rights reserved.