> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 演练:使用 PowerShell/API 部署构建

> 使用 PowerShell 和 Multiplayer Servers API 通过 Windows Runner C# 示例部署 Windows 操作系统 PlayFab Multiplayer Servers 构建的演练。

本主题介绍如何基于 Windows Runner C# 示例,在 Windows 10 开发设备上使用 PowerShell 部署/创建 Windows 操作系统 VM 的构建。

<Note>
  要使用和查看 PlayFab Multiplayer Servers,你需要启用该功能。我们建议你使用 Game Manager 方法启用此功能。有关说明,请参阅[启用 PlayFab 服务器功能](/services/playfab/multiplayer/servers/enable-playfab-multiplayer-servers)。
</Note>

## 先决条件

确保你已完成以下步骤。

* [配置 API 功能选项](/services/playfab/multiplayer/servers/windows-runner-sample#configure-api-feature-option)
* [服务器设置](/services/playfab/multiplayer/servers/windows-runner-sample#server-set-up)。

## 1. 获取你的游戏 ID 和开发者密钥

* 获取你的 PlayFab 游戏 ID
  * 登录到 [PlayFab.com](https://developer.playfab.com) 上的开发者账户
  * 在 Game Manager 中,转到 **My Studios and Titles** 页面。查找你的游戏标题并获取 PlayFab 游戏标题 ID

* 获取该游戏的开发者密钥
  * 在 Game Manager 中,选择你的游戏 > 设置(齿轮图标)
  * 选择 **Title settings**,然后选择 **Secret Keys** 选项卡以获取开发者密钥

有关密钥的详细信息,请参阅[密钥管理](/services/playfab/live-service-management/gamemanager/secret-key-management)

## 2. 安装 PlayFab Multiplayer PowerShell 模块

以管理员身份打开 Windows Powershell 并运行以下命令以安装 [PlayFabMultiplayer API 模块](https://github.com/PlayFab/MpsPowershell)。这个新的 [PlayFabMultiplayer API 模块](https://github.com/PlayFab/MpsPowershell)替换了已弃用的 Multiplayer PowerShell 模块。

如果你之前安装了 [PlayFab Multiplayer Powershell](https://github.com/PlayFab/MultiplayerPowershell) 模块,请使用以下命令卸载它。对于要过渡到新模块的用户,请参阅[命令映射](/services/playfab/multiplayer/servers/deploy-using-powershell-api#mapping-commands)以查找新的等效命令。

```powershell theme={null}
Uninstall-Package PlayFabMultiplayer
```

此新模块中的命令和参数都不同。有关每个命令的详细文档,请参阅 [Cmdlet 文档](https://github.com/PlayFab/MpsPowershell/tree/main/MpsPowershell/docs)。

```powershell theme={null}
Install-Module -Name PlayFabMultiplayerApi
```

<Tip>
  在安装模块之前,你可能需要先使用 `Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser` 设置执行策略。要了解更多信息,请参阅 [PowerShell 执行策略](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_execution_policies)。
</Tip>

## 3. 为你的游戏获取 EntityToken

在 **PowerShell** 窗口中,使用你的游戏 ID 和相关开发者密钥运行以下命令:

```powershell theme={null}
Set-PfTitle -TitleID "mytitleID" -SecretKey "mysecretkey"
```

## 4. 上传资产

在部署 Windows 服务器时,你将使用托管 Windows 容器。你所要做的就是将 PlayFab Multiplayer 游戏服务器构建作为资产上传。

将 **FilePath** 标志的值更新为 **winrunnerSample.zip** 的本地文件位置。如果你不知道此文件在哪里,请按照[此处](/services/playfab/multiplayer/servers/windows-runner-sample#server-set-up)的说明获取该文件。

```powershell theme={null}
New-PfAsset -FilePath C:\windowsRunnerCSharp.zip -AssetName windowsRunnerCSharp.zip
```

## 5. 创建构建

现在资产已上传,我们可以创建构建。运行以下命令。

下面的代码在 **EastUS** 区域中使用 **Standard\_D2as\_v4** VM。请根据你想使用的 VM 和区域替换字符串。

```powershell theme={null}
$vmSize = "Standard_D2as_v4"

$regions = @( @{ StandbyServers = 1; MaxServers = 1; Region = 'EastUS'; ScheduledStandbySettings = $NULL } )

$ports = @( @{ Name = 'game_port'; Num = 3600; Protocol = 'TCP' } )

$gameAssets = @( @{ FileName = 'windowsRunnerCSharp.zip'; MountPath = 'C:\Assets' } )

$buildResponse = New-PfBuild -BuildName PSTest_build -ContainerFlavor ManagedWindowsServerCore -StartMultiplayerServerCommand 'C:\Assets\WindowsRunnerCSharp.exe' -GameAssetReferences $gameAssets -VMSize $vmSize -MultiplayerServerCountPerVM 1 -Ports $ports -RegionConfigurations $regions

# All PlayFabMultiplayerApi cmdlets return objects, so we can pass the returned object to ConvertTo-Json for human readability.
$buildResponse | ConvertTo-Json -depth 5
```

## 检查你的构建是否成功

几秒钟后,你将看到通过 **PowerShell** 或 [ListBuildSummaries API](xref:titleid.playfabapi.com.multiplayer.multiplayerserver.listbuildsummariesv2) 创建的构建。

你还可以运行以下命令来检查构建是否成功部署。

```powershell theme={null}
Get-PfBuild | ConvertTo-Json -depth 5
```

## 另请参阅

* [演练:使用 Game Manager 部署构建](/services/playfab/multiplayer/servers/quickstart-for-multiplayer-servers-game-manager)
* [Windows Runner C# 示例](/services/playfab/multiplayer/servers/windows-runner-sample)
* [示例和资源](/services/playfab/multiplayer/servers/server-samples-resources)
