Azure-Samples / Azure-Samples/TestDeviceRegConnectivity

Modernization

オープン
#12 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る
主要言語
PowerShell
スター
35
フォーク
14
PR マージ指標
30日以内にマージされた PR はありません

説明

Had to spar with AI to make two changes to even get the script to run without errors:

**1. The Problem: Legacy Identity Logic**
The script crashes at line 277 because [System.DirectoryServices.AccountManagement.UserPrincipal]::Current is designed for Active Directory Domain Services (AD DS). In an Entra-joined environment, the identity object returned often doesn't fit the expected schema of that specific .NET class, leading to the Unable to cast...GroupPrincipal error.

The Solution: Modernized Test-DeviceRegConnectivity.ps1
Apply these three surgical fixes to the script you provided:

1. Fix Identity Resolution (Line 277)
Replace the block starting at line 277 with logic that gracefully handles Entra ID identities:

```
# Add-Type is kept for compatibility, but we avoid strict casting
Add-Type -AssemblyName System.DirectoryServices.AccountManagement

try {
$CurrentPrincipal = [System.DirectoryServices.AccountManagement.Principal]::Current
$UserUPN = "Unknown"

if ($CurrentPrincipal -and $CurrentPrincipal.StructuralObjectClass -eq "user") {
$UserUPN = whoami /upn
}
} catch {
Write-Log -Message "AccountManagement failed to resolve identity. Falling back to whoami." -Level WARN
$UserUPN = whoami /upn
}

$msg = "User Account: " + (whoami) + ", UPN: " + $UserUPN
Write-Log -Message $msg
```

**2. Robust Task Execution (Line 23)**
The RunPScript function fails because it uses Register-ScheduledJob, which creates a complex overhead. Since you confirmed Register-ScheduledTask works, replace the RunPScript function (Lines 23–32) with this simplified version:

```
Function RunPScript([String] $PSScript){
$GUID = [guid]::NewGuid().Guid
$B64Payload = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($PSScript))
$Action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -WindowStyle Hidden -EncodedCommand $B64Payload"
$Principal = New-ScheduledTaskPrincipal -UserID "NT AUTHORITY\SYSTEM" -LogonType ServiceAccount -RunLevel Highest

# Register and Start
$Task = Register-ScheduledTask -TaskName $GUID -Action $Action -Principal $Principal -ErrorAction Stop
Start-ScheduledTask -TaskName $GUID -ErrorAction Stop

# Wait for completion
while ((Get-ScheduledTask -TaskName $GUID).State -ne 'Ready') { Start-Sleep -Milliseconds 200 }

# Clean up
Unregister-ScheduledTask -TaskName $GUID -Confirm:$false
return "Task Executed" # Note: This script architecture makes capturing STDOUT difficult without a temp file.
}
```

With the changes, the test ran as it should

コントリビューションガイド

コントリビューションガイドを開く

調査の方向性

Test-DeviceRegConnectivity.ps1 を開き、23~32 行付近の RunPScript と、277 行付近の ID 解決を調査します。Entra-joined 環境でスクリプトを実行して失敗を再現し、その後、スケジュールされたタスクの実行、ID 解決、クリーンアップがエラーなしで完了することを確認します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
powershell
領域
tooling
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
45/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。