0

I have following script to create an basic IIS site which is working

New-Website -Name "TestApp1" -Port 80 -IPAddress "192.168.1.222" -HostHeader "testapp1.example.com" -PhysicalPath C:\Powershell\TestCreateFolder1 -ApplicationPool "DefaultAppPool"

But I dont have this script working

$site_command = "-Name ""TestApp1"" -Port 80 -IPAddress ""192.168.1.222"" -HostHeader ""testapp1.example.com"" -PhysicalPath C:\Powershell\TestCreateFolder1 -ApplicationPool ""DefaultAppPool"""

New-WebSite $site_command

I am getting this error

-Name "TestApp1" -Port 80 -IPAddress "192.168.1.222" -H
ershell\TestCreateFolder1 -ApplicationPool "DefaultAppP
New-Website : Invalid site name
At C:\powershell\CreateIisSite.ps1:35 char:12
+ New-WebSite <<<<  $site_command
    + CategoryInfo          : InvalidData: (:) [New-Web
    + FullyQualifiedErrorId : Invalid site name
,Microsoft.IIs.PowerShell.Provider.NewWebsiteCommand

1 Answer 1

2

The PowerShell command New-WebSite $site_command is interpreting the entire $site_command as the name of the website. If you want to use your New-Website command as a variable, then try this instead:

$site_command = "New-Website -Name ""TestApp1"" -Port 80 -IPAddress ""192.168.1.222"" -HostHeader ""testapp1.example.com"" -PhysicalPath ""C:\Powershell\TestCreateFolder1"" -ApplicationPool ""DefaultAppPool"""
Invoke-Expression $site_command

Note that the New-Website command is in the variable, instead of starting at -Name in your example.

2
  • Is there any other possible way of just keep $site_command in a different variable.
    – Praneeth
    May 24, 2016 at 16:39
  • Yes, you could do this instead: Invoke-Expression "New-Website $site_command"
    – Matt
    May 24, 2016 at 16:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.