コマンドで正しい情報が取得できている、例外が発生する

妹尾剛 60 評価のポイント
2024-02-20T06:41:28.35+00:00

スクリプト内で以下のコマンドを実行しました。 $lastSyncTime = (Get-MgDeviceManagementManagingDevice -ManagedDeviceId $deviceId).LastSyncDateTime 上記コマンドは正常に実行されますが、例外処理のcatchの処理が実行されます。 解決するにはどうすればよいですか? スクリプトは以下を実行しており、端末機同時刻の取得で発生しています。

===================

全デバイスの情報を取得

$Devices = @(Get-MgDevice | Select-Object Id, DeviceId | Sort-Object DeviceId) $deviceInfo = @(Get-MgDeviceManagementManagedDevice | Select-Object Id, AzureAdDeviceId | Sort-Object AzureAdDeviceId)

デバイスごとに処理

for($i = 0; $i -lt $Devices.Count; $i++) { $objectId = $Devices[$i].Id $deviceId = $deviceInfo[$i].Id $AzureAdDeviceId = $deviceInfo[$i].AzureAdDeviceId

# プライマリユーザーの取得
try {
    $primaryUser = (Get-MgDeviceManagementManagedDeviceUser -ManagedDeviceId $deviceId).DisplayName
} catch {
    Write-Host "デバイスのプライマリユーザーを取得できませんでした。デバイスID: $AzureAdDeviceId"
    continue
}

# 端末起動時刻の取得
try {
     $lastSyncTime = (Get-MgDeviceManagementManagedDevice -ManagedDeviceId $deviceId).LastSyncDateTime
     $lastSyncTimeJst = $lastSyncTime.AddHours(9)
     Write-Host "デバイスの起動時刻 (JST): $lastSyncTimeJst"
} catch {
    Write-Host "デバイスの起動時刻を取得できませんでした。デバイスID: $AzureAdDeviceId"
    continue
}

# ログインユーザーとログイン時間の取得
try {
    $signInInfo = Get-MgAuditLogSignIn -Filter "startsWith(appDisplayName,'Windows Sign In')" | ForEach-Object -Process {
        [PSCustomObject]@{
            DeviceId = $_.DeviceDetail.DeviceId
            CreatedDateTime = $_.CreatedDateTime
            loginTime = $_.CreatedDateTime.AddHours(9)
            UserPrincipalName = $_.UserPrincipalName
        }
    } | Where-Object DeviceId -eq $deviceId | Select-Object -First 1

    $loginUser = $signInInfo.UserPrincipalName
    $loginTime = $signInInfo.loginTime
} catch {
    Write-Host "デバイスの最終ログイン情報を取得できませんでした。デバイスID: $AzureAdDeviceId"
    continue
}

# OrderIDの取得
try {
     $output = Get-AzureADDevice -ObjectId "6302f913-c1b3-488b-8236-5f649732831e" | select DevicePhysicalIds -ExpandProperty DevicePhysicalIds
     $gid = $null
     $output -split "`n" | ForEach-Object {
       # 行が[OrderId]で始まる場合、その行を出力
     if ($_ -match "^\[OrderId\]") {
         $orderId = $_
        }
     }
} catch {
    Write-Host "デバイスの起動時刻を取得できませんでした。デバイスID: $AzureAdDeviceId"
    continue
}

# 拡張属性の追加
try {
     $Attribute1 = $primaryUser
     $body1 = "{`"extensionAttributes`": {`"extensionAttribute1`": `"${Attribute1}`" }}"
     Update-MgDevice -DeviceId $objectId -BodyParameter $body1
     
     if ($loginUser -ne $null) {
         $Attribute2 = $loginUser
         $body2 = "{`"extensionAttributes`": {`"extensionAttribute2`": `"${Attribute2}`" }}"
         Update-MgDevice -DeviceId $objectId -BodyParameter $body2
     }
     
     if ($loginTime -ne $null) {
         $Attribute3 = $loginTime
         $body3 = "{`"extensionAttributes`": {`"extensionAttribute3`": `"${Attribute3}`" }}"
         Update-MgDevice -DeviceId $objectId -BodyParameter $body3
     }
     
     $Attribute4 = $lastSyncTimeJst
     $body4 = "{`"extensionAttributes`": {`"extensionAttribute4`": `"${Attribute4}`" }}"
     Update-MgDevice -DeviceId $objectId -BodyParameter $body4

     $Attribute6 = $orderId
     $body6 = "{`"extensionAttributes`": {`"extensionAttribute6`": `"${Attribute6}`" }}"
     Update-MgDevice -DeviceId $objectId -BodyParameter $body6

} catch {
    Write-Host "エラー: デバイスの拡張属性を追加できませんでした。デバイスID: $AzureAdDeviceId"
    continue
}

Write-Host "デバイスの拡張属性の処理が完了しました。デバイスID: $AzureAdDeviceId"

}

Microsoft Q&A
Microsoft Q&A
このタグを使用して、提案、機能要求、バグを Microsoft Q&A チームと共有します。 Microsoft Q&A チームは、定期的にお客様のフィードバックを評価し、その過程で更新プログラムを提供します。
72 件の質問
0 件のコメント コメントはありません
{count} 件の投票

承認済みの回答
  1. gekka 7,011 評価のポイント MVP
    2024-02-20T09:41:43.7733333+00:00

    まずは、例外をキャッチしたらエラーメッセージを表示させて、原因を調べればよいのではないでしょうか?

    try{
        #コマンド
    }catch{
        Write-Host $PSItem.Exception.Message
    }
    

    LastSyncDateTimeはDateTime?型となっているのでnullの可能性があります。 その場合、nullはAddHoursできないのでエラーになります。


    # 正常に実行されるとされるコマンドはGet-MgDeviceManagementManagingDeviceで下部のコードだとGet-MgDeviceManagementManagedDeviceという問題は置いておいて

    1 人がこの回答が役に立ったと思いました。
    0 件のコメント コメントはありません

0 件の追加の回答

並べ替え方法: 最も役に立つ