Home > OS >  How do i make IIS Website Site Name check
How do i make IIS Website Site Name check

Time:03-31

I updated my script and solved my previous issues about my second conditional did not triggered when i called my function. But now i found new problems: 1. when how do i put user input outside the function without changing my function template " function createIISWebsite($siteName,$sitePath,$siteUrl) "so, the function will be run when automatically without me manually calling it.

 function createIISWebsite($siteName,$sitePath,$siteUrl) {

    #Asking for User input
    $siteName = Read-Host "siteName: "
    $sitePath = Read-Host "sitePath: "
    $siteUrl = Read-Host "siteUrl: "
    
    try {
        
        # Check if site name already exists and update site path
        if ((Get-Website $siteName) -and (Test-Path $sitePath) )

            { Set-Location $sitePath  
              Write-Host "$siteName " -ForegroundColor Red -NoNewline; 
              Write-Host "already exist, updated the site path to "-NoNewline; 
              Write-Host "$sitePath "-ForegroundColor Yellow -NoNewline;  
              Write-Host "and you can access it via "-NoNewline;
              Write-Host "$siteUrl"-ForegroundColor Green -NoNewline;}
              
        # if not exists create new website * can specify port and protocol "http or https"
        # using out-null for cleaner output
        else 
            { $sitePort = Read-Host "Input Port: "
              $siteProtocolType = Read-Host "Input Protocol Type http or https: "
              New-Website -Name $siteName  -HostHeader $siteUrl -PhysicalPath $sitePath | Out-Null
              New-WebBinding -Name $siteName -Port $sitePort -Protocol $siteProtocolType
              Write-Host $siteName "successfully created, you can access it via" $siteUrl -ForegroundColor Green }
        }
    catch
            {Write-host "Error: $($_.Exception.Message)"}
            
return

}  

CodePudding user response:

To test if a sitename already exists, use Get-IISSite.
Because you are asking for user input inside the function, there is no need for any of the function parameters.
return at the end of the function is also not needed.

Try:

function createIISWebsite {

    #Asking for User input
    $siteName = Read-Host "siteName: "
    $sitePath = Read-Host "sitePath: "
    $siteUrl  = Read-Host "siteUrl: "
    
    $existingSite = Get-IISSite -Name $siteName -ErrorAction SilentlyContinue
    # check If the site name already exists, update the existing using new input parameter
    if ($existingSite) {
        Write-Host "$siteName already exist, updated the site path to $sitePath and you can access it via $siteUrl"
    }
    #if not exist create new website using port 80 and shows successfully created new site
    else { 
        try {
            $null = New-Website -Name $siteName -Port 80 -HostHeader $siteUrl -PhysicalPath $sitePath -ErrorAction Stop
            Write-Host "$siteName successfully created, you can access it via $siteUrl"
        }
        catch {
            Write-host "Error: $($_.Exception.Message)"
        }
    }
}

If you do want to use parameters, then just do the Read-Host lines outside of the function like

function createIISWebsite {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true, Position = 0)]
        [string]$siteName,

        [Parameter(Mandatory = $true, Position = 1)]
        [string]$sitePath,

        [Parameter(Mandatory = $true, Position = 2)]
        [string]$siteUrl
    )
    $existingSite = Get-IISSite -Name $siteName -ErrorAction SilentlyContinue
    # check If the site name already exists, update the existing using new input parameter
    if ($existingSite) {
        try {
            # update the site physical path to whatever is in variable $sitePath
            # I don't know your configuration, but the path for Set-ItemProperty in IIS usually starts with "IIS:\Sites\"
            Set-ItemProperty "IIS:\Sites\$siteName" -Name physicalPath -Value $sitePath -ErrorAction Stop
            Write-Host "$siteName already exist, updated the site path to $sitePath and you can access it via $siteUrl"
        }
        catch {
            Write-Host "Error updating the site's physical path:`r`n$($_.Exception.Message)" -ForegroundColor Red
        }
    }
    # if not exist create new website using port 80 and shows successfully created new site
    else { 
        try {
            $null = New-Website -Name $siteName -Port 80 -HostHeader $siteUrl -PhysicalPath $sitePath -ErrorAction Stop
            Write-Host "$siteName successfully created, you can access it via $siteUrl"
        }
        catch {
            Write-Host "Error creating new website:`r`n$($_.Exception.Message)"
        }
    }
}

#Asking for User input
$siteName = Read-Host "siteName: "
$sitePath = Read-Host "sitePath: "
$siteUrl  = Read-Host "siteUrl: "

# now call the function with parameters NAMED
createIISWebsite -siteName $siteName -sitePath $sitePath -siteUrl $siteUrl

# or without the parameter names, just by position:
# createIISWebsite $siteName $sitePath $siteUrl
  • Related