使用 PowerShell 管理 Windows Admin Center 设置

网关管理员可以使用 Windows Admin Center PowerShell 模块,在多个运行在管理模式下运行的 Windows Admin Center 服务器之间管理连接列表和扩展。 这种方法帮助您在不逐台更新服务器的情况下应用一致配置。 这些命令不为虚拟化模式下的 Windows Admin Center 提供通用自动化。

在本文中,你使用 PowerShell 导入和导出连接和标签,并管理扩展源和软件包。

用PowerShell导入或导出Windows Admin Center的连接和标签

# Load the module
Import-Module "$env:'Program Files'\WindowsAdminCenter\PowerShellModules\Microsoft.WindowsAdminCenter.ConnectionTools"
# Available cmdlets: Export-WACConnection, Import-WACConnection

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

Important

导入连接时需要该 -AccessKey 字段,但导出连接不需要该字段。 可以在Windows Admin Center设置的 Advanced 选项卡中找到Windows Admin Center访问密钥。

使用表单登录身份验证访问Windows Admin Center时,需要提供用于导入和导出连接的凭据。 可以使用以下命令传递凭据:

# Export connections by using credentials
Export-WACConnection "https://wac.contoso.com" -fileName "WAC-connections.csv" -AccessKey "<key string>" -Credentials (Get-Credential)
# Import connections by using credentials
Import-WACConnection -Endpoint "https://wac.contoso.com" -FileName "WAC-connections.csv" -AccessKey "<key string>" -Credentials (Get-Credential)

用于导入连接的 CSV 文件格式

CSV 文件的格式从以下四个标题开始,然后每个连接另起一行。

  • name:连接的FQDN。

  • type:连接类型。 对于 Windows Admin Center 附带的默认连接,请使用以下字符串之一:

    连接类型 连接字符串
    Windows Server msft.sme.connection-type.server
    故障转移群集 msft.sme.connection-type.cluster
  • tags:管道分离标签。

  • groupId:共享连接栏。 使用此列中的值 global 进行共享连接。

Note

修改共享连接仅限于网关管理员。 任何用户都可以使用 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"

Note

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 Windows Admin Center
     }
     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 want to export the new .csv file. If it's 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 管理 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"

Note

必须是网关管理员才能使用 PowerShell 修改 Windows Admin Center 扩展。

其他引用