I got it sort of working. But with a big issue. I now have group objects that are being locked. And I have no idea how I can unlock those objects so I can delete them.
The current script:
# PowerShell script to read Computer details from Comandline
# Import Modules
import-module ActiveDirectory
import-module .\CheckPoint.psm1
# Variables for customisation
$ADG = "ActiveDirectoryHosts"
$Color = "Cyan"
$Comments = "All hosts in the Active Directory"
# Get my Domain name and Fill in the blanks
$Domain = Get-ADDomain
$DNSRoot = $Domain.DNSRoot
$ADG = -join("$ADG", "-", "$DNSRoot")
$Comments = "$Comments $DNSRoot"
# Read Active Directory Computerlist
$ADComputers = Get-ADComputer -Filter * -Property Name,IPv4Address,IPv6Address
## Output to screen
$DNSRoot
$ADComputers|Format-Table Name,DNSHostname,IPv4Address,IPv6Address
# Ignore Certificate Block on self-sign certificate
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $True }
# Login to Check Point API to get Session ID
Write-Host " *** Log in to Check Point Smart Center API *** "
$CPsession = Invoke-CPLogin
# Create ActiveDirectory Group
Write-Host " *** Adding Group $ADG *** "
Add-CPGroup -Session $CPsession -Name $ADG -Tag ActiveDirectory,$DNSRoot -Color Red -Comments "$Comments"
foreach ($Computer in $ADcomputers) {
$NOW = Get-Date
if ($Computer.Enabled -eq $True) {
$CDN = $Computer.DistinguishedName
Write-Host " *** Adding Host $CDN *** "
if ($Computer.IPv6Address -ne $Null -And $Computer.IPv4Address -ne $Null) {
Add-CPHost -Session $CPsession -Name $Computer.DNSHostName -Ipv4address $Computer.IPv4Address -Ipv6address $Computer.IPv6Address -Tag ActiveDirectory,$DNSRoot -Color $Color -Groups $ADG -Comments "$CDN added $NOW"
} elseif ($Computer.IPv4Address -ne $Null) {
Add-CPHost -Session $CPsession -Name $Computer.DNSHostName -Ipv4address $Computer.IPv4Address -Tag ActiveDirectory,$DNSRoot -Color $Color -Groups $ADG -Comments "$CDN added $NOW"
}
}
}
# Publish Changes
Invoke-CPPublish -Session $CPsession
# Logout from Check Point API
Invoke-CPLogout -Session $CPsession
# Remove Modules
Remove-Module ActiveDirectory
Remove-Module CheckPoint
# DONE!
<< We make miracles happen while you wait. The impossible jobs take just a wee bit longer. >>