🙄

指定したディレクトリにあるファイルを印刷する (PowerShell)

2022/08/01に公開
# Praram は先頭に宣言する必要がある
param(
	[parameter(mandatory)]
	[string] $Path,
	[string] $PrinterName,
	[string] $FileFilter
)

Set-StrictMode -Version Latest

# Write-Verbose (Default: SilentlyContinue)
$VerbosePreference = "Continue"
# Write-Debug (Default: SilentlyContinue)
$DebugPreference = "Continue"
#
# Write-Information (Default: SilentlyContinue)
$InformationPreference = "Continue"
# Write-Warning (Default: Continue)
$WarningPreference = "Stop"
# Write-Error (Default: Continue)
$ErrorActionPreference = "Stop"


function Main([string] $path, [string] $printerName, [string] $fileFilter) {
	Write-Verbose ""
	Write-Verbose "################################################################################"
	Write-Verbose "Out-Printer-PDF.ps1"
	Write-Verbose ""
	Write-Verbose "Path:        $path"
	Write-Verbose "PrinterName: $printerName"
	Write-Verbose "FileFilter:  $fileFilter"
	return Out-Printer-Directory $path $printerName $fileFilter
}

function Out-Printer-Directory([string] $path, [string] $printerName, [string] $fileFilter) {
	if ([string]::IsNullOrEmpty($path)) {
		Write-Error "An error occurred." -ErrorAction Stop
		return 254
	}
	if (-not (Test-Path $path)) {
		Write-Error "An error occurred." -ErrorAction Stop
		return 254
	}
	if (-not ((Get-Item $path) -is [System.IO.DirectoryInfo])) {
		Write-Error "An error occurred." -ErrorAction Stop
		return 254
	}
	<#
	if ([string]::IsNullOrEmpty($printerName)) {
		Write-Error "An error occurred." -ErrorAction Stop
		return 254
	}
	#>
	if ([string]::IsNullOrEmpty($fileFilter)) {
		$fileFilter = "*.*"
	}

	[int] $result = 0

	if ($null -ne $printerName) {
		[string] $filter = "Name='$printerName'"
		[PSCustomObject] $printer = Get-Cim-Printer $filter
		Set-Cim-DefaultPrinter $printer
	}

	[array] $items = Get-ChildItem -Path $path -Name -Filter $fileFilter
	foreach ($item in $items) {
		Write-Verbose "$item"

		Start-Process `
			-FilePath (Join-Path $path $item) `
			-Verb Print `
		| Stop-Process

		Start-Sleep -seconds 5
	}
	
	return $result
}


################################################################################
# プリンター

function Get-Cim-Printers {
	[array] $printers = Get-CimInstance -Class Win32_Printer
	foreach ($printer in $printers) {
		[string] $caption = $printer.Caption
		[string] $name = $printer.Name
		[string] $deviceID = $printer.DeviceID
		[string] $description = $printer.Description

		Write-Verbose "printer.Caption:     $caption"
		Write-Verbose "printer.Name:        $name"
		Write-Verbose "printer.DeviceID:    $deviceID"
		Write-Verbose "printer.Description: $description"
		Write-Verbose ""
	}

	return $printers
}

# $filter = "Name='HP LaserJet 5Si'"
function Get-Cim-Printer([string] $filter) {
	if ($null -eq $filter) {
		Write-Error "An error occurred." -ErrorAction Stop
		return $null
	}

	return Get-CimInstance -Class Win32_Printer -Filter $filter
}

function Get-Cim-DefaultPrinter {
	return Get-WmiObject Win32_Printer | Where-Object default
}

function Set-Cim-DefaultPrinter([PSCustomObject] $printer) {
	if ($null -eq $printer) {
		Write-Error "An error occurred." -ErrorAction Stop
		return
	}

	Invoke-CimMethod -InputObject $printer -MethodName SetDefaultPrinter	
}


# Entry point
exit Main $Path $PrinterName $FileFilter

lanuch.json のサンプル

{
	// IntelliSense を使用して利用可能な属性を学べます。
	// 既存の属性の説明をホバーして表示します。
	// 詳細情報は次を確認してください: https://go.microsoft.com/fwlink/?linkid=830387
	"version": "0.2.0",
	"configurations": [
		{
			"name": "ファイルを印刷 (ディレクトリ指定)",
			"type": "PowerShell",
			"request": "launch",
			"script": "./Out-Printer-Directory.ps1",
			"args": [
				"-PrinterName \"Canon MF644Cdw\"",
				"-FileFilter \"*.pdf\""
			],
			"cwd": "${workspaceFolder}"
		},
	]
}

Discussion