powershellでプリントスクリーンで取ったスクショをエクセルに自動で貼り付ける

Add-Type -AssemblyName Microsoft.Office.Interop.Excel

# Excelアプリケーションを起動
$excel = New-Object -ComObject Excel.Application
$workbook = $excel.Workbooks.Add()
$worksheet = $workbook.Worksheets.Item(1)

# 現在の行と列を初期化
$currentRow = 1
$currentColumn = 1

# 無限ループを開始
while ($true) {
    Write-Host "Press ESC to stop..."
    
    # エスケープキーが押されたかどうかをチェック
    if ([System.Console]::KeyAvailable -and ([System.Console]::ReadKey($true).Key -eq [System.ConsoleKey]::Escape)) {
        break
    }
    
    # クリップボードから画像を取得
    $image = Get-Clipboard -Format Image

    # 一時ファイルに画像を保存
    $tempImagePath = "C:\Path\To\Your\Temp\Image.png"
    $image.Save($tempImagePath)
    
    # 画像をエクセルに貼り付け
    $worksheet.Shapes.AddPicture($tempImagePath, $false, $true, $range.Left, $range.Top, -1, -1)
    
    # 現在の行を更新
    $currentRow++
    
    # ファイルを保存
    $workbook.SaveAs("C:\Path\To\Your\Excel\File.xlsx")
    
    # 一時ファイルを削除
    Remove-Item $tempImagePath
    
    # 一定の待機時間(例: 5秒)を設ける
    Start-Sleep -Seconds 5
}

# ループ終了後にアプリケーションを終了
$workbook.Close($false)
$excel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)