Hi there,
I am trying to setup a system for my work environment and im having some trouble running the api.
I have created a test instance in a different environment prior to being approved to roll it out for production and my script that I had done up for asset import was working perfectly.
Since the move to a production server the API seems to have lost the ability to write. I run the script and the read function works just fine, it returns the asset ID if it exists or nothing if it does not. as soon as it tries to write to the database it fails.
I have gone through database permissions and everything seems to be inline with the initial setup guide.
Any help would be very appreciated.
For reference, the script (redacted) that I have been running.
$apiKey = "jnhILUEHNRjknhfwe980u45ohjnwl"
# Base site URL
$ITFlow_url = "itflow.relevantaddress.com.au"
# Read Module
$read_module = "/api/v1/assets/read.php"
$list = Get-ADComputer -filter * -Properties * -SearchBase "releventOUinformation" | select Name,Created,ipv4address,OperatingSystem
ForEach ($Client in $list)
{
$server = $Client.Name
$pcCreated = $Client.Created
$createDate = $pcCreated.ToString("yyyy-MM-dd")
$pcipv4 = $Client.ipv4address
$pcOS = $Client.OperatingSystem
if (Test-Connection -count 1 -computer $server -quiet){
Write-Host "Collecting information from" $server -ForegroundColor Green
try
{
[wmi]$sysInfo = get-wmiobject Win32_ComputerSystem -Namespace "root\CIMV2" -ComputerName $server -ErrorAction Stop
[wmi]$bios = Get-WmiObject Win32_BIOS -Namespace "root\CIMV2" -computername $server
$asset_mac = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Namespace "root\CIMV2" -computername $server | where {$_.DHCPEnabled -ne $null -and $_.DefaultIPGateway -ne $null}).macaddress | Select-Object -First 1
$asset_name = $server
$asset_make = $sysInfo.Manufacturer
$asset_model = $sysInfo.Model
$asset_serial = $bios.SerialNumber
}
catch [Exception]
{
Write-Host "Error Checking WMI Objects on $server, skipping to next" -ForegroundColor Red
Continue
}
finally
{
#
}
# Search all assets in ITFlow by name to see if this asset already exists
$uri_read = $ITFlow_url + $read_module + "?api_key=" + $apiKey + "&asset_name=" + $asset_name
# Check if PC exists in ITFlow database
$exists = Invoke-RestMethod -Method GET -Uri $uri_read
$asset_id = $exists.data.asset_id
#$asset_location_id = $ITFlow_location_ID
#$asset_network_id = $ITFlow_network_ID
# If the asset exists update it, if not create it.
if ($exists.success -eq "False") {
$module = "/api/v1/assets/create.php"
Write-Host "Asset does not exist - Creating..."
} else {
if ($exists.success -eq "True") {
$module = "/api/v1/assets/update.php"
Write-Host "ITFlow IDs - Location ID: " $exists.data.asset_location_id "Network ID: " $exists.data.asset_network_id
if ( $ITFlow_location_ID -eq "0" ) {
$asset_location_id = $exists.data.asset_location_id
}
if ( $ITFlow_network_ID -eq "0" ) {
$asset_network_id = $exists.data.asset_network_id
}
Write-Host "Asset already exists - Updating..."
} else {
Write-Host "API connection error. Aborting..."
Exit
}
}
$body = @"
{
"api_key" : "$apiKey",
"asset_name" : "$asset_name",
"asset_type" : "Desktop",
"asset_make" : "$asset_make",
"asset_model" : "$asset_model",
"asset_serial" : "$asset_serial",
"asset_os" : "$pcOS",
"asset_ip" : "$pcipv4",
"asset_mac" : "$asset_mac",
"asset_install_date" : "$createDate",
"asset_status" : "Deployed",
"client_id" : "$ITFlow_client_ID",
"asset_location_id" : "$asset_location_id",
"asset_network_id" : "$asset_network_id",
"asset_id" : "$asset_id"
}
"@
Write-Host "Importing information from" $asset_name "ID:" $asset_id
#
$uri_write = $ITFlow_url + $module
$write = Invoke-RestMethod -Method Post -Uri $uri_write -Body $body
if ($exists.success -eq "True" -And $write.success -eq "True") {
Write-Host "Asset updated."
}
if ($exists.success -eq "True" -And $write.success -eq "False") {
Write-Host "No changes to update."
}
if ($exists.success -eq "False" -And $write.success -eq "True") {
Write-Host "Asset created."
}
if ($exists.success -eq "False" -And $write.success -eq "False") {
Write-Host "Failed to create asset."
}
}
else
{
Write-Host "Error communicating with $server, skipping" -ForegroundColor Red
}
}
Write-Host " ***Hardware Asset Import Completed*** " -ForegroundColor Green