Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error handling #8

Merged
merged 4 commits into from
Oct 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
local.settings.json
tenants.cache.json
chocoapps.cache
*.log
*.log
.vscode/
35 changes: 22 additions & 13 deletions GraphHelper.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ function Get-GraphToken($tenantid, $scope, $AsApp, $AppID, $refreshToken, $Retur
return $header
}

function Log-Request ($message, $tenant, $API, $user, $sev) {
$username = ([System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($user)) | ConvertFrom-Json).userDetails
$date = (Get-Date).ToString('s')
$LogMutex = New-Object System.Threading.Mutex($false, "LogMutex")
if (!$username) { $username = "CIPP" }
if (!$tenant) { $tenant = "None" }
$logdata = "$($date)|$($tenant)|$($API)|$($message)|$($username)|$($sev)"
if ($LogMutex.WaitOne(1000)) {
$logdata | Out-File -Append -path "$((Get-Date).ToString('MMyyyy')).log"
}
$LogMutex.ReleaseMutex()
}

function New-GraphGetRequest ($uri, $tenantid, $scope, $AsApp) {
$TenantList = Get-Content 'Tenants.cache.json' -ErrorAction SilentlyContinue | ConvertFrom-Json
$Skiplist = Get-Content "ExcludedTenants" | ConvertFrom-Csv -Delimiter "|" -Header "Name", "User", "Date"
Expand All @@ -52,7 +65,15 @@ function New-GraphGetRequest ($uri, $tenantid, $scope, $AsApp) {
#not a fan of this, have to reconsider and change. Seperate function?
if ($tenantid -in $tenantlist.defaultdomainname -or $uri -like "https://graph.microsoft.com/beta/contracts?`$top=999" -or $uri -like "*/customers/*") {
$ReturnedData = do {
$Data = (Invoke-RestMethod -Uri $nextURL -Method GET -Headers $headers -ContentType "application/json; charset=utf-8")
try {
$Data = (Invoke-RestMethod -Uri $nextURL -Method GET -Headers $headers -ContentType "application/json; charset=utf-8")
}
catch {
$errorMessage = "Status Code: $($_.Exception.Response.StatusCode.value__) - Status Description: $($_.ErrorDetails.Message)"
Log-Request $errorMessage $tenantid "GraphRequest" $null "High"
throw $_
}

if ($data.value) { $data.value } else { ($Data) }
$nextURL = $data.'@odata.nextLink'
} until ($null -eq $NextURL)
Expand Down Expand Up @@ -84,18 +105,6 @@ function New-GraphPOSTRequest ($uri, $tenantid, $body, $type, $scope, $AsApp) {
Write-Error "Not allowed. You cannot manage your own tenant or tenants not under your scope"
}
}
Function Log-request ($message, $tenant, $API, $user, $sev) {
$username = ([System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($user)) | ConvertFrom-Json).userDetails
$date = (Get-Date).ToString('s')
$LogMutex = New-Object System.Threading.Mutex($false, "LogMutex")
if (!$username) { $username = "CIPP" }
if (!$tenant) { $tenant = "None" }
$logdata = "$($date)|$($tenant)|$($API)|$($message)|$($username)|$($sev)"
if ($LogMutex.WaitOne(1000)) {
$logdata | Out-File -Append -path "$((Get-Date).ToString('MMyyyy')).log"
}
$LogMutex.ReleaseMutex()
}
function convert-skuname($skuname, $skuID) {
$ConvertTable = Import-Csv Conversiontable.csv
if ($skuname) { $ReturnedName = ($ConvertTable | Where-Object { $_.String_Id -eq $skuname } | Select-Object -Last 1).'Product_Display_Name' }
Expand Down