我更新了我的腳本并解決了我之前關于我的第二個條件在我呼叫我的函式時沒有觸發的問題。但是現在我發現了新的問題: 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
}
uj5u.com熱心網友回復:
要測驗站點名稱是否已存在,請使用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
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/454136.html
標籤:电源外壳
