0 Replies Latest reply on Jan 22, 2018 12:48 PM by cbass

    PowerShell/MBSDK - Cleanup devices migrated from Old Core to New Core

    Rookie

      Recently we had the opportunity to migrate to a new core.  During that project we developed a PowerShell script to delete devices that had been migrated to the new core from the old core.  Below is the script we used and it can probably be modified to work for other things as well. It assumes that you are running PowerShell with an account that has rights to both cores.  It collects all the devices on each core, then check each legacy core device against the new core device list and deletes it from the legacy core if it's on both.

       

      <#

          Clear out devices migrated to new core from legacy core

          Assumes that script is run as account that has rights to delete devices in legacy core

      #>

       

      function GetCoreAllDevices($coreName)

      {

          $localResults = @()

          try

          {

              $ldws = New-WebServiceProxy -Uri http://$coreName/MBSDKService/MsgSDK.asmx?WSD -UseDefaultCredential -ErrorAction Stop

       

              $testAccess = $ldws.ResolveScopeRights()

              if($testAccess)

              {

                  $devices = $ldws.ListMachines("").Devices

                  if($devices.Count -gt 0)

                  {

                      foreach($device in $devices)

                      {

                          $info = New-Object psobject

                          $info | Add-Member -MemberType NoteProperty -Name DeviceName -Value $device.DeviceName

                          $info | Add-Member -MemberType NoteProperty -Name DeviceID -Value $device.GUID

                          $localResults += $info

                      }

                  }

              }

              else

              { Write-host "No Access to LANDesk: $coreName" -ForegroundColor Red  }

          }

          catch

          {

              Write-host "Error accessing LANDesk: $coreName" -ForegroundColor Red

              $lerrMsg = $Error[0].ToString() + $Error[0].InvocationInfo.PositionMessage

              Write-Host $lerrMsg -ForegroundColor Red

          }

          $ldws.Dispose()

          Write-Host "Completed core pull from:"$coreName

          return $localResults

      }

       

      function PurgeLegacyDevice()

      {

      [CmdletBinding()]

      Param(

          [Parameter(Mandatory=$true,Position=1)]

          [string]$computerName,

          [Parameter(Mandatory=$true,Position=2)]

          [string]$deviceGUID)

          $blnExists = $false

          Write-Host $computerName "exists." -NoNewline -ForegroundColor Green

          try

          {

              $ldws = New-WebServiceProxy -Uri http://$Global:LegacyCore/MBSDKService/MsgSDK.asmx?WSD -UseDefaultCredential -ErrorAction Stop

       

              $testAccess = $ldws.ResolveScopeRights()

              if($testAccess)

              {

                  try

                  {    

                      $deleteResult = $ldws.DeleteComputerByGUID($deviceGUID)

                      if($deleteResult -eq 1)

                      { Write-Host " Successfully deleted." -ForegroundColor Green }

                      else

                      {

                          Write-Host " Failed to delete." -ForegroundColor Red

                          Write-Host $computerName"`t"$deviceGUID -ForegroundColor Red                

                      }

                  }

                  catch

                  { Write-host "Error deleting $computerName" -ForegroundColor Red }

              }

              else

              { Write-host "No Access to LANDesk" -ForegroundColor Red  }

          }

          catch

          {

              Write-host "Error accessing LANDesk: $coreName" -ForegroundColor Red

              $lerrMsg = $Error[0].ToString() + $Error[0].InvocationInfo.PositionMessage

              Write-Host $lerrMsg -ForegroundColor Red

          }

          $ldws.Dispose()

      }

       

      #FQDN for the Core your are migrating from (Legacy)

      $Global:LegacyCore = "LegacyCore.com"

      #FQDN for the Core your are migrating to (New)

      $Global:NewCore = "NewCore.com"

      $Global:NewCoreDevices = @()

      $Global:LegacyDevices = @()

       

      $Global:LegacyDevices = GetCoreAllDevices $Global:LegacyCore

      $Global:NewCoreDevices = GetCoreAllDevices $Global:NewCore

       

      <# Compares the hash tables to see if the device exists on both cores, if so delete from legacy core #>

      $i = 1

      $listCount = $Global:NewCoreDevices.Count

       

      foreach($NewDevice in $Global:NewCoreDevices)

      {

          Write-Progress -Activity Checking -Status "Progress: $i of $listCount" -PercentComplete ($i/$listCount*100)

          $i += 1

          if($Global:LegacyDevices.DeviceName -contains $NewDevice.DeviceName)

          {

             $legacyDeviceID = ($Global:LegacyDevices | Where-Object {$_.DeviceName -eq $NewDevice.DeviceName}).DeviceID

             Write-Host $NewDevice.DeviceName"`t"$legacyDeviceID

             PurgeLegacyDevice $NewDevice.DeviceName $legacyDeviceID

          }

       

      }