Advertisement
jahnow

Import AD Contacts into a SharePoint List

Jul 19th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function Get-Inventory {
  2. $strFilter = "computer"
  3.  
  4. $objDomain = New-Object System.DirectoryServices.DirectoryEntry
  5.  
  6. $objSearcher = New-Object System.DirectoryServices.DirectorySearcher
  7. $objSearcher.SearchRoot = "LDAP://OU=X,DC=Y,DC=Z" //Update that line to mauch that desired OU CN
  8. $objSearcher.SearchScope = "Subtree"
  9. $objSearcher.PageSize = 1000
  10. $objSearcher.PropertiesToLoad.Add("Name") | Out-Null
  11.  
  12. $objSearcher.Filter = "(objectCategory=$strFilter)"
  13.  
  14. $colResults = $objSearcher.FindAll()
  15. $global:computer_array = @()
  16.  
  17.      
  18.          
  19. foreach ($objResult in $colResults) {
  20.      If (Test-Connection -ComputerName $objResult.Properties.name -Quiet -Count 1)
  21.      {
  22.      Try
  23.      {
  24.          $CS = gwmi win32_computersystem -ComputerName $objResult.Properties.name |
  25.             select name,model
  26.          if ($Error[0].Exception -match "HRESULT: 0x800706BA")
  27.          {
  28.          $Error.Clear()
  29.          continue
  30.          }
  31.         $OS = gwmi win32_operatingsystem -ComputerName $objResult.Properties.name |
  32.           select caption,version,osarchitecture,serialnumber
  33.          if ($Error[0].Exception -match "HRESULT: 0x800706BA")
  34.          {
  35.          $Error.Clear()
  36.          continue
  37.          }        
  38.          $obj = New-Object PSObject -Property @{
  39.          Name=$CS.name
  40.          Model=$CS.Model
  41.          OS=$OS.caption
  42.          Architecture = $OS.osArchitecture
  43.          SerialNumber = $OS.SerialNumber
  44.          Version = $OS.version
  45.          }
  46.        
  47.      
  48.  
  49.          $global:computer_array += $obj
  50.       }
  51.       catch [Exception]
  52.       {
  53.         Write-Host "Error $objResult.Properties.name"
  54.       }
  55.       }
  56.       Else { "Unable to contact $strComputerName" }
  57.  }
  58.   Write-Output $computer_array
  59. }#End Function            
  60.  
  61. Function Import-ListItems {            
  62.  
  63.    Param(
  64.    $site = "http://intranet", //update to list URL
  65.    $spweb = (Get-SPWeb -Identity $site),
  66.    $List = $spweb.lists["Inventory"]
  67.   )            
  68.  
  69.    Foreach($row in $computer_array) {
  70.      $item = $List.items.add()
  71.      $item["Title"] = $row.Name
  72.      $item["Model"] = $row.Model
  73.      $item["Architecture"]= $row.Architecture
  74.      $item["Serial Number"] = $row.SerialNumber
  75.      $item["OS Version"] = $row.version
  76.      $item.update()
  77.     }
  78.  }#End Function
  79.  
  80.  
  81. Get-Inventory $computer_array
  82. Import-ListItems -site "http://intranet" //update to list URL
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement