Share via


ユニバーサル プリント PowerShell ユース ケースのサンプル

UniversalPrintManagement PowerShell モジュールは、確立された PowerShell スクリプト パターンをサポートします。 この記事では、コマンドレットを組み合わせて選択したユース ケースに取り組む方法の開始点として、いくつかのパターンを示します。

非対話型ユニバーサル プリント接続

PowerShell を使用することの主な有用性の 1 つは、何度も繰り返し実行できる非対話型スクリプトを作成することです。 ユニバーサル プリントへの接続を確立するためにユーザー資格情報を入力する要求は、この考えに反します。 1 つのオプションは、ユーザー パスワード シークレットを安全に格納し、必要に応じて取得することです。

  1. パスワード シークレットを安全にファイルに格納する
  2. 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

Note

ConvertFrom-SecureStringConvertTo-SecureString に関する追加情報は、こちらからご覧ください。

コネクタのプリンターのバッチ登録解除

プリンターの登録に使用した登録済みコネクタの名前が既にわかっていると仮定します。 登録されているコネクタの一覧を取得するには、Get-UPConnector コマンドレットを参照してください。

  1. ユニバーサル プリントに接続する
  2. 特定のコネクタを介して登録されているプリンターの一覧を取得する
  3. 登録済みのプリンターを削除する
Connect-UPService
$ConnectorPrinters = Get-UPPrinter -IncludeConnectorDetails
$ConnectorPrinters.Results | Where-Object {$_.Connectors.DisplayName -Contains "<Connector Name>"} | Remove-UPPrinter

共有プリンター名の背後にある登録済みプリンターの識別

  1. ユニバーサル プリントに接続する
  2. プリンターの一覧を取得し、ローカル コンピューターを使用して結果をフィルター処理する
Connect-UPService
$Printers = Get-UPPrinter
$Printer = $Printers.Results | Where-Object {$_.Shares.DisplayName -eq "<Share Name>"}

バッチの共有解除済みプリンタ

  1. ユニバーサル プリントに接続する
  2. 目的のプリンターの一覧を取得する
  3. プリンターのコレクションの共有を解除する

Note

この例では、すべての共有プリンターの共有解除を示します。 選択したプリンターのみの共有を解除するには、プリンターを取得するときにさらにフィルターを追加できます。

Connect-UPService
$Printers = Get-UPPrinter
$Printers.Results.Shares | Remove-UPPrinterShare

すべてのユーザーに共有プリンターへのアクセス権をバッチ付与する

  1. ユニバーサル プリントに接続する
  2. 目的のプリンター共有の一覧を取得する
  3. プリンターのコレクションへのアクセス権をユーザーに付与する
Connect-UPService
$PrinterShares = Get-UPPrinterShare
$PrinterShares.Results | Grant-UPAccess -AllUsersAccess

ユーザーまたはユーザー グループに共有プリンターへのアクセス権をバッチ付与する

  1. 前提条件: ユーザー ID とユーザー グループ ID を取得する
  2. ユニバーサル プリントに接続する
  3. 目的のプリンター共有の一覧を取得する
  4. 特定のユーザーまたはグループにプリンターのコレクションへのアクセス権を付与する
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}

バッチ セットプリンターの場所のプロパティ

  1. ユニバーサル プリントに接続する
  2. 目的のプリンターの一覧を取得する
  3. プリンターのコレクションへのアクセス権をユーザーに付与する
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 の使用

  1. ユニバーサル プリントに接続する
  2. 目的のプリンターの一覧を取得する
  3. 断続的なエラーが発生した場合は、後続トークンと共に部分的な結果が返されます。
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)"
  1. ContinuationToken を使用すると、結果の呼び出しがより堅牢になります。 再試行は、相互に追加および付加できます。 これは、登録済みのプリンター/コネクタ/共有などの数が多く (最大 8000 以上)、一覧全体を反復処理するときに変わらずエラーが発生しているテナントにのみ必要であることに注意してください。
do
{
    $i = Get-UPPrinter -ContinuationToken $i.ContinuationToken 
    $allprinters += $i.Results
}
while (![string]::IsNullOrEmpty($i.ContinuationToken))

$allprinters.Results

先月の拡張ユーザーとプリンターの使用状況レポートをダウンロードする

  1. 正しい Microsoft Graph モジュールがインストールされていることを確認する
  2. Microsoft Graph へのサインイン
  3. 必要な "Reports.Read.All" 承認スコープを要求する
  4. プリンター レポートを収集 & エクスポートする
  5. ユーザー レポートを収集 & エクスポートする
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'."