閱讀英文

共用方式為


在 Azure Functions 和 App Service 混合式連線中使用 PowerShell 管理混合式環境

Azure App Service 混合式連線功能可讓您存取其他網路中的資源。 您可以在混合式連線文件中深入了解此功能。 本文描述如何使用此功能來執行以內部部署伺服器為目標的 PowerShell 函式。 然後您便可以使用此伺服器,從 Azure PowerShell 函式管理內部部署環境中的所有資源。

設定用於 PowerShell 遠端處理的內部部署伺服器

下列指令碼會啟用 PowerShell 遠端處理,並建立新的防火牆規則和 WinRM https 接聽程式。 為了測試目的,會使用自我簽署憑證。 在生產環境中,建議您使用簽署的憑證。

# For configuration of WinRM, see
# https://learn.microsoft.com/windows/win32/winrm/installation-and-configuration-for-windows-remote-management.

# Enable PowerShell remoting.
Enable-PSRemoting -Force

# Create firewall rule for WinRM. The default HTTPS port is 5986.
New-NetFirewallRule -Name "WinRM HTTPS" `
                    -DisplayName "WinRM HTTPS" `
                    -Enabled True `
                    -Profile "Any" `
                    -Action "Allow" `
                    -Direction "Inbound" `
                    -LocalPort 5986 `
                    -Protocol "TCP"

# Create new self-signed-certificate to be used by WinRM.
$Thumbprint = (New-SelfSignedCertificate -DnsName $env:COMPUTERNAME  -CertStoreLocation Cert:\LocalMachine\My).Thumbprint

# Create WinRM HTTPS listener.
$Cmd = "winrm create winrm/config/Listener?Address=*+Transport=HTTPS @{Hostname=""$env:COMPUTERNAME ""; CertificateThumbprint=""$Thumbprint""}"
cmd.exe /C $Cmd

在入口網站中建立 PowerShell 函式應用程式

App Service 混合式連線功能僅適用於基本、標準和隔離式定價方案。 使用 PowerShell 建立函式應用程式時,請建立或選取其中一個方案。

  1. 從 Azure 入口網站功能表或 [首頁] 頁面,選取 [建立資源]

  2. 在 [新增] 頁面中,選取 [計算]>[函數應用程式]

  3. 在 [基本資訊] 頁面中,使用下表中指定的函式應用程式設定。

    設定 建議的值 描述
    訂用帳戶 您的訂用帳戶 將在其下建立這個新函式應用程式的訂用帳戶。
    資源群組 myResourceGroup 要在其中建立函式應用程式的新資源群組名稱。
    函數應用程式名稱 全域唯一的名稱 用以識別新函式應用程式的名稱。 有效的字元是 a-z (不區分大小寫)、0-9-
    發行 代碼 發佈程式碼檔案或 Docker 容器的選項。
    執行階段堆疊 慣用語言 選擇 [PowerShell Core]。
    版本 版本號碼 選擇已安裝的執行階段版本。
    區域 慣用區域 選擇與您接近的區域,或選擇與函式將會存取之其他服務接近的區域。

    建立函式應用程式 - 基本概念。

  4. 選取 [下一步:裝載]。 在 [裝載] 頁面中輸入下列設定。

    設定 建議的值 描述
    儲存體帳戶 全域唯一的名稱 建立您函式應用程式使用的儲存體帳戶。 儲存體帳戶名稱必須介於 3 到 24 個字元的長度,而且只能包含數字和小寫字母。 您也可以使用現有帳戶,條件是必須符合儲存體帳戶需求
    作業系統 慣用的作業系統 系統會根據您的執行階段堆疊選項預先選取作業系統,但您可以視需要變更設定。
    方案類型 App Service 方案 選擇 [App Service 方案]。 在 App Service 方案中執行時,您必須管理函式應用程式的調整

    建立函式應用程式 - 裝載。

  5. 選取 [下一步:監視]。 在 [掛接] 頁面中輸入下列設定。

    設定 建議的值 描述
    Application Insights 預設 在最近的支援區域中,建立相同應用程式名稱的 Application Insights 資源。 展開此設定或選取 [新增],即可變更 Application Insights 名稱或在 Azure 地理位置中依您希望儲存資料的地點,選擇不同的區域。

    建立函式應用程式 - 監視。

  6. 選取 [檢閱 + 建立],以檢閱應用程式組態選項。

  7. 在 [檢閱 + 建立] 頁面中檢閱您的設定,然後選取 [建立] 來佈建和部署函式應用程式。

  8. 選取入口網站右上角的 [通知] 圖示,查看是否有 [部署成功] 訊息。

  9. 選取 [前往資源],以檢視您新的函式應用程式。 您也可以選取 [釘選到儀表板]。 釘選可讓您更輕鬆地從儀表板返回此函式應用程式資源。

建立函式應用程式的混合式連線

混合式連線可從函式應用程式的網路區段設定:

  1. 在您剛才建立的函式應用程式中的 [設定] 下,選取 [網路]

  2. 選取 [設定您的混合式連線端點]

    設定混合式連線端點。

  3. 選取 [新增混合式連線]

    新增混合式連線。

  4. 輸入混合式連線的相關資訊,如下列螢幕擷取畫面後所示。 對於端點主機,請使用您為其建立自我簽署憑證的內部部署伺服器的主機名稱。 當憑證名稱與內部部署伺服器的主機名稱不相符時,您將遇到連線問題。 連接埠符合稍早在伺服器上定義的預設 Windows 遠端系統管理服務連接埠。

    新增混合式連線。

    設定 建議的值
    混合式連線名稱 ContosoHybridOnPremisesServer
    端點主機 finance1
    端點連接埠 5986
    服務匯流排命名空間 新建
    地點 挑選可用的位置
    名稱 contosopowershellhybrid
  5. 選取 [確定] 以建立混合式連線。

下載和安裝混合式連線

  1. 選取 [下載連線管理員],將 .msi 檔案儲存在本機電腦上。

    下載安裝程式。

  2. .msi 檔案從本機電腦複製到內部部署伺服器。

  3. 執行混合式連線管理員安裝程式,以在內部部署伺服器上安裝服務。

    安裝混合式連線。

  4. 從入口網站開啟混合式連線,然後將閘道連接字串複製到剪貼簿。

    複製混合式 連接字串。

  5. 在內部部署伺服器上開啟混合式連線管理員 UI。

    開啟混合式連線 UI。

  6. 選取 [手動輸入],然後貼上剪貼簿的連接字串。

    貼上混合式連線。

  7. 如果 PowerShell 未顯示為已連線,請將混合式連線管理員重新啟動。

    Restart-Service HybridConnectionManager
    

建立系統管理員帳戶密碼的應用程式設定

  1. 在函式應用程式的 [設定] 底下,選取 [組態]

  2. 選取 [+ 新增應用程式設定]

    設定系統管理員帳戶的密碼。

  3. 將設定命名為 ContosoUserPassword,然後輸入密碼。 選取 [確定]。

  4. 選取 [儲存] 以將密碼儲存在函式應用程式中。

建立函式 HTTP 觸發程序

  1. 在您的函式應用程式下,選取 Functions,然後選取 [+ 新增]

    建立新的 HTTP 觸發程式。

  2. 選取 HTTP 觸發程式範本、命名新函式,然後選取 [建立函式]。

    命名並建立新的 HTTP 觸發程式函式。

測試函式

  1. 在新函式中,選取 [程式碼 + 測試]。 以下列程式碼取代範本中的 PowerShell 程式碼:

    # Input bindings are passed in via param block.
    param($Request, $TriggerMetadata)
    
    # Write to the Azure Functions log stream.
    Write-Output "PowerShell HTTP trigger function processed a request."
    
    # Note that ContosoUserPassword is a function app setting, so I can access it as $env:ContosoUserPassword.
    $UserName = "ContosoUser"
    $securedPassword = ConvertTo-SecureString  $Env:ContosoUserPassword -AsPlainText -Force
    $Credential = [System.management.automation.pscredential]::new($UserName, $SecuredPassword)
    
    # This is the name of the hybrid connection Endpoint.
    $HybridEndpoint = "finance1"
    
    $Script = {
        Param(
            [Parameter(Mandatory=$True)]
            [String] $Service
        )
        Get-Service $Service
    }
    
    Write-Output "Scenario 1: Running command via Invoke-Command"
    Invoke-Command -ComputerName $HybridEndpoint `
                   -Credential $Credential `
                   -Port 5986 `
                   -UseSSL `
                   -ScriptBlock $Script `
                   -ArgumentList "*" `
                   -SessionOption (New-PSSessionOption -SkipCACheck)
    
  2. 選取 [儲存] 以儲存您的變更,然後選取 [測試>回合] 以測試函式。

  3. 選取 [ 記錄 ] 以檢閱記錄,並確認測試是否成功。

管理內部部署的其他系統

您可以使用連線的內部部署伺服器連線到本機環境中的其他伺服器和管理系統。 這可讓您使用您的 PowerShell 函式,從 Azure 管理資料中心作業。 下列指令碼會註冊在所提供的認證下執行的 PowerShell 組態工作階段。 這些認證必須是用於遠端伺服器上的系統管理員。 然後,您便可以使用此組態來存取本機伺服器或資料中心上的其他端點。

# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)

# Write to the Azure Functions log stream.
Write-Host "PowerShell HTTP trigger function processed a request."

# Note that ContosoUserPassword is a function app setting, so I can access it as $env:ContosoUserPassword.
$UserName = "ContosoUser"
$SecuredPassword = ConvertTo-SecureString  $Env:ContosoUserPassword -AsPlainText -Force
$Credential = [System.management.automation.pscredential]::new($UserName, $SecuredPassword)

# This is the name of the hybrid connection Endpoint.
$HybridEndpoint = "finance1"

# The remote server that will be connected to run remote PowerShell commands on
$RemoteServer = "finance2".

Write-Output "Use hybrid connection server as a jump box to connect to a remote machine"

# We are registering an endpoint that runs under credentials ($Credential) that has access to the remote server.
$SessionName = "HybridSession"
$ScriptCommand = {
    param (
        [Parameter(Mandatory=$True)]
        $SessionName)

    if (-not (Get-PSSessionConfiguration -Name $SessionName -ErrorAction SilentlyContinue))
    {
        Register-PSSessionConfiguration -Name $SessionName -RunAsCredential $Using:Credential
    }
}

Write-Output "Registering session on hybrid connection jumpbox"
Invoke-Command -ComputerName $HybridEndpoint `
               -Credential $Credential `
               -Port 5986 `
               -UseSSL `
               -ScriptBlock $ScriptCommand `
               -ArgumentList $SessionName `
               -SessionOption (New-PSSessionOption -SkipCACheck)

# Script to run on the jump box to run against the second machine.
$RemoteScriptCommand = {
    param (
        [Parameter(Mandatory=$True)]
        $ComputerName)
        # Write out the hostname of the hybrid connection server.
        hostname
        # Write out the hostname of the remote server.
        Invoke-Command -ComputerName $ComputerName -Credential $Using:Credential -ScriptBlock {hostname} `
                        -UseSSL -Port 5986 -SessionOption (New-PSSessionOption -SkipCACheck)
}

Write-Output "Running command against remote machine via jumpbox by connecting to the PowerShell configuration session"
Invoke-Command -ComputerName $HybridEndpoint `
               -Credential $Credential `
               -Port 5986 `
               -UseSSL `
               -ScriptBlock $RemoteScriptCommand `
               -ArgumentList $RemoteServer `
               -SessionOption (New-PSSessionOption -SkipCACheck) `
               -ConfigurationName $SessionName

以您環境的適當值取代此指令碼中的下列變數:

  • $HybridEndpoint
  • $RemoteServer

在上述兩個案例中,您可以在 Azure Functions 和混合式連線中使用 PowerShell 來連線和管理您的內部部署環境。 建議您深入了解混合式連線函式中的 PowerShell

您也可以使用 Azure 虛擬網路,以透過 Azure Functions 連線到內部部署環境。

下一步