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
}