Tee to public desktop log file

This commit is contained in:
Ryan McCallum 2023-07-10 14:16:08 -04:00
Родитель 63e8b1c7ed
Коммит bd0773eb0e
1 изменённых файлов: 40 добавлений и 29 удалений

Просмотреть файл

@ -49,7 +49,15 @@
# Initialize script
. .\src\windows\common\setup\init.ps1
. .\src\windows\common\helpers\Get-Disk-Partitions.ps1
Log-Output "START: Running script win-collect-attached-disk-logs"
# Declare variables
$scriptStartTime = get-date -f yyyyMMddHHmmss
$scriptPath = split-path -path $MyInvocation.MyCommand.Path -parent
$scriptName = (split-path -path $MyInvocation.MyCommand.Path -leaf).Split('.')[0]
$logFile = "$env:PUBLIC\Desktop\$($scriptName).log"
$scriptStartTime | Tee-Object -FilePath $logFile -Append
Log-Output "START: Running script win-collect-attached-disk-logs" | Tee-Object -FilePath $logFile -Append
try {
# Declaring variables
@ -65,7 +73,7 @@ try {
$logArray = @()
$driveLetters = @()
Log-Output "#01 - Collect log manifest files"
Log-Output "#01 - Collect log manifest files" | Tee-Object -FilePath $logFile -Append
# Download Windows manifest files from Github
# https://github.com/Azure/azure-diskinspect-service/tree/master/pyServer/manifests/windows
@ -84,10 +92,10 @@ try {
ForEach ( $url in $urls) {
try {
$githubContent += (New-Object System.Net.WebClient).DownloadString($url).Split([Environment]::NewLine)
Log-Output "Grabbed $($url)"
Log-Output "Grabbed $($url)" | Tee-Object -FilePath $logFile -Append
}
catch {
Log-Warning "Error for $($url)"
Log-Warning "Error for $($url)" | Tee-Object -FilePath $logFile -Append
}
}
@ -97,7 +105,7 @@ try {
$logArray = $removeKeywords | Where-Object { $_ -notmatch "/Boot/BCD" } | Where-Object { $_ -notmatch "echo," } | Where-Object { ![String]::IsNullOrWhiteSpace($_) } | Sort-Object
# Make sure the disk is online
Log-Output "#02 - Bringing partition(s) online if present"
Log-Output "#02 - Bringing partition(s) online if present" | Tee-Object -FilePath $logFile -Append
$disk = Get-Disk -ErrorAction Stop | Where-Object { $_.FriendlyName -eq 'Msft Virtual Disk' }
$disk | Set-Disk -IsOffline $false -ErrorAction SilentlyContinue
@ -111,10 +119,10 @@ try {
# Log drive letters
if ($fixedDrives) {
Log-Output "Attached volumes: $($fixedDrives -join ", ")"
Log-Output "Attached volumes: $($fixedDrives -join ", ")" | Tee-Object -FilePath $logFile -Append
}
if ($mappedDrives) {
Log-Output "Mapped drives: $($mappedDrives -join ", ")"
Log-Output "Mapped drives: $($mappedDrives -join ", ")" | Tee-Object -FilePath $logFile -Append
}
# Collect drive letters except for root drive of Rescue VM
@ -133,7 +141,7 @@ try {
# Create subfolder named after the current time in UTC
$timeFolder = New-Item -Path $folder.ToString() -Name "$($scriptStartTimeUTC)_UTC" -ItemType "directory"
Log-Output "#03 - Copy log files to $($timeFolder.ToString())"
Log-Output "#03 - Copy log files to $($timeFolder.ToString())" | Tee-Object -FilePath $logFile -Append
# Scan all collected partitions to determine if BCD or OS partition
ForEach ($drive in $driveLetters ) {
@ -165,7 +173,7 @@ try {
# If Boot partition found grab BCD store
if ( $isBcdPath ) {
Log-Output " BCD store on $($drive)"
Log-Output " BCD store on $($drive)" | Tee-Object -FilePath $logFile -Append
$bcdParentFolderName = "bcd"
$bcdFileName = $bcdPath.Split("\")[-1]
@ -176,20 +184,20 @@ try {
$folder = New-Item -Path $subFolder -Name $bcdParentFolderName -ItemType "directory"
}
Log-Output "Copy $($bcdPath) to $($subFolder.ToString())"
Log-Output "Copy $($bcdPath) to $($subFolder.ToString())" | Tee-Object -FilePath $logFile -Append
Copy-Item -Path $bcdPath -Destination "$($folder)\$($bcdFileName)" -Recurse
"$($bcdPath)" | Out-File -FilePath $logFile -Append
Log-Output "Print $($bcdPath) to $($subFolder.ToString())"
"$($bcdPath)" | Tee-Object -FilePath $logFile -Append
Log-Output "Print $($bcdPath) to $($subFolder.ToString())" | Tee-Object -FilePath $logFile -Append
bcdedit /store $bcdPath /enum /v > "$($folder)\$($bcdFileName).txt"
"$($bcdFileName).txt" | Out-File -FilePath $logFile -Append
"$($bcdFileName).txt" | Tee-Object -FilePath $logFile -Append
}
else {
Log-Warning "No BCD store on $($drive)"
Log-Warning "No BCD store on $($drive)" | Tee-Object -FilePath $logFile -Append
}
# If Windows partition found grab log files
if ( $isOsPath ) {
Log-Output "OS logs on $($drive)"
Log-Output "OS logs on $($drive)" | Tee-Object -FilePath $logFile -Append
foreach ($logName in $logArray) {
$logLocation = "$($drive):$($logName)"
@ -206,7 +214,7 @@ try {
}
}
Log-Output "Copy Windows OS logs from $($drive) to $($subFolder.ToString())"
Log-Output "Copy Windows OS logs from $($drive) to $($subFolder.ToString())" | Tee-Object -FilePath $logFile -Append
$collectedLogArray | ForEach-Object {
# Retain directory structure while replacing partition letter
try {
@ -281,40 +289,40 @@ try {
$healthSignalsFile = "$($subFolder.ToString())\healthSignals_latest.json"
$jsonResult = $matches['content']
$jsonConversion = ConvertFrom-Json $jsonResult
$jsonConversion | ConvertTo-Json | Out-File -FilePath $healthSignalsFile -Append
$jsonConversion | ConvertTo-Json | Tee-Object -FilePath $healthSignalsFile -Append
}
else {
Log-Warning "Could not generate Health Signals log, confirm $($subFolder)\WindowsAzure\Logs\TransparentInstaller.log has Health Signals logged"
Log-Warning "Could not generate Health Signals log, confirm $($subFolder)\WindowsAzure\Logs\TransparentInstaller.log has Health Signals logged" | Tee-Object -FilePath $logFile -Append
}
}
else {
Log-Warning "Could not generate Health Signals log, confirm $($subFolder)\WindowsAzure\Logs\TransparentInstaller.log exists"
Log-Warning "Could not generate Health Signals log, confirm $($subFolder)\WindowsAzure\Logs\TransparentInstaller.log exists" | Tee-Object -FilePath $logFile -Append
}
}
catch {
Log-Warning "Could not generate Health Signals log, confirm $($subFolder)\WindowsAzure\Logs\TransparentInstaller.log exists"
Log-Warning "Could not generate Health Signals log, confirm $($subFolder)\WindowsAzure\Logs\TransparentInstaller.log exists" | Tee-Object -FilePath $logFile -Append
}
}
else {
Log-Warning "No OS logs on $($drive)"
Log-Warning "No OS logs on $($drive)" | Tee-Object -FilePath $logFile -Append
}
}
}
# Include tree of subdirectory
try {
Log-Output "Generating tree file"
Log-Output "Generating tree file" | Tee-Object -FilePath $logFile -Append
tree $timeFolder /f /a | Out-File -FilePath $treeFile -Append
}
catch {
Log-Warning "Could not generate tree file"
Log-Warning "Could not generate tree file" | Tee-Object -FilePath $logFile -Append
}
# Include size of partitions: https://devblogs.microsoft.com/scripting/inventory-drive-types-by-using-powershell/
try {
Log-Output "Collecting partition sizes"
Log-Output "Collecting partition sizes" | Tee-Object -FilePath $logFile -Append
$hash = @{
2 = "Removable disk"
3 = "Fixed local disk"
@ -324,22 +332,22 @@ try {
Get-CimInstance -Class CIM_LogicalDisk | Select-Object @{Name = "Size(GB)"; Expression = { ($_.size / 1gb).tostring("#.###") } }, @{Name = "Free Space(GB)"; Expression = { ($_.freespace / 1gb).tostring("#.###") } }, @{Name = "Free (%)"; Expression = { "{0,6:P0}" -f (($_.freespace / 1gb) / ($_.size / 1gb)) } }, DeviceID, @{Name = "DriveType (Type)"; Expression = { $hash.item([int]$_.DriveType) } } | Where-Object { ($_.DeviceID -split ":")[0] -in $driveLetters } | Format-Table | Out-File -FilePath $partitionSizeFile -Append
}
catch {
Log-Warning "Could not collect partition sizes"
Log-Warning "Could not collect partition sizes" | Tee-Object -FilePath $logFile -Append
}
# Zip files
try {
Log-Output "#04 - Creating zipped archive $($timeFolder.Name).zip"
Log-Output "#04 - Creating zipped archive $($timeFolder.Name).zip" | Tee-Object -FilePath $logFile -Append
$compress = @{
Path = $timeFolder
CompressionLevel = "Fastest"
DestinationPath = "$($desktopFolderPath)$($timeFolder.Name).zip"
}
Compress-Archive @compress
Log-Output "END: Please collect zipped log file $($desktopFolderPath)$($timeFolder.Name).zip from desktop"
Log-Output "END: Please collect zipped log file $($desktopFolderPath)$($timeFolder.Name).zip from desktop" | Tee-Object -FilePath $logFile -Append
}
catch {
Log-Warning "Could not generate ZIP file, collect logs from CaseFiles folder on desktop instead"
Log-Warning "Could not generate ZIP file, collect logs from CaseFiles folder on desktop instead" | Tee-Object -FilePath $logFile -Append
}
return $STATUS_SUCCESS
}
@ -350,7 +358,10 @@ catch {
Please confirm a Windows OS disk is attached as a data disk
You can also map a network drive with:
az vm run-command invoke --command-id RunPowerShellScript --name vm --resource-group rg --scripts `"net use <DRIVE
LETTER>: \\<PRIVATE_IP_OF_VM_ON_VNET>\c$ /persistent:no /user:<USERNAME> <PASSWORD>`""
LETTER>: \\<PRIVATE_IP_OF_VM_ON_VNET>\c$ /persistent:no /user:<USERNAME> <PASSWORD>`"" | Tee-Object -FilePath $logFile -Append
throw $_
return $STATUS_ERROR
}
$scriptEndTime = get-date -f yyyyMMddHHmmss
$scriptEndTime | Tee-Object -FilePath $logFile -Append