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
|
||||
#>
|
||||
|
||||
# ── Configuration ────────────────────────────────────────────────────────────
|
||||
# -- Configuration ------------------------------------------------------------
|
||||
$LogFile = "$env:TEMP\Set-ComputerManagedBy.log"
|
||||
$MaxLogSizeKB = 256
|
||||
|
||||
# ── Logging helper ───────────────────────────────────────────────────────────
|
||||
# -- Logging helper ------------------------------------------------------------
|
||||
function Write-Log {
|
||||
param([string]$Message, [string]$Level = "INFO")
|
||||
$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
|
||||
}
|
||||
|
||||
# ── Gather environment ──────────────────────────────────────────────────────
|
||||
# -- Gather environment --------------------------------------------------------
|
||||
$currentUser = $env:USERNAME
|
||||
$computerName = $env:COMPUTERNAME
|
||||
$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"
|
||||
|
||||
# ── Skip if local/non-domain login ──────────────────────────────────────────
|
||||
# -- Skip if local/non-domain login --------------------------------------------
|
||||
if ($userDomain -eq $computerName) {
|
||||
Write-Log "Local login detected (domain = computername). Skipping." "WARN"
|
||||
exit 0
|
||||
}
|
||||
|
||||
# ── Try AD module first, fall back to ADSI ──────────────────────────────────
|
||||
# -- Try AD module first, fall back to ADSI ------------------------------------
|
||||
$useADModule = $false
|
||||
try {
|
||||
Import-Module ActiveDirectory -ErrorAction Stop
|
||||
@@ -60,16 +60,16 @@ try {
|
||||
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)"
|
||||
|
||||
try {
|
||||
if ($useADModule) {
|
||||
# ── AD Module path ──────────────────────────────────────────────────
|
||||
# -- AD Module path ----------------------------------------------------
|
||||
$userObj = Get-ADUser -Identity $currentUser -Properties info -ErrorAction Stop
|
||||
$computerObj = Get-ADComputer -Identity $computerName -Properties managedBy -ErrorAction Stop
|
||||
|
||||
# — Set computer ManagedBy ——
|
||||
# Set computer ManagedBy
|
||||
if ($computerObj.managedBy -eq $userObj.DistinguishedName) {
|
||||
Write-Log "ManagedBy already set to $currentUser. No change needed."
|
||||
} else {
|
||||
@@ -77,7 +77,7 @@ try {
|
||||
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) {
|
||||
Write-Log "User notes already current. No change needed."
|
||||
} else {
|
||||
@@ -86,7 +86,7 @@ try {
|
||||
}
|
||||
|
||||
} else {
|
||||
# ── ADSI fallback (no module required) ──────────────────────────────
|
||||
# -- ADSI fallback (no module required) --------------------------------
|
||||
$rootDSE = [ADSI]"LDAP://RootDSE"
|
||||
$domainDN = $rootDSE.defaultNamingContext
|
||||
|
||||
@@ -117,7 +117,7 @@ try {
|
||||
|
||||
$computerDN = $computerResult.Properties["distinguishedname"][0]
|
||||
|
||||
# — Set computer ManagedBy ——
|
||||
# Set computer ManagedBy
|
||||
$currentManagedBy = $null
|
||||
if ($computerResult.Properties["managedby"].Count -gt 0) {
|
||||
$currentManagedBy = $computerResult.Properties["managedby"][0]
|
||||
@@ -132,7 +132,7 @@ try {
|
||||
Write-Log "SUCCESS: Set ManagedBy on '$computerName' to '$userDN'"
|
||||
}
|
||||
|
||||
# — Set user Notes (info attribute) ——
|
||||
# Set user Notes (info attribute)
|
||||
$currentNotes = $null
|
||||
if ($userResult.Properties["info"].Count -gt 0) {
|
||||
$currentNotes = $userResult.Properties["info"][0]
|
||||
|
||||
Reference in New Issue
Block a user