Created
February 20, 2025 07:47
-
-
Save MarcoGriep88/8664e196376df1f69482923831722c6f to your computer and use it in GitHub Desktop.
Snipe IT Asset Report Script for Excel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[CmdletBinding()] | |
param ( | |
[Parameter()] | |
[string]$URL = "http://localhost/api/v1/hardware", | |
[Parameter()] | |
[string]$APIKEY = "" | |
) | |
Import-Module ImportExcel | |
# Ziel-Excel-Datei definieren | |
$ExcelFilePath = "$($env:TEMP)\ExportierteDaten.xlsx" | |
function Export-DataToExcel { | |
param ( | |
[string]$SheetName, | |
[array]$Data, | |
[string]$FilePath | |
) | |
if (-not (Test-Path $FilePath)) { | |
New-ExcelPackage -Path $FilePath -WorksheetName $SheetName -Force | |
} | |
# Daten in die Tabelle schreiben | |
$Data | Export-Excel -Path $FilePath -WorksheetName $SheetName -TableName "${SheetName}Table" -ClearSheet | |
} | |
function Send-JsonWebRequest { | |
param ( | |
[string]$Url, | |
[string]$ApiKey, | |
[hashtable]$Headers = @{}, | |
[string]$Method = "GET", | |
[object]$Body = $null | |
) | |
$defaultHeaders = @{ | |
"Authorization" = "Bearer $ApiKey" | |
"Content-Type" = "application/json" | |
} | |
# Zusätzliche Header zusammenführen | |
$requestHeaders = $defaultHeaders + $Headers | |
# Konvertiere den Body in JSON, falls er angegeben ist | |
if ($Body -ne $null) { | |
$jsonBody = $Body | ConvertTo-Json -Depth 10 -ErrorAction Stop | |
} else { | |
$jsonBody = $null | |
} | |
# Webrequest absetzen | |
try { | |
$response = Invoke-RestMethod -Uri $Url -Headers $requestHeaders -Method $Method -Body $jsonBody -ErrorAction Stop | |
Write-Output $response | |
} catch { | |
Write-Error "Fehler beim Senden des Webrequests: $_" | |
return $null | |
} | |
} | |
$response = Send-JsonWebRequest -Url $URL -ApiKey $APIKEY | |
Write-Host $response | |
# Daten vorbereiten | |
$computers = $response.rows | | |
Select-Object @{ | |
Name = "ID"; Expression = { $_.id } | |
}, @{ | |
Name = "Name"; Expression = { $_.name } | |
}, @{ | |
Name = "Asset Tag"; Expression = { $_.asset_tag } | |
}, @{ | |
Name = "Serial"; Expression = { $_.serial } | |
}, @{ | |
Name = "Model"; Expression = { $_.model.name } | |
}, @{ | |
Name = "Category"; Expression = { $_.category.name } | |
}, @{ | |
Name = "Company"; Expression = { $_.company.name } | |
}, @{ | |
Name = "Location"; Expression = { $_.location.name } | |
}, | |
@{ | |
Name = "rtd_location"; Expression = { $_.rtd_location.name } | |
} | |
Write-Host $computers | |
Export-DataToExcel -SheetName "Report" -Data $computers -FilePath $ExcelFilePath | |
Write-Host "Die Daten wurden erfolgreich exportiert: $ExcelFilePath" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment