You are not logged in.

Announcement

 Téléchargez la dernière version stable de GLPI      -     Et vous, que pouvez vous faire pour le projet GLPI ? :  Contribuer
 Download last stable version of GLPI                      -     What can you do for GLPI ? :  Contribute

#1 2024-02-08 14:11:01

Maschik1234
Member
Registered: 2023-07-03
Posts: 25

Delete the network port via the API

I would like to automate the asset creation with Powershell. However, I am still having problems with the network port part. The Mac addresses that are read from the local computer via Powershell are automatically entered in the asset, but if there are changes to the MAC address, the outdated MACs remain linked in the asset. Normally I can delete the ID with which the component connects to the asset so that the component is removed from the asset. However, I cannot do this with the network ports, as the network ports are not components but are created directly in the asset.

I have tried it in the following white way:

$PowershellNetAdapter = Get-CimInstance -ClassName Win32_NetworkAdapter | Select-Object MACAddress, NetConnectionID | Where-Object {($_.NetConnectionID -and $_.NetConnectionID -ne "") -and ($_.MACAddress -and $_.MACAddress -ne "")}
$NetworkPortsName = $PowershellNetAdapter.NetConnectionID
$NetworkPortsMac = $PowershellNetAdapter.MACAddress

function LinkNetwork {
    param (
        $NetworkPortsName,
        $NetworkPortsMac
    )
    $LinkData = @{
        "input" = @{
            "items_id" = $assetId
            "itemtype" = "Computer"
            "name" = $NetworkPortsName
            "mac" = $NetworkPortsMac
            "instantiation_type" = "NetworkPortEthernet"
            "entities_id" = $Entities_id
        }
    }   
    $SearchUrl = $GLPI_Link + "/Computer/"+ $assetId +"/NetworkPort/"
    $responseSearch = Invoke-RestMethod -Uri $SearchUrl -Method GET -Headers $Headers
    $SearchItem = $responseSearch.mac

    $existingItem = $SearchItem | Where-Object { $_ -eq $NetworkPortsMac }
    
    if (!$existingItem) {
        $LinkUrl = $GLPI_Link + "/NetworkPort/"
        $responseLink = Invoke-RestMethod -Uri $LinkUrl -Method POST -Headers $Headers -Body ($LinkData | ConvertTo-Json)
        $linkId = $responseLink.id
        Write-Output "Network wurde verlinkt mit der ID:" $linkId
    }
    else {
        Write-Output "Network existiert bereits für dieses Asset."
    }
}

function UpdateAndCleanNetworkPorts {
    param (
        $assetId,
        $currentAdapters
    )

    $SearchUrl = $GLPI_Link + "/Computer/"+ $assetId +"/NetworkPort/"
    $existingPortsResponse = Invoke-RestMethod -Uri $SearchUrl -Method GET -Headers $Headers
    $existingNetworkPorts = $existingPortsResponse.mac
    $currentMacLookup = @{}
    foreach ($adapter in $currentAdapters) {
        $currentMacLookup[$adapter.MACAddress] = $adapter
    }

    foreach ($existingPort in $existingPortsResponse) {
        if ($null -ne $existingPort.mac -and $existingPort.mac -ne "") {
            if (-not $currentMacLookup.ContainsKey($existingPort.mac)) {
                $deleteUrl = $GLPI_Link + "/NetworkPort/" + $($existingPort.id)
                Invoke-RestMethod -Uri $deleteUrl -Method DELETE -Headers $Headers
                Write-Output "Nicht aktuelle Netzwerkport-MAC-Adresse entfernt: $($existingPort.mac)"
            }
        }
    }
    foreach ($adapter in $currentAdapters) {
        if (-not $existingNetworkPorts.mac -contains $adapter.MACAddress) {
            LinkNetwork -NetworkPortsName $adapter.NetConnectionID -NetworkPortsMac $adapter.MACAddress
        }
    }
}


UpdateAndCleanNetworkPorts -assetId $assetId -currentAdapters $PowershellNetAdapter

This is only a small part of the Powershell script.

In any case, it ensures that the new Macs are added that it reads from Powershell and the old ones are also removed. With the API query I can see that the old ones have been removed, but in the tab in the asset I can still see the network port with the MAC address. Am I doing something wrong? Does anyone have any tips for me?

Offline

Board footer

Powered by FluxBB