型[Windows.Data.Pdf.PdfDocument]が見つかるかの確認
# PowerShell 5.1, Windows 11 (2025年4月頃) $str = 'Windows.Data.Pdf.PdfDocument, Windows.Data, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime' [System.Type]::GetType($str).AssemblyQualifiedName | Write-Host -ForegroundColor DarkYellow #=> Windows.Data.Pdf.PdfDocument, Windows.Data, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime
WindowsRuntimeSystemExtensions.AsTaskメソッドに関連して[System.WindowsRuntimeSystemExtensions].GetMethods()の確認
# PowerShell 5.1, Windows 11 (2025年4月頃) Set-StrictMode -Version Latest Add-Type -AssemblyName System.Runtime.WindowsRuntime $mInfos = [System.WindowsRuntimeSystemExtensions].GetMethods() $psObjs = [PSObject[]]::new($mInfos.Length) for ($i = 0; $i -lt $psObjs.Length; $i++) { $psObjs[$i] = [PSObject]::new() } $numParamsMax = 0 for ($i = 0; $i -lt $psObjs.Length; $i++) { $psObjs[$i] | Add-Member -NotePropertyName "Name" -NotePropertyValue $mInfos[$i].Name $psObjs[$i] | Add-Member -NotePropertyName "ReturnType" -NotePropertyValue $mInfos[$i].ReturnType.ToString() $pInfos = $mInfos[$i].GetParameters() $psObjs[$i] | Add-Member -NotePropertyName "NumParams" -NotePropertyValue $pInfos.Length for ($k = 0; $k -lt $pInfos.Length; $k++) { $psObjs[$i] | Add-Member -NotePropertyName $("ParameterType" + $k.ToString("00")) -NotePropertyValue $pInfos[$k].ParameterType.Name } $numParamsMax = [System.Math]::Max($pInfos.Length, $numParamsMax) } $propNamesParameterType = [string[]]::new($numParamsMax) for ($i = 0; $i -lt $propNamesParameterType.Length; $i++) { $propNamesParameterType[$i] = "ParameterType" + $i.ToString("00") } $psObjs | Select-Object -Property $(@("Name", "ReturnType", "NumParams") + $propNamesParameterType) | Out-GridView
PDFのページ数を取得
# PowerShell 5.1, Windows 11 (2025年4月頃) $ErrorActionPreference = [System.Management.Automation.ActionPreference]::Stop $VerbosePreference = [System.Management.Automation.ActionPreference]::Continue Set-StrictMode -Version Latest $filePath = "${env:USERPROFILE}\Desktop\test.pdf" $fStream = [System.IO.File]::OpenRead($filePath) Add-Type -AssemblyName System.Runtime.WindowsRuntime $raStream = [System.IO.WindowsRuntimeStreamExtensions]::AsRandomAccessStream($fStream) $null = [Windows.Data.Pdf.PdfDocument, Windows.Data, ContentType = WindowsRuntime] $iAsyncOpPdfDoc = [Windows.Data.Pdf.PdfDocument]::LoadFromStreamAsync($raStream) Add-Type -AssemblyName System.Runtime.WindowsRuntime # メソッド名が AsTask であるものを選択 $miAsTasks = [System.Reflection.MethodInfo[]]@( [System.WindowsRuntimeSystemExtensions].GetMethods() | Where-Object { $_.Name -eq "AsTask" } ) # パラメーターが IAsyncOperation<TResult> だけのものを選択 $miAsTaskIAsyncOpGen = [System.Reflection.MethodInfo]( $miAsTasks | Where-Object { $paramInfos = $_.GetParameters() if ($paramInfos.Length -eq 1) { $paramInfos[0].ParameterType.Name -eq 'IAsyncOperation`1' } } ) # 戻り値とパラメーターを確認 $miAsTaskIAsyncOpGen.ReturnType.ToString() | Write-Verbose #=> 詳細: System.Threading.Tasks.Task`1[TResult] $miAsTaskIAsyncOpGen.GetParameters() | ForEach-Object { $_.ParameterType.ToString() | Write-Verbose } #=> 詳細: Windows.Foundation.IAsyncOperation`1[TResult] # MakeGenericMethod $miAsTaskIAsyncOpPdfDoc = $miAsTaskIAsyncOpGen.MakeGenericMethod([System.Type[]]@([Windows.Data.Pdf.PdfDocument])) # 戻り値とパラメーターを確認 $miAsTaskIAsyncOpPdfDoc.ReturnType.ToString() | Write-Verbose #=> 詳細: System.Threading.Tasks.Task`1[Windows.Data.Pdf.PdfDocument] $miAsTaskIAsyncOpPdfDoc.GetParameters() | ForEach-Object { $_.ParameterType.ToString() | Write-Verbose } #=> 詳細: Windows.Foundation.IAsyncOperation`1[Windows.Data.Pdf.PdfDocument] # Task<PdfDocument> を取得 $taskPdfDoc = $miAsTaskIAsyncOpPdfDoc.Invoke($null, @($iAsyncOpPdfDoc)) $taskPdfDoc.Wait() # Task<PdfDocument>.Result $pdfDoc = $taskPdfDoc.Result # PDFのページ数 $pdfDoc.PageCount $fStream.Dispose()