使用 PowerShell 管理 Windows Admin Center 設定

如果您有包含多個 Windows Admin Center 伺服器的大型組織,您可以使用 PowerShell,一次在多部伺服器上設定連線和擴充功能清單。

使用 PowerShell 匯入或匯出您的連線 (包含標記)

# Load the module
Import-Module "$env:ProgramFiles\windows admin center\PowerShell\Modules\ConnectionTools"
# Available cmdlets: Export-Connection, Import-Connection

# Export connections (including tags) to a .csv file
Export-Connection "https://wac.contoso.com" -fileName "WAC-connections.csv"
# Import connections (including tags) from a .csv file
Import-Connection "https://wac.contoso.com" -fileName "WAC-connections.csv"
# Import connections (including tags) from .csv files, and remove any connections that are not explicitly in the imported file using the -prune switch parameter 
Import-Connection "https://wac.contoso.com" -fileName "WAC-connections.csv" -prune

用於匯入連線的 CSV 檔案格式

CSV 檔案的格式會以此四個標題開頭:"name","type","tags","groupId",然後接著新一行上的每個連線。

名稱是連線的 FQDN

類型是連線類型。 針對 Windows Admin Center 隨附的預設連線,您將使用下列其中一項:

Connection type Connection string
Windows Server msft.sme.connection-type.server
容錯移轉叢集 msft.sme.connection-type.cluster

標記會以管線分隔。

groupId 會用於共用連線。 使用此資料行中的 global 值,使此項目成為共用連線。

注意

修改共用連線僅限於閘道系統管理員可進行 - 任何使用者都可以使用 PowerShell 來修改其個人連線清單。

用於匯入連線的 CSV 檔案範例

"name","type","tags","groupId"
"myServer.contoso.com","msft.sme.connection-type.server","hyperv"
"myDesktop.contoso.com","msft.sme.connection-type.windows-server","hyperv"
"teamcluster.contoso.com","msft.sme.connection-type.cluster","legacyCluster|WS2016","global"
"myHCIcluster.contoso.com,"msft.sme.connection-type.cluster","myHCIcluster|hyperv|JIT|WS2019"
"teamclusterNode.contoso.com","msft.sme.connection-type.server","legacyCluster|WS2016","global"
"myHCIclusterNode.contoso.com","msft.sme.connection-type.server","myHCIcluster|hyperv|JIT|WS2019"

注意

CSV 檔案會分大小寫。

匯入 RDCman 連線

使用下列指令碼,將 RDCman 中已儲存的連線匯出至檔案。 接著,您可以將檔案匯入 Windows Admin Center,使用標記來維護 RDCMan 群組階層。 試試看!

  1. 將下列程式碼複製並貼到您的 PowerShell 工作階段:

    #Helper function for RdgToWacCsv
    function AddServers {
     param (
     [Parameter(Mandatory = $true)]
     [Xml.XmlLinkedNode]
     $node,
     [Parameter()]
     [String[]]
     $tags,
     [Parameter(Mandatory = $true)]
     [String]
     $csvPath
     )
     if ($node.LocalName -eq 'server') {
         $serverName = $node.properties.name
         $tagString = $tags -join "|"
         Add-Content -Path $csvPath -Value ('"'+ $serverName + '","msft.sme.connection-type.server","'+ $tagString +'"')
     } 
     elseif ($node.LocalName -eq 'group' -or $node.LocalName -eq 'file') {
         $groupName = $node.properties.name
         $tags+=$groupName
         $currNode = $node.properties.NextSibling
         while ($currNode) {
             AddServers -node $currNode -tags $tags -csvPath $csvPath
             $currNode = $currNode.NextSibling
         }
     } 
     else {
         # Node type isn't relevant to tagging or adding connections in WAC
     }
     return
    }
    
    <#
    .SYNOPSIS
    Convert an .rdg file from Remote Desktop Connection Manager into a .csv that can be imported into Windows Admin Center, maintaining groups via server tags. This will not modify the existing .rdg file and will create a new .csv file
    
     .DESCRIPTION
     This converts an .rdg file into a .csv that can be imported into Windows Admin Center.
    
     .PARAMETER RDGfilepath
     The path of the .rdg file to be converted. This file will not be modified, only read.
    
     .PARAMETER CSVdirectory
     Optional. The directory you wish to export the new .csv file. If not provided, the new file is created in the same directory as the .rdg file.
    
     .EXAMPLE
     C:\PS> RdgToWacCsv -RDGfilepath "rdcmangroup.rdg"
     #>
    function RdgToWacCsv {
     param(
         [Parameter(Mandatory = $true)]
         [String]
         $RDGfilepath,
         [Parameter(Mandatory = $false)]
         [String]
         $CSVdirectory
     )
     [xml]$RDGfile = Get-Content -Path $RDGfilepath
     $node = $RDGfile.RDCMan.file
     if (!$CSVdirectory){
         $csvPath = [System.IO.Path]::GetDirectoryName($RDGfilepath) + [System.IO.Path]::GetFileNameWithoutExtension($RDGfilepath) + "_WAC.csv"
     } else {
         $csvPath = $CSVdirectory + [System.IO.Path]::GetFileNameWithoutExtension($RDGfilepath) + "_WAC.csv"
     }
     New-item -Path $csvPath
     Add-Content -Path $csvPath -Value '"name","type","tags"'
     AddServers -node $node -csvPath $csvPath
     Write-Host "Converted $RDGfilepath `nOutput: $csvPath"
    }
    
  2. 若要建立 .CSV 檔案,請執行下列命令:

    RdgToWacCsv -RDGfilepath "path\to\myRDCManfile.rdg"
    
  3. 將所產生的 .CSV 檔案匯入 Windows Admin Center,而您所有的 RDCMan 群組階層都會透過標記呈現在連線清單中。 如需詳細資訊,請參閱使用 PowerShell 匯入或匯出您的連線 (包含標記)

使用 PowerShell 管理 Windows Admin Center 擴充功能

# Add the module to the current session
Import-Module "$env:ProgramFiles\windows admin center\PowerShell\Modules\ExtensionTools"
# Available cmdlets: Get-Feed, Add-Feed, Remove-Feed, Get-Extension, Install-Extension, Uninstall-Extension, Update-Extension

# List feeds
Get-Feed "https://wac.contoso.com"

# Add a new extension feed
Add-Feed -GatewayEndpoint "https://wac.contoso.com" -Feed "\\WAC\our-private-extensions"

# Remove an extension feed
Remove-Feed -GatewayEndpoint "https://wac.contoso.com" -Feed "\\WAC\our-private-extensions"

# List all extensions
Get-Extension "https://wac.contoso.com"

# Install an extension (locate the latest version from all feeds and install it)
Install-Extension -GatewayEndpoint "https://wac.contoso.com" "msft.sme.containers"

# Install an extension (latest version from a specific feed, if the feed is not present, it will be added)
Install-Extension -GatewayEndpoint "https://wac.contoso.com" "msft.sme.containers" -Feed "https://aka.ms/sme-extension-feed"

# Install an extension (install a specific version)
Install-Extension "https://wac.contoso.com" "msft.sme.certificate-manager" "0.133.0"

# Uninstall-Extension
Uninstall-Extension "https://wac.contoso.com" "msft.sme.containers"

# Update-Extension
Update-Extension "https://wac.contoso.com" "msft.sme.containers"

注意

您必須是閘道系統管理員,才能使用 PowerShell 來修改 Windows Admin Center 擴充功能。

其他參考