Skip to content

Commit d94f24f

Browse files
Resolved copilot comments
1 parent 4362560 commit d94f24f

File tree

2 files changed

+21
-28
lines changed

2 files changed

+21
-28
lines changed

diagnostics/resources/log_collection_script.ps1

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ param(
1212
[string]$ExeName = "",
1313

1414
[Parameter(Mandatory=$false)]
15-
[string]$userDataDir = ""
15+
[string]$UserDataDir = ""
1616
)
1717

1818
# Load Windows Forms assembly for GUI
@@ -305,7 +305,7 @@ function Create-DiagnosticZip {
305305
[string]$DirectoryFilePath,
306306
[string]$TraceFilePath,
307307
[string]$ZipPath,
308-
[string]$WatsonMetadataFilePath = ""
308+
[string]$CrashpadFolderPath = ""
309309
)
310310

311311
try {
@@ -377,15 +377,18 @@ function Create-DiagnosticZip {
377377
}
378378

379379
# Add Crashpad folder if it exists
380-
if ($WatsonMetadataFilePath -and (Test-Path $WatsonMetadataFilePath)) {
380+
if ($CrashpadFolderPath -and (Test-Path $CrashpadFolderPath)) {
381381
try {
382382
Write-Host "Adding Crashpad folder contents..." -ForegroundColor Cyan
383-
$crashpadFiles = Get-ChildItem -Path $WatsonMetadataFilePath -Recurse -File -ErrorAction SilentlyContinue
383+
$crashpadFiles = Get-ChildItem -Path $CrashpadFolderPath -Recurse -File -ErrorAction SilentlyContinue
384+
385+
# Trim trailing backslash to ensure correct substring calculation
386+
$crashpadBasePath = $CrashpadFolderPath.TrimEnd('\')
384387

385388
foreach ($crashpadFile in $crashpadFiles) {
386389
try {
387390
# Get relative path within Crashpad folder
388-
$relativePath = $crashpadFile.FullName.Substring($WatsonMetadataFilePath.Length + 1)
391+
$relativePath = $crashpadFile.FullName.Substring($crashpadBasePath.Length + 1)
389392
$zipEntryName = "Crashpad/$relativePath".Replace("\", "/")
390393

391394
$entry = $zip.CreateEntry($zipEntryName)
@@ -474,7 +477,7 @@ function Stop-WPRTrace {
474477
# Create diagnostic zip with all collected files
475478
Write-Host ""
476479
Write-Host "Creating diagnostic package..." -ForegroundColor Cyan
477-
$zipResult = Create-DiagnosticZip -RegistryFilePath $script:RegistryFilePath -DirectoryFilePath $script:DirectoryFilePath -TraceFilePath $TracePath -ZipPath $script:FinalZipPath -WatsonMetadataFilePath $script:WatsonMetadataFilePath
480+
$zipResult = Create-DiagnosticZip -RegistryFilePath $script:RegistryFilePath -DirectoryFilePath $script:DirectoryFilePath -TraceFilePath $TracePath -ZipPath $script:FinalZipPath -CrashpadFolderPath $script:CrashpadFolderPath
478481

479482
if ($zipResult) {
480483
Write-Host "All diagnostic files have been packaged and saved to: $zipResult" -ForegroundColor Green
@@ -757,7 +760,7 @@ function Get-WebView2UserDataFolder {
757760
$uniqueUserDataFolders = @()
758761

759762
if (-not [string]::IsNullOrWhiteSpace($UserDataDir)) {
760-
Write-Host "Using provided userDataDir: $UserDataDir" -ForegroundColor Cyan
763+
Write-Host "Using provided UserDataDir: $UserDataDir" -ForegroundColor Cyan
761764
$folderToCheck = $UserDataDir
762765
}
763766
else {
@@ -771,8 +774,6 @@ function Get-WebView2UserDataFolder {
771774
return @{ UserDataFolders = @(); CrashpadFolder = "" }
772775
}
773776

774-
$userDataFolders = @()
775-
776777
foreach ($process in $processes) {
777778
$commandLine = $process.CommandLine
778779

@@ -786,26 +787,18 @@ function Get-WebView2UserDataFolder {
786787
# Extract --user-data-dir value
787788
# Pattern handles: --user-data-dir="path" or --user-data-dir=path
788789
if ($commandLine -match '--user-data-dir=(?:"([^"]+)"|([^\s]+))') {
789-
$userDataFolder = if ($matches[1]) { $matches[1] } else { $matches[2] }
790-
$userDataFolders += $userDataFolder
790+
$folderToCheck = if ($matches[1]) { $matches[1] } else { $matches[2] }
791+
$uniqueUserDataFolders = @($folderToCheck)
792+
Write-Host "Found user data folder: $folderToCheck" -ForegroundColor Green
793+
break
791794
}
792795
}
793796
}
794797
}
795798

796-
# Get unique values only
797-
$uniqueUserDataFolders = $userDataFolders | Select-Object -Unique
798-
799-
if ($uniqueUserDataFolders.Count -eq 0) {
799+
if (-not $folderToCheck) {
800800
Write-Host "No matching processes found with the specified criteria" -ForegroundColor Yellow
801801
}
802-
else {
803-
Write-Host "Total user data folders found: $($userDataFolders.Count) (unique: $($uniqueUserDataFolders.Count))" -ForegroundColor Green
804-
}
805-
806-
if ($uniqueUserDataFolders.Count -gt 0) {
807-
$folderToCheck = $uniqueUserDataFolders[0]
808-
}
809802
}
810803

811804
if (-not [string]::IsNullOrWhiteSpace($folderToCheck)) {
@@ -854,21 +847,21 @@ Write-Host "Zip destination: $ZipPath" -ForegroundColor Yellow
854847
Write-Host ""
855848

856849
$result = @{ UserDataFolders = @(); CrashpadFolder = "" }
857-
if (-not [string]::IsNullOrWhiteSpace($ExeName)) {
858-
$result = Get-WebView2UserDataFolder -ExeName $ExeName -UserDataDir $userDataDir
850+
if (-not [string]::IsNullOrWhiteSpace($ExeName) -or -not [string]::IsNullOrWhiteSpace($UserDataDir)) {
851+
$result = Get-WebView2UserDataFolder -ExeName $ExeName -UserDataDir $UserDataDir
859852
Write-Host "User data folders found: $($result.UserDataFolders.Count)" -ForegroundColor Yellow
860853
foreach ($folder in $result.UserDataFolders) {
861854
Write-Host " $folder" -ForegroundColor Yellow
862855
}
863856
Write-Host ""
864857
}
865858
else {
866-
Write-Host "ExeName not provided, skipping user data folder detection" -ForegroundColor Yellow
859+
Write-Host "ExeName and UserDataDir not provided, skipping user data folder detection" -ForegroundColor Yellow
867860
Write-Host ""
868861
}
869862

870863
# Set Crashpad folder path
871-
$script:WatsonMetadataFilePath = $result.CrashpadFolder
864+
$script:CrashpadFolderPath = $result.CrashpadFolder
872865
Write-Host ""
873866

874867
# Start WPR tracing automatically

diagnostics/script.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ To specify a different output directory for the ZIP file, provide the `ZipPath`
3939
4040
```
4141

42-
Sometimes if the script is not able to find the crashpad folder, user can use the `userDataDir` parameter to pass the user data directory to the script.
42+
Sometimes if the script is not able to find the crashpad folder, the user can use the `UserDataDir` parameter to pass the user data directory to the script.
4343

4444
```
45-
.\log_collection_script.ps1 -userDataDir <absolute-path-to-user-data-dir>
45+
.\log_collection_script.ps1 -UserDataDir <absolute-path-to-user-data-dir>
4646
```
4747

4848

0 commit comments

Comments
 (0)