# 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
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme