User Tools

Site Tools


userspace:opsi2ad

This is an old revision of the document!


OPSI2AD

# This script synchronizes the client descritions from OPSI to AD
 
# Connect to the MYSQL of OPSI and crete a csv with $hostId and $description
echo "Getting information from OPSI-Backend..."
chcp 850
echo "SET NAMES 'cp850' ; select hostId, description from HOST" | `
./mysql.exe --default-character-set=utf8 -hDATABASE-HOST -uDATABASEUSER -pPASSWORD -DDATABASE > .output.sql
 
# Convert SQL output to valid CSV output
(Get-Content .output.sql )  | Foreach-Object {
    $_ -replace '\t', ';'
	} | Set-Content -encoding utf8 .output.csv
 
echo "Pushing to AD..."
# Load CSV and transform values for $hostId and $description
Import-Csv ./.output.csv -encoding utf8 -Delimiter ";"  |`
    ForEach-Object {
        $hostId += $_.hostId.ToUpper().Replace(".WKK-INTRA.NET","")
        $description += $_.description
 
		# If $description is NOT empty or null 
		if (!([string]::IsNullOrEmpty($description))) {
			# If $hostId is found in AD-Backend
			if ($hostou=(dsquery computer domainroot -name "$hostId")) {
				# If $hostId has NOT same $description yet
				if (!(dsquery computer domainroot -name "$hostId" -desc "$description")) {
				$olddescription=(dsget computer "$hostou" -desc | select -first 1 -skip 1)
				$olddescription= $olddescription.Trim()
 
				echo "Updating $hostId with description: `"$description`" was `"$olddescription`"" >> log
				dsmod computer $hostou -desc "$description"
 
				}
			}
		}
 
		$hostOu = @()
		$hostid=@()
		$description = @()
		$objComputer= @()
		}
userspace/opsi2ad.1392030749.txt.gz · Last modified: 2021/08/23 08:37 (external edit)