0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PowerShellでアクセスにあるテーブル情報を元にoutlookから予定表を取得する方法

Posted at
# Accessデータベースの接続文字列
$connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Path\To\YourDatabase.accdb;"

# Accessデータベースからユーザーリストを取得
function Get-UserList {
    param (
        [string]$connectionString
    )

    $userList = @()
    
    $connection = New-Object -ComObject ADODB.Connection
    $connection.Open($connectionString)
    
    $command = New-Object -ComObject ADODB.Command
    $command.ActiveConnection = $connection
    $command.CommandText = "SELECT Email, Name FROM EmployeeTable"

    $recordset = $command.Execute()
    
    while (-not $recordset.EOF) {
        $user = [PSCustomObject]@{
            Email = $recordset.Fields.Item("Email").Value
            Name = $recordset.Fields.Item("Name").Value
        }
        $userList += $user
        $recordset.MoveNext()
    }

    $recordset.Close()
    $connection.Close()

    return $userList
}

# ユーザーリストを取得
$users = Get-UserList -connectionString $connectionString

# PowerShellで並列処理を行うためのジョブを定義
foreach ($user in $users) {
    Start-Job -ArgumentList $user.Email, $connectionString -ScriptBlock {
        param ($email, $connectionString)
        
        # Outlookアプリケーションのインスタンスを作成
        $outlook = New-Object -ComObject Outlook.Application
        $namespace = $outlook.GetNamespace("MAPI")
        $recipient = $namespace.CreateRecipient($email)
        $calendar = $namespace.GetSharedDefaultFolder($recipient, 9) # 9は予定表のフォルダID

        # Accessデータベースに接続
        $connection = New-Object -ComObject ADODB.Connection
        $connection.Open($connectionString)
        
        $items = $calendar.Items
        $items.Sort("[Start]")

        foreach ($item in $items) {
            # クエリを準備
            $query = "INSERT INTO YourTableName (Subject, Start, [End], Location) VALUES (?,?,?,?)"
            
            # コマンドを作成
            $command = New-Object -ComObject ADODB.Command
            $command.ActiveConnection = $connection
            $command.CommandText = $query

            # パラメータを追加
            $command.Parameters.Append($command.CreateParameter("p1", 200, 1, -1, $item.Subject))
            $command.Parameters.Append($command.CreateParameter("p2", 135, 1, -1, $item.Start))
            $command.Parameters.Append($command.CreateParameter("p3", 135, 1, -1, $item.End))
            $command.Parameters.Append($command.CreateParameter("p4", 200, 1, -1, $item.Location))

            # クエリを実行
            $command.Execute()
        }

        # 接続を閉じる
        $connection.Close()
    }
}

# 全ジョブの完了を待つ
Get-Job | Wait-Job | Out-Null

# ジョブの出力を取得して表示(オプション)
$results = Get-Job | Receive-Job
$results

# ジョブをクリーンアップ
Get-Job | Remove-Job
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?