Last active
January 12, 2023 11:54
-
-
Save MalteKiefer/ad121439bcc0213acaf70313657b8c2e to your computer and use it in GitHub Desktop.
Powershell script to add a asset to Snipe-IT.
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
#Pre-reqs | |
param ( | |
[string] $clientName | |
) | |
#Check if NuGet is installed, if not install it | |
if ((Get-PackageProvider -Name NuGet).version -lt 2.8.5.201 ) { | |
try { | |
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Confirm:$False -Force | |
} | |
catch [Exception]{ | |
$_.message | |
exit | |
} | |
} | |
Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted | |
#Check if SnipeITPS Modul is installed, if not installed. If update to latest version | |
if (-not (Get-Module -Name "SnipeitPS")) { | |
try { | |
Install-Module SnipeitPS | |
} | |
catch [Exception]{ | |
$_.message | |
exit | |
} | |
} else { | |
try { | |
Update-Module -Name SnipeitPS -Force | |
} | |
catch [Exception]{ | |
$_.message | |
exit | |
} | |
} | |
#Connection variables - Be sure to create a new user in Snipe-IT with ONLY Read, Create and Edit rights, and then generate API key. | |
$apikey = "API KEY" | |
$url = "SNIPEIT URL" #Base URL, do not add /api/v1/hardware | |
#Create connection with Snipe-IT installation | |
Connect-SnipeitPS -url $url -apiKey $apikey | |
#Computer name | |
$computerName = $env:computername | |
#Serial Number | |
$assetTag = (Get-WmiObject win32_bios -ComputerName $computerName).SerialNumber | |
#Raw Memory Value | |
$cs = (Get-WmiObject -class Win32_ComputerSystem -ComputerName $computerName).TotalPhysicalMemory | |
#Convert to GB Value | |
$memoryAmount = [math]::Ceiling($cs / 1024 / 1024 / 1024) | |
#Model Number | |
$modelno = (Get-WmiObject -class Win32_ComputerSystem -ComputerName $computerName).Model | |
#Manufacturer | |
$manufacturer = (Get-WmiObject -class Win32_ComputerSystem -ComputerName $computerName).Manufacturer | |
#Type of computer | |
$ComputerType = (Get-WmiObject -Class:Win32_ComputerSystem -ComputerName $computerName).PCSystemTypeEx | |
#OS Version | |
$OS = (Get-WmiObject Win32_OperatingSystem -ComputerName $computerName).caption | |
#OS Architecture | |
$OSArch = (Get-WmiObject Win32_OperatingSystem -ComputerName $computerName).osarchitecture | |
#Processor type | |
$cpu = (Get-WmiObject Win32_Processor -ComputerName $computerName).Name | |
#If manufacturer does not exist in Snipe-IT, create. | |
$manufacturerSelection = Get-SnipeitManufacturer | Where-Object {$_.name -like "$manufacturer"} | |
if(([string]::IsNullOrEmpty($manufacturerSelection))){ | |
New-SnipeitManufacturer -Name $manufacturer | |
} | |
#Set default type of asset to be "Other" | |
$Categoryid = 8 | |
#Update type of computer if applicable. | |
if ($ComputerType = 1) | |
{$Categoryid = 7} #Desktop | |
if ($ComputerType = 2) | |
{$Categoryid = 6} #Laptop | |
if ($manufacturer -like "*VMware*") | |
{$Categoryid = 9} | |
#If the model does not exist in Snipe-IT, create. | |
#Fieldset_id 2 refers to the collection of custom fields you also want to populate on each asset. You can get the field ID for your Fieldset by running 'Get-SnipeitFieldset' | |
$modelSelection = Get-SnipeitModel | Where-Object {$_.name -like "*$modelno*"} | |
if(([string]::IsNullOrEmpty($modelSelection))){ | |
$Assetmanufacturer = Get-SnipeitManufacturer | Where-Object {$_.name -like "*$manufacturer*"} | |
New-SnipeitModel -name $modelno -category_id $Categoryid -manufacturer_id $Assetmanufacturer.id -Fieldset_id 2 | |
$modelSelection = Get-SnipeitModel | Where-Object {$_.name -like "*$modelno*"} | |
} | |
#If the client (name pulled from TRMM) does not exist in Snipe-IT, create. | |
#Ensure you have '-clientName ' as a Script Arguement. | |
$companySelection = Get-SnipeitCompany | Where-Object {$_.name -like "*$clientName*"} | |
if(([string]::IsNullOrEmpty($companySelection))){ | |
New-SnipeitCompany -name $clientName | |
$companySelection = Get-SnipeitCompany | Where-Object {$_.name -like "*$clientName*"} | |
} | |
if([string]::IsNullOrWhiteSpace($assetTag)) | |
{ | |
$assetTag = -join ((48..57) + (97..122) | Get-Random -Count 8 | % {[char]$_}) | |
} | |
else { | |
#Check whether asset tag exists in Snipe-IT | |
$assetExists = Get-SnipeitAsset -search $assetTag | |
#If asset exists, update. If not exist, create. | |
#Each custom field needs to be referenced through the hashtable. You can get the field IDs by running 'Get-SnipeitCustomeField' | |
if(([string]::IsNullOrEmpty($assetExists))) | |
{ | |
New-SnipeitAsset -Name $computerName -tag $assetTag -serial $assetTag -company_id $($companySelection.id) -model_id $($modelSelection.id) -Status "2" -customfields @{ "_snipeit_cpu_3" = "$cpu"; "_snipeit_memory_2" = "$memoryAmount GB"; "_snipeit_os_4" = "$OS"; "_snipeit_os_architecture_5" = "$OSArch" } | |
} | |
else { | |
Set-SnipeitAsset -id $($assetExists.id) -Name $computerName -company_id $($companySelection.id) -model_id $($modelSelection.id) -Status "2" -customfields @{ "_snipeit_cpu_3" = "$cpu"; "_snipeit_memory_2" = "$memoryAmount GB"; "_snipeit_os_4" = "$OS"; "_snipeit_os_architecture_5" = "$OSArch" } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment