Replace Unicode box-drawing characters with ASCII for ISE compatibility
This commit is contained in:
+12
-12
@@ -21,11 +21,11 @@
|
|||||||
Filename: Set-ComputerManagedBy.ps1
|
Filename: Set-ComputerManagedBy.ps1
|
||||||
#>
|
#>
|
||||||
|
|
||||||
# ── Configuration ────────────────────────────────────────────────────────────
|
# -- Configuration ------------------------------------------------------------
|
||||||
$LogFile = "$env:TEMP\Set-ComputerManagedBy.log"
|
$LogFile = "$env:TEMP\Set-ComputerManagedBy.log"
|
||||||
$MaxLogSizeKB = 256
|
$MaxLogSizeKB = 256
|
||||||
|
|
||||||
# ── Logging helper ───────────────────────────────────────────────────────────
|
# -- Logging helper ------------------------------------------------------------
|
||||||
function Write-Log {
|
function Write-Log {
|
||||||
param([string]$Message, [string]$Level = "INFO")
|
param([string]$Message, [string]$Level = "INFO")
|
||||||
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
|
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
|
||||||
@@ -36,7 +36,7 @@ function Write-Log {
|
|||||||
Add-Content -Path $LogFile -Value $entry -ErrorAction SilentlyContinue
|
Add-Content -Path $LogFile -Value $entry -ErrorAction SilentlyContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
# ── Gather environment ──────────────────────────────────────────────────────
|
# -- Gather environment --------------------------------------------------------
|
||||||
$currentUser = $env:USERNAME
|
$currentUser = $env:USERNAME
|
||||||
$computerName = $env:COMPUTERNAME
|
$computerName = $env:COMPUTERNAME
|
||||||
$userDomain = $env:USERDOMAIN
|
$userDomain = $env:USERDOMAIN
|
||||||
@@ -44,13 +44,13 @@ $loginTimestamp = Get-Date -Format "yyyy-MM-dd HH:mm"
|
|||||||
|
|
||||||
Write-Log "Script started. User=$userDomain\$currentUser, Computer=$computerName"
|
Write-Log "Script started. User=$userDomain\$currentUser, Computer=$computerName"
|
||||||
|
|
||||||
# ── Skip if local/non-domain login ──────────────────────────────────────────
|
# -- Skip if local/non-domain login --------------------------------------------
|
||||||
if ($userDomain -eq $computerName) {
|
if ($userDomain -eq $computerName) {
|
||||||
Write-Log "Local login detected (domain = computername). Skipping." "WARN"
|
Write-Log "Local login detected (domain = computername). Skipping." "WARN"
|
||||||
exit 0
|
exit 0
|
||||||
}
|
}
|
||||||
|
|
||||||
# ── Try AD module first, fall back to ADSI ──────────────────────────────────
|
# -- Try AD module first, fall back to ADSI ------------------------------------
|
||||||
$useADModule = $false
|
$useADModule = $false
|
||||||
try {
|
try {
|
||||||
Import-Module ActiveDirectory -ErrorAction Stop
|
Import-Module ActiveDirectory -ErrorAction Stop
|
||||||
@@ -60,16 +60,16 @@ try {
|
|||||||
Write-Log "AD module not available. Falling back to ADSI/DirectorySearcher." "WARN"
|
Write-Log "AD module not available. Falling back to ADSI/DirectorySearcher." "WARN"
|
||||||
}
|
}
|
||||||
|
|
||||||
# ── Helper: build the notes string ──────────────────────────────────────────
|
# -- Build the notes string ----------------------------------------------------
|
||||||
$notesValue = "Last logon: $computerName ($loginTimestamp)"
|
$notesValue = "Last logon: $computerName ($loginTimestamp)"
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if ($useADModule) {
|
if ($useADModule) {
|
||||||
# ── AD Module path ──────────────────────────────────────────────────
|
# -- AD Module path ----------------------------------------------------
|
||||||
$userObj = Get-ADUser -Identity $currentUser -Properties info -ErrorAction Stop
|
$userObj = Get-ADUser -Identity $currentUser -Properties info -ErrorAction Stop
|
||||||
$computerObj = Get-ADComputer -Identity $computerName -Properties managedBy -ErrorAction Stop
|
$computerObj = Get-ADComputer -Identity $computerName -Properties managedBy -ErrorAction Stop
|
||||||
|
|
||||||
# — Set computer ManagedBy ——
|
# Set computer ManagedBy
|
||||||
if ($computerObj.managedBy -eq $userObj.DistinguishedName) {
|
if ($computerObj.managedBy -eq $userObj.DistinguishedName) {
|
||||||
Write-Log "ManagedBy already set to $currentUser. No change needed."
|
Write-Log "ManagedBy already set to $currentUser. No change needed."
|
||||||
} else {
|
} else {
|
||||||
@@ -77,7 +77,7 @@ try {
|
|||||||
Write-Log "SUCCESS: Set ManagedBy on '$computerName' to '$($userObj.DistinguishedName)'"
|
Write-Log "SUCCESS: Set ManagedBy on '$computerName' to '$($userObj.DistinguishedName)'"
|
||||||
}
|
}
|
||||||
|
|
||||||
# — Set user Notes (info attribute) ——
|
# Set user Notes (info attribute)
|
||||||
if ($userObj.info -eq $notesValue) {
|
if ($userObj.info -eq $notesValue) {
|
||||||
Write-Log "User notes already current. No change needed."
|
Write-Log "User notes already current. No change needed."
|
||||||
} else {
|
} else {
|
||||||
@@ -86,7 +86,7 @@ try {
|
|||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
# ── ADSI fallback (no module required) ──────────────────────────────
|
# -- ADSI fallback (no module required) --------------------------------
|
||||||
$rootDSE = [ADSI]"LDAP://RootDSE"
|
$rootDSE = [ADSI]"LDAP://RootDSE"
|
||||||
$domainDN = $rootDSE.defaultNamingContext
|
$domainDN = $rootDSE.defaultNamingContext
|
||||||
|
|
||||||
@@ -117,7 +117,7 @@ try {
|
|||||||
|
|
||||||
$computerDN = $computerResult.Properties["distinguishedname"][0]
|
$computerDN = $computerResult.Properties["distinguishedname"][0]
|
||||||
|
|
||||||
# — Set computer ManagedBy ——
|
# Set computer ManagedBy
|
||||||
$currentManagedBy = $null
|
$currentManagedBy = $null
|
||||||
if ($computerResult.Properties["managedby"].Count -gt 0) {
|
if ($computerResult.Properties["managedby"].Count -gt 0) {
|
||||||
$currentManagedBy = $computerResult.Properties["managedby"][0]
|
$currentManagedBy = $computerResult.Properties["managedby"][0]
|
||||||
@@ -132,7 +132,7 @@ try {
|
|||||||
Write-Log "SUCCESS: Set ManagedBy on '$computerName' to '$userDN'"
|
Write-Log "SUCCESS: Set ManagedBy on '$computerName' to '$userDN'"
|
||||||
}
|
}
|
||||||
|
|
||||||
# — Set user Notes (info attribute) ——
|
# Set user Notes (info attribute)
|
||||||
$currentNotes = $null
|
$currentNotes = $null
|
||||||
if ($userResult.Properties["info"].Count -gt 0) {
|
if ($userResult.Properties["info"].Count -gt 0) {
|
||||||
$currentNotes = $userResult.Properties["info"][0]
|
$currentNotes = $userResult.Properties["info"][0]
|
||||||
|
|||||||
Reference in New Issue
Block a user