不少公司都會針對每個員工鎖定使用USB隨身碟,例如建立一個OU並且透過WSUS部署,讓每台AD網域內的電腦都受政策控制,不過也有些情況是某些人的電腦會申請某些時段要能開放給他用USB隨身碟等,而管理員為了要能輕鬆且不忘記時間到時,要把它拉回到原本的群組,所以這時候就能用到powershell來幫忙,寫個腳本然後放到用戶申請使用的截止日,排程器時間到的時候就會把原本移到不鎖定USB的OU裡面,在移回到原本的群組裡,這樣就可以省時和避免忘記。
[console]::OutputEncoding = [System.Text.Encoding]::UTF8
$ComputerDN = "CN=ABC123456789PC,OU=不鎖USB,OU=電腦主機,DC=abc,DC=com" $NewOU = "OU=資訊部,OU=電腦主機,DC=abc,DC=com"
$smtpServer = "smtp.office365.com"$smtpPort = 587$smtpUser = "mis@abc.com"$smtpPass = "sfafsszvegtwetewehsgdas"$from = "mis@abc.com"$to = "mis@abccom"$subject = "不鎖USB設定有更新"
# 定義發送郵件的函數function Send-Notification { param ( [string]$body )
$mailParams = @{ From = $from To = $to Subject = $subject Body = $body SmtpServer = $smtpServer Port = $smtpPort UseSsl = $true Credential = New-Object System.Management.Automation.PSCredential($smtpUser, (ConvertTo-SecureString $smtpPass -AsPlainText -Force)) Encoding = ([System.Text.Encoding]::UTF8) }
try { Send-MailMessage @mailParams Write-Host "郵件已成功發送" } catch { Write-Warning "錯誤: 無法發送郵件 - $_" }}
# 嘗試移動AD物件try { Move-ADObject -Identity $ComputerDN -TargetPath $NewOU -ErrorAction Stop Write-Host "已成功將 $ComputerDN 移回到 $NewOU" -ForegroundColor Green $body = "已成功將 $ComputerDN 移回到 $NewOU" Send-Notification -body $body} catch { Write-Host "錯誤: 無法移動 $ComputerDN 到 $NewOU。$_" -ForegroundColor Red $body = "錯誤: 無法移動 $ComputerDN 到 $NewOU。$_" Send-Notification -body $body}
設定排程器的操作流程前面就寫過了,這篇就不再重複了,把腳本設定到一個時間點去執行,後面就交給電腦處理了。
沒有留言:
張貼留言