0

I'm trying to run a Bash script which calls appcmd to add a site to IIS.

#!/bin/bash

windir=c:\\windows
domain="$1"
path="$2"

#also tried using forward slashes by replacing backslashes
#physicalPath=`echo "$path" | sed 's/\\\\/\//g'`

#add site
$windir\\syswow64\\inetsrv\\appcmd add site /name:$domain /physicalpath:$path

I'm calling the script using:

script.sh mydomain.com c:\mypath

However when I check IIS, the Physical Path property of the site is set using forward slashes instead of backslashes.

appcmd Bash

What am I doing wrong?

Ropstah
  • 17,538
  • 24
  • 120
  • 194
  • Try changing your sed pattern maybe `sed 's!\/!\\!g'` – l'L'l May 15 '16 at 12:01
  • This produces an error: `sed: -e expression #1, char 8: unterminated `s' command` – Ropstah May 15 '16 at 12:07
  • @l'L'l: this produces the same error. But my `sed` part doesn't actually produce an error, it I `echo $physicalPath` the values are replaced. It's just that in `IIS` it will come up with forward slashes which it doesn't understand – Ropstah May 15 '16 at 12:15
  • @l'L'l: still the `c:\ mypath` appears in IIS as `c:/ mypath` – Ropstah May 15 '16 at 12:55
  • 1
    I did notice a few things, and I think you're close; in many examples there's a `-` before the physicalPath... so maybe `-PhysicalPath:"C:\ $path"`, or `-PhysicalPath:"$env:systemdrive\"$path`, or `/[path='/'].physicalPath:%SystemDrive%\$path` ... – l'L'l May 15 '16 at 13:29
  • 1
    This actually does the trick! Thanks! - So very strangely (mixed `/` and `-` delimiters) I now have this as a working command: `$windir\\syswow64\\inetsrv\\appcmd add site /name:$domain -physicalpath:$physicalPath /bindings:http/*:80:$domain` – Ropstah May 15 '16 at 20:40

2 Answers2

1

does the following data from https://technet.microsoft.com/en-us/library/cc725781%28v=ws.10%29.aspx helps?

To change the path of an application's content, use the following syntax:

appcmd set app /app.name: string /[path='/'].physicalPath: string

The variable app.namestring is the virtual path of the application, and physicalPathstring is the physical path of the application's content.

For example, to change the physical path of the location c:\application for an application named marketing in a site named contoso, type the following at the command prompt, and then press ENTER:

appcmd set app /app.name: contoso / marketing /[path='/'].physicalPath:c:\ application

For more information about Appcmd.exe, see Appcmd.exe (IIS 7).

Yaron
  • 10,166
  • 9
  • 45
  • 65
  • The OP is using variables though, and not hardcoded strings. – l'L'l May 15 '16 at 12:42
  • I tried the following command: `$windir\\syswow64\\inetsrv\\appcmd set app /app.name:$domain/ /"[path='/'].physicalPath:$physicalPath"` (note: `domain$/` is appname and also note double quotes around `/"[path=...."`. Still the physical path in IIS is with forward slashes... – Ropstah May 15 '16 at 12:51
1

Backslashes are used to prevent special treatment of certain characters:

$ z=foo
$ echo "$z"
foo
$ echo "\$z"
$z

Because of this, you need escape backslashes themselves in order to use them literally. Each pair \\ is seen by the shell as a single literal \.

windir=c:\\\\windows
domain="$1"
path="$2"

#add site
"$windir"\\\\syswow64\\\\inetsrv\\\\appcmd add site /name:"$domain" /physicalpath:"$path"

However, a simpler way to escape them is to include them in single quotes.

windir='c:\\windows'
domain="$1"
path="$2"

#add site
"$windir"'\\syswow64\\inetsrv\\appcmd' add site /name:"$domain" /physicalpath:"$path"

(You can use double quotes as well, but since there are a few characters that have special meaning in double quotes, a backslash can be used to escape them, which means sometimes you need to escape a backslash, sometimes you don't. For example:

$ echo "\$"
$
$ echo "\t"
\t

)

chepner
  • 497,756
  • 71
  • 530
  • 681