Skip to content

Instantly share code, notes, and snippets.

@mavhc
Created January 25, 2024 11:08
Show Gist options
  • Save mavhc/dda09504a91f5007d4202e0f7ea4c5b0 to your computer and use it in GitHub Desktop.
Save mavhc/dda09504a91f5007d4202e0f7ea4c5b0 to your computer and use it in GitHub Desktop.
code to automate inventory
$apikey = "..."
$url = "https://..."
#Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
write-output "Install NuGet"
Install-PackageProvider NuGet -Force
if(-not (Get-Module SnipeitPS -ListAvailable)){
write-output "Install SnipeitPS"
Install-Module SnipeitPS -Force
}
write-output "Update SnipeitPS"
Update-Module SnipeitPS
write-output "Import SnipeitPS"
Import-Module SnipeitPS
#write-output (get-help set-snipeitasset -full)
write-output "Connect to SnipeIT website"
Connect-SnipeitPS -URL $url -apiKey $apikey
# if calling remotely perhaps
#$computerName = (Get-WmiObject -class Win32_ComputerSystem -ComputerName $computerName).name
write-output "Get details"
$computerName = $env:COMPUTERNAME
$assetTag = (Get-WmiObject win32_bios).SerialNumber
$assetExists = Get-SnipeitAsset -search $assetTag
$cs = (Get-WmiObject -class Win32_ComputerSystem -ComputerName $computerName).TotalPhysicalMemory
$memoryAmount = [math]::Ceiling($cs / 1024 / 1024 / 1024)
$modelno = (Get-WmiObject -class Win32_ComputerSystem -ComputerName $computerName).Model
$manufacturer = (Get-WmiObject -class Win32_ComputerSystem -ComputerName $computerName).Manufacturer
$SystemType = (Get-WmiObject -class Win32_ComputerSystem -ComputerName $computerName).PCSystemType
$OS = (Get-WmiObject Win32_OperatingSystem -ComputerName $computerName).caption
$OSArch = (Get-WmiObject Win32_OperatingSystem -ComputerName $computerName).osarchitecture
$cpu = (Get-WmiObject Win32_Processor -ComputerName $computerName).Name
if ($cpu -like "*i5*"){
$cpuType = "Intel i5"
}
elseif ($cpu -like "*i7*"){
$cpuType = "Intel i7"
}
elseif ($cpu -like "*i3*"){
$cpuType = "Intel i3"
}
elseif ($cpu -like "*Ryzen 5*"){
$cpuType = "AMD Ryzen 5"
}
$modelSelection = Get-SnipeitModel | Where-Object {$_.name -like "*$modelno*"}
if(([string]::IsNullOrEmpty($modelSelection))) {
$NewModelNumber = "$($modelno) $($cpuType)/$($memoryAmount)GB"
$NewModel = "$($modelno)"
if ($NewModel = "HP ProBook 430 G8 Notebook PC") {$NewModel = "HP ProBook 430 G8"}
$Assetmanufacturer = Get-SnipeitManufacturer | Where-Object {$_.name -like "*$manufacturer*"}
$ComputerType = (Get-WmiObject -Class:Win32_ComputerSystem -ComputerName $computerName).PCSystemTypeEx
If($ComputerType = 2){$Categoryid = 4} # mobile
If($ComputerType = 1){$Categoryid = 3} # desktop
New-SnipeitModel $NewModel -category_id $Categoryid -manufacturer_id $Assetmanufacturer.id -fieldset_id 5
$modelSelection = Get-SnipeitModel | Where-Object {$_.name -like "*$modelno*"}
}
Write-Host $modelno
$assetExists = Get-SnipeitAsset -search $assetTag
if(([string]::IsNullOrEmpty($assetExists)))
{
New-SnipeitAsset -Name $computerName -tag $assetTag -Model_id $modelSelection.id -Status "2"
} else {
Write-Host "Asset Exist $($assetExists.id) : $($computerName) $($assetTag) $($SystemType) $($manufacturer) $($OS)"
Set-SnipeitAsset -id $($assetExists.id) -Name $computerName -Model_id $modelSelection.id -Status "2" -CustomFields @{ "OS" = "$($OS)"}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment