I want to use appcmd with Invoke-Command in Powershell to add a virtual directory.
I am using reference from: 1) appcmd to create virtual directory 2) Powershell Invoke-Command
Here is the code snippet:
$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}
When I run the code above, I keep getting error saying:
Unexpected token 'name='Default Web Site'].[path='/'].[path='/MyWebsite/dev'' in expression or statement.
I am new to Powershell and I have been searching everywhere how to solve this problem.
If anyone could show me how to correct my Powershell snippet so I could create a virtual directory? Thank you
Looks like you may have a quoting issue combined with some extraneous characters. The {}
characters in the String.Format call aren't doing anything for you. The double quote here "appcmd.exe
starts a string that ends at /+"
which makes everything after it error. You can escape the double quote in the string with a backtick ` character.
[string]::Format("appcmd.exe set config -section:system.applicationHost/sites /+`"[name='Default Web Site'].[path='/'].[path='/MyWebsite/dev',physicalPath='{0}']`" /commit:apphost",$folderName)
Powershell also has the -f operator which does string formatting without having to call string.format but you still need to escape the quotes.
$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}