通用列印 PowerShell 使用案例範例
UniversalPrintManagement PowerShell 模組支援已建立的 PowerShell 腳本模式。 本文將示範一些模式作為如何結合 Cmdlet 以處理選取的使用案例的起點。
非互動式通用印表連線
使用 PowerShell 的主要值之一是建立可以反覆執行的非互動式腳本。 必須輸入用戶認證,才能建立與通用列印的連線,這與這個想法不一定。 其中一個選項是解決這個問題,就是安全地儲存使用者密碼密碼,並視需要加以擷取。
- 安全地將密碼密碼儲存至檔案
- 在呼叫之前擷取密碼
Connect-UPService
$Secure = Read-Host -AsSecureString
$Encrypted = ConvertFrom-SecureString -SecureString $Secure -Key (1..16)
$Encrypted | Set-Content Encrypted.txt
...
$Secure2 = Get-Content Encrypted.txt | ConvertTo-SecureString -Key (1..16)
Connect-UPService -UserPrincipalName username@tenantname.com -Password $Secure2
注意
如需 和 的其他資訊ConvertFrom-SecureString
,請參閱這裡。ConvertTo-SecureString
批次取消註冊 連線 or 的印表機
假設您已經知道用來註冊印表機的已註冊 連線 或名稱。 請參閱 Get-UP 連線 or Cmdlet 來擷取已註冊的 連線 器清單。
- 連線 至通用列印
- 取得透過特定 連線 or 註冊的印表機清單
- 拿掉已註冊的印表機
Connect-UPService
$ConnectorPrinters = Get-UPPrinter -IncludeConnectorDetails
$ConnectorPrinters.Results | Where-Object {$_.Connectors.DisplayName -Contains "<Connector Name>"} | Remove-UPPrinter
識別共用印表機名稱後方的已註冊印表機
- 連線 至通用列印
- 擷取印表機清單,並使用本機計算機來篩選結果
Connect-UPService
$Printers = Get-UPPrinter
$Printer = $Printers.Results | Where-Object {$_.Shares.DisplayName -eq "<Share Name>"}
Batch 取消共用印表機
- 連線 至通用列印
- 取得感興趣的印表機清單
- 取消共用印表機的集合
注意
此範例顯示所有共享印表機的未共用。 若要只取消共用選取的印表機,擷取印表機時可以新增其他篩選。
Connect-UPService
$Printers = Get-UPPrinter
$Printers.Results.Shares | Remove-UPPrinterShare
Batch 授與所有使用者共用印表機的存取權
- 連線 至通用列印
- 取得感興趣的印表機共享清單
- 授與使用者對印表機集合的存取權
Connect-UPService
$PrinterShares = Get-UPPrinterShare
$PrinterShares.Results | Grant-UPAccess -AllUsersAccess
Batch 授與使用者或使用者群組共用印表機的存取權
- 預先要求:擷取使用者標識碼和使用者群組標識碼
- 連線 至通用列印
- 取得感興趣的印表機共享清單
- 授與特定使用者或群組對印表機集合的存取權
Connect-AzAccount
$Users = Get-AzADUser -First 10
$UserGroups = Get-AzADGroup -SearchString Contoso
Connect-UPService
$PrinterShares = Get-UPPrinterShare
$Users | ForEach-Object {$PrinterShares.Results | Grant-UPAccess -UserID $_.Id}
$UserGroups | ForEach-Object {$PrinterShares.Results | Grant-UPAccess -GroupID $_.Id}
批次集印表機位置屬性
- 連線 至通用列印
- 取得感興趣的印表機清單
- 授與使用者對印表機集合的存取權
Connect-UPService
$Printers = Get-UPPrinter
$Printers.Results | Set-UPPrinterProperty -Latitude 47.642391 -Longitude -122.137001 -Organization "Contoso" -Site "Main Campus" -City "Redmond" -Building "101"
使用 ContinuationToken
- 連線 至通用列印
- 取得感興趣的印表機清單
- 如果遇到間歇性失敗,則會傳回部分結果,以及接續令牌。
Connect-UPService
$Printers = Get-UPPrinter
"Get-UPPrinter :
At line:1 char:13
+ $Printers = Get-UPPrinter
+ ~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (System.Collecti...Models.Printer]:List`1) [Get-UPPrinter], FailedCmdletException
+ FullyQualifiedErrorId : Rerun with -ContinuationToken to continue from last cutoff: MyZRVkZCUVVGQlFVRXZMeTh2THk4dkx5OHZMMGxCUVVGQk5IQTRiR
EY0YWpOdU1DdEtPSG94T1dwUWNHWm5kejA5,Microsoft.UPManagement.Cmdlets.GetPrinter"
$Printers.Results
"(Partial results list of printers)"
$Printers.ContinuationToken
"MyZRVkZCUVVGQlFVRXZMeTh2THk4dkx5OHZMMGxCUVVGQk5IQTRiREY0YWpOdU1DdEtPSG94T1dwUWNHWm5kejA5"
$MorePrinters = Get-UPPrinter -ContinuationToken $Printers.ContinuationToken
$MorePrinters.Results
"(Printer results list continued from previously left off)"
- ContinuationToken 可讓呼叫結果更健全。 重試可以新增並附加至彼此。 請注意,只有已註冊的印表機/連接器/共用/等數量很高(~8000+)且在逐一查看整個清單時發生一致失敗的租使用者才需要此專案。
do
{
$i = Get-UPPrinter -ContinuationToken $i.ContinuationToken
$allprinters += $i.Results
}
while (![string]::IsNullOrEmpty($i.ContinuationToken))
$allprinters.Results
下載上個月的擴充使用者和印表機使用量報告
- 請確定已安裝正確的 Microsoft Graph 模組
- 登入 Microsoft Graph
- 要求必要的 「Reports.Read.All」 授權範圍
- 收集及導出印表機報表
- 收集及導出用戶報告
Param (
# Date should be in this format: 2020-09-01
# Default is the first day of the previous month at 00:00:00 (Tenant time zone)
$StartDate = "",
# Date should be in this format: 2020-09-30
# Default is the last day of the previous month 23:59:59 (Tenant time zone)
$EndDate = "",
# Set if only the Printer report should be generated
[switch]$PrinterOnly,
# Set if only the User report should be generated
[switch]$UserOnly
)
#############################
# INSTALL & IMPORT MODULES
#############################
if(-not (Get-Module Microsoft.Graph.Reports -ListAvailable)){
Write-Progress -Activity "Installing Universal Print dependencies..." -PercentComplete 60
Install-Module Microsoft.Graph.Reports -Scope CurrentUser -Force
}
Import-Module Microsoft.Graph.Reports
#############################
# SET DATE RANGE
#############################
if ($StartDate -eq "") {
$StartDate = (Get-Date -Day 1).AddMonths(-1).ToString("yyyy-MM-ddT00:00:00Z")
} else {
$StartDate = ([DateTime]$StartDate).ToString("yyyy-MM-ddT00:00:00Z")
}
if ($EndDate -eq "") {
$EndDate = (Get-Date -Day 1).AddDays(-1).ToString("yyyy-MM-ddT23:59:59Z")
} else {
$EndDate = ([DateTime]$EndDate).ToString("yyyy-MM-ddT23:59:59Z")
}
echo "Gathering reports between $StartDate and $EndDate."
########################################
# SIGN IN & CONNECT TO MICROSOFT GRAPH
########################################
# These scopes are needed to get the list of users, list of printers, and to read the reporting data.
Connect-MgGraph -Scopes "Reports.Read.All"
##########################
# GET PRINTER REPORT
##########################
if (!$UserOnly)
{
Write-Progress -Activity "Gathering Printer usage..." -PercentComplete -1
# Get the printer usage report
$printerReport = Get-MgReportMonthlyPrintUsageByPrinter -All -Filter "completedJobCount gt 0 and usageDate ge $StartDate and usageDate lt $EndDate"
## Join extended printer info with the printer usage report
$reportWithPrinterNames = $printerReport |
Select-Object (
@{Name = "UsageMonth"; Expression = {$_.Id.Substring(0,8)}},
@{Name = "PrinterId"; Expression = {$_.PrinterId}},
@{Name = "DisplayName"; Expression = {$_.PrinterName}},
@{Name = "TotalJobs"; Expression = {$_.CompletedJobCount}},
@{Name = "ColorJobs"; Expression = {$_.CompletedColorJobCount}},
@{Name = "BlackAndWhiteJobs"; Expression = {$_.CompletedBlackAndWhiteJobCount}},
@{Name = "ColorPages"; Expression = {$_.ColorPageCount}},
@{Name = "BlackAndWhitePages"; Expression = {$_.BlackAndWhitePageCount}},
@{Name = "TotalSheets"; Expression = {$_.MediaSheetCount}})
# Write the aggregated report CSV
$printerReport | Export-Csv -Path .\printerReport.csv
}
##################
# GET USER REPORT
##################
if (!$PrinterOnly)
{
Write-Progress -Activity "Gathering User usage..." -PercentComplete -1
# Get the user usage report
$userReport = Get-MgReportMonthlyPrintUsageByUser -All -Filter "completedJobCount gt 0 and usageDate ge $StartDate and usageDate lt $EndDate"
$reportWithUserInfo = $userReport |
Select-Object (
@{Name = "UsageMonth"; Expression = {$_.Id.Substring(0,8)}},
@{Name = "UserPrincipalName"; Expression = {$_.UserPrincipalName}},
@{Name = "TotalJobs"; Expression = {$_.CompletedJobCount}},
@{Name = "ColorJobs"; Expression = {$_.CompletedColorJobCount}},
@{Name = "BlackAndWhiteJobs"; Expression = {$_.CompletedBlackAndWhiteJobCount}},
@{Name = "ColorPages"; Expression = {$_.ColorPageCount}},
@{Name = "BlackAndWhitePages"; Expression = {$_.BlackAndWhitePageCount}},
@{Name = "TotalSheets"; Expression = {$_.MediaSheetCount}})
$reportWithUserInfo | Export-Csv -Path .\userReport.csv
}
echo "Reports were written to 'printerReport.csv' and 'userReport.csv'."