我更新了我的脚本,并解决了之前关于第二个条件的问题。当我调用我的函数时,没有触发。但现在我发现了新的问题:1。当我如何在不改变我的功能模板的情况下将用户输入置于功能之外时;函数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
因为您在函数内部请求用户输入,所以不需要任何函数参数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