Skip to content

Commit

Permalink
Append investigation folder with seconds to avoid duplicate folder cr…
Browse files Browse the repository at this point in the history
…eation error.
  • Loading branch information
jonnybottles committed Jan 11, 2025
1 parent 74d10dd commit 6a7a0be
Showing 1 changed file with 26 additions and 22 deletions.
48 changes: 26 additions & 22 deletions Hawk/internal/functions/Initialize-HawkGlobalObject.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -91,36 +91,40 @@
param([string]$RootPath)

$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"

Write-Information "[$timestamp UTC] - [ACTION] - Connecting to Microsoft Graph"

try {
# Test Graph connection
$null = Test-GraphConnection
# Test Graph connection first to see if we're already connected
try {
$null = Get-MgOrganization -ErrorAction Stop
Write-Information "[$timestamp UTC] - [INFO] - Already connected to Microsoft Graph"
}
catch {
# Only show connecting message if we actually need to connect
Write-Information "[$timestamp UTC] - [ACTION] - Connecting to Microsoft Graph"
$null = Test-GraphConnection
Write-Information "[$timestamp UTC] - [INFO] - Connected to Microsoft Graph Successfully"
}

# If successful, display a success message
Write-Information "[$timestamp UTC] - [INFO] - Connected to Microsoft Graph Successfully"
}
catch {
# If it fails, display an error message
Write-Error "[$timestamp UTC] - [ERROR] - Failed to connect to Microsoft Graph"
}
# Get tenant name
$TenantName = (Get-MGDomain -ErrorAction Stop | Where-Object { $_.isDefault }).ID
[string]$FolderID = "Hawk_" + $TenantName.Substring(0, $TenantName.IndexOf('.')) + "_" + (Get-Date).ToUniversalTime().ToString("yyyyMMdd_HHmmss")

# Get tenant name
$TenantName = (Get-MGDomain -ErrorAction Stop | Where-Object { $_.isDefault }).ID
[string]$FolderID = "Hawk_" + $TenantName.Substring(0, $TenantName.IndexOf('.')) + "_" + (Get-Date).ToUniversalTime().ToString("yyyyMMdd_HHmm")
$FullOutputPath = Join-Path $RootPath $FolderID

$FullOutputPath = Join-Path $RootPath $FolderID
if (Test-Path $FullOutputPath) {
Write-Information "[$timestamp UTC] - [ERROR] - Path $FullOutputPath already exists"
}
else {
Write-Information "[$timestamp UTC] - [ACTION] - Creating subfolder $FullOutputPath"
$null = New-Item $FullOutputPath -ItemType Directory -ErrorAction Stop
}

if (Test-Path $FullOutputPath) {
Write-Information "[$timestamp UTC] - [ERROR] - Path $FullOutputPath already exists"
Return $FullOutputPath
}
else {
Write-Information "[$timestamp UTC] - [ACTION] - Creating subfolder $FullOutputPath"
$null = New-Item $FullOutputPath -ItemType Directory -ErrorAction Stop
catch {
# If it fails at any point, display an error message
Write-Error "[$timestamp UTC] - [ERROR] - Failed to create logging folder: $_"
}

Return $FullOutputPath
}

Function Set-LoggingPath {
Expand Down

0 comments on commit 6a7a0be

Please sign in to comment.