我如何检查 IIS 网站站点名称

How do i make IIS Website Site Name check

我更新了我的脚本并解决了我之前关于我的第二个条件在我调用我的函数时没有触发的问题。但是现在我发现了新的问题: 1. 什么时候我如何在不改变我的函数模板的情况下将用户输入放在函数之外 " function createIISWebsite($siteName,$sitePath,$siteUrl) " 所以,函数将是 运行 当自动无需我手动调用它。

 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

}  

要测试站点名称是否已存在,请使用 Get-IISSite
因为您要求用户输入 inside 函数,所以不需要任何函数参数。
函数末尾的 return 也不需要。

尝试:

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)"
        }
    }
}

如果您确实想使用参数,那么只需在函数 外部 执行 Read-Host 行,例如

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