Question

first and foremost I am NOT a Powershell dev so I'm a little over my head here, if this was BASH it would have been done by now....

I'm getting several errors that I don't really understand the what or why of,

Errors

I believe that maybe $vars cannot be interpreted in the way that I am trying to use? idk... If someone WITH powershell knowledge could lend an assist and educate this poor linux guy, I would be very grateful...

I would expect this to register and load a new (empty) site with IIS based of the input params provided.

Here's my PS1

# --------------------------------------------------------------------
# Define the variables.
# --------------------------------------------------------------------
Param (
    [string]$InetSiteName = $( Read-Host "Site Name" ),
    [int]$InetSitePort    = $( Read-Host "Site Port" ),
    [string]$InetPhysPath = $( $env:SystemDrive + "\inetpub\wwwroot" )
)


Import-Module "WebAdministration"

# --------------------------------------------------------------------
# Check for empty.
# --------------------------------------------------------------------
if(-not($InetSiteName)) { Throw "Site name cannot be empty..." }
if(-not($InetSitePort)) { Throw "Site port cannot be empty..." }
if(-not($InetPhysPath)) { Throw "Path name cannot be empty..." }

# --------------------------------------------------------------------
# Configure and register.
# --------------------------------------------------------------------
New-Item IIS:\Sites\$InetSiteName -physicalPath $InetPhysPath -bindings @{ protocol="http";bindingInformation=":"+$InetSitePort+":"+$InetSiteName } 
Set-ItemProperty IIS:\Sites\$InetSiteName -name applicationPool -value BenAPI
Start-WebSite $InetSiteName

# --------------------------------------------------------------------
# Run.
# --------------------------------------------------------------------
$webclient = New-Object Net.WebClient 
$webclient.DownloadString("http://localhost:$InetSitePort/");

$ie = New-Object -com InternetExplorer.Application 
$ie.Visible = $true 
$ie.Navigate("http://localhost:$InetSitePort/");
Was it helpful?

Solution 3

Looks like the real answer was that I didn't have a real pool created, here's my updated code...

Thanks Everyone

# --------------------------------------------------------------------
# Checking Execution Policy
# --------------------------------------------------------------------
#$Policy = "Unrestricted"
$Policy = "RemoteSigned"
If ((get-ExecutionPolicy) -ne $Policy) {
  Write-Host "Script Execution is disabled. Enabling it now"
  Set-ExecutionPolicy $Policy -Force
  Write-Host "Please Re-Run this script in a new powershell enviroment"
  Exit
}

# --------------------------------------------------------------------
# Define the variables.
# --------------------------------------------------------------------
[string]$InetSiteName = $( Read-Host "Site Name" )
[int]$InetSitePort    = $( Read-Host "Site Port" )
[string]$InetPhysPath = $( $env:SystemDrive + "\inetpub" )

$PoolName = "BenAPI"

# --------------------------------------------------------------------
# Loading IIS Modules
# --------------------------------------------------------------------
Import-Module "WebAdministration"

# --------------------------------------------------------------------
# Test or Create App Pool
# --------------------------------------------------------------------
if (!(Test-Path "IIS:\AppPools\$PoolName" -pathType container))
{
    # Create pool
    $appPool = New-Item "IIS:\AppPools\$PoolName"
    $appPool | Set-ItemProperty -Name "managedRuntimeVersion" -Value "v4.0"
}

# --------------------------------------------------------------------
# Configure and register.
# --------------------------------------------------------------------
$WebRoot = New-Item "$InetPhysPath\$InetSiteName" -type Directory
New-Item IIS:\Sites\$InetSiteName -physicalPath $WebRoot -bindings @{ protocol="http";bindingInformation="*:"+$InetSitePort+":" }

Set-ItemProperty IIS:\Sites\$InetSiteName -name applicationPool -value BenAPI
Set-Content "$WebRoot\default.htm" "Test Page: $InetSiteName"

Start-WebSite $InetSiteName

# --------------------------------------------------------------------
# Run.
# --------------------------------------------------------------------
$ie = New-Object -com InternetExplorer.Application
$ie.Visible = $true
$ie.Navigate("http://localhost:$InetSitePort/");

OTHER TIPS

This is because "WebAdministration" module is not getting loaded.
Run Powershell as Administrator so that "WebAdministration" module gets imported successfully.

enter image description here

Then Go to location where you have saved the script to run the script

PS C:\> cd 'C:\Users\jDaniel\Documents'
PS C:\Users\jDaniel\Documents> .\iis-site.ps1

There is a step to step guide Here - how to create Web-Sites, Web Applications, Virtual Directories and Application Pools in powershell.

This is how I create a new site:

New-WebSite -Name $InetSiteName -port $InetSitePort -id 555 -PhysicalPath "$InetPhysPath" -HostHeader $InetSiteName

if you need more binding you can use:

New-WebBinding -Name $InetSiteName -Port $InetSitePort2 -HostHeader $InetSiteName2
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top