通过


通用打印 PowerShell 模块

有两个选项可用于从 PowerShell 管理通用打印:

  • 使用 UniversalPrintManagement 模块,该模块提供了完成常见管理任务的最简单方法。 虽然命令更易于使用,但Microsoft图形 API 中提供的一些高级功能可能缺失。
  • 使用 Microsoft Graph Powershell SDK,该 SDK 允许在 PowerShell 中针对整个Microsoft图形 API 编写脚本。 命令级别较低,因此它们可能不太容易用于简单任务。

下面提供了这两种方法的文档。 在示例之间切换以查看主要差异。

开始使用

先决条件

  • 升级到 PowerShell 5.1 或更高版本
  • 使用 将 PowerShellGet 更新到最新版本 Install-Module PowerShellGet
  • 必须使用 PowerShell 脚本执行策略设置为 RemoteSigned 或降低限制级别 Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser详细了解限制级别

步骤 1:安装 PowerShell 模块

# To install for all users (must be administrator), remove -Scope CurrentUser
Install-Module UniversalPrintManagement -Scope CurrentUser

此命令将从PowerShell 库获取请求的模块,该模块默认情况下未配置为 PowerShellGet 的受信任存储库。 首次运行此命令时,可能会看到以下提示:

Untrusted repository

You are installing the modules from an untrusted repository. If you trust this repository, change
its InstallationPolicy value by running the `Set-PSRepository` cmdlet.

Are you sure you want to install the modules from 'PSGallery'?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"):

请回答 YesYes to All 继续安装。

步骤 2:连接到通用打印

安装模块后,需要先连接到通用打印,然后才能运行任何命令。

# To provide authentication details interactively using a system authentication prompt:
Connect-UPService

# To provide authentication details non-interactively:
# Connect-UPService -UserPrincipalName username@tenantname.com -Password $variableHoldingSecureString

步骤 3:通过获取打印机列表来验证配置

若要确保一切设置正确,请尝试获取向租户注册的打印机列表。 有关可用命令的完整列表,请参阅 命令参考

Get-UPPrinter

更新或卸载

更新 PowerShell 模块

Update-Module UniversalPrintManagement

卸载 PowerShell 模块

如果需要稍后卸载模块,请使用以下命令:

Uninstall-Module -Name UniversalPrintManagement