简体   繁体   中英

Powershell Script to create IIS

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM