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.

Sign up to request clarification or add additional context in comments.

2 Comments

Is there any other possible way of just keep $site_command in a different variable.
Yes, you could do this instead: Invoke-Expression "New-Website $site_command"

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.