本文主要給大家簡單講講用Powershell 腳本如何修改用戶配置文件,相關(guān)專業(yè)術(shù)語大家可以上網(wǎng)查查或者找一些相關(guān)書籍補(bǔ)充一下,這里就不涉獵了,我們就直奔主題吧,希望用Powershell 腳本如何修改用戶配置文件這篇文章可以給大家?guī)硪恍?shí)際幫助。
為花山等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及花山網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、花山網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!
以其他管理員身份登錄計(jì)算機(jī);
確認(rèn)該用戶abc已經(jīng)退出登錄狀態(tài),可以通過任務(wù)管理器或者quser來操作
修改C:\users\abc 的文件名為新的用戶名C:\users\abc1
修改注冊表,這個(gè)里面有一堆根據(jù)SID命名的key,需要找到對應(yīng)的,然后修改對應(yīng)的profileImagePath
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList
創(chuàng)建新的symboLink連接從 c:\users\abc <==> c:\users\abc1。windows下面有自帶的mklink命令可以使用,比如 mklink /D c:\users \abc c:\users\abc1。PS5以后可以用New-item創(chuàng)建,但是早期的版本沒有原生的PS命令,只能間接調(diào)用cmd,或者自己寫一個(gè)方法
上面的操作都可以通過PS腳本來實(shí)現(xiàn)。
#創(chuàng)建SymLink的方法,這個(gè)網(wǎng)上發(fā)現(xiàn)有現(xiàn)成的,我就直接下載了 function New-Symlink { <# .SYNOPSIS Creates a symbolic link. #> param ( [Parameter(Position=0, Mandatory=$true)] [string] $Link, [Parameter(Position=1, Mandatory=$true)] [string] $Target ) Invoke-MKLINK -Link $Link -Target $Target -Symlink } function New-Hardlink { <# .SYNOPSIS Creates a hard link. #> param ( [Parameter(Position=0, Mandatory=$true)] [string] $Link, [Parameter(Position=1, Mandatory=$true)] [string] $Target ) Invoke-MKLINK -Link $Link -Target $Target -HardLink } function New-Junction { <# .SYNOPSIS Creates a directory junction. #> param ( [Parameter(Position=0, Mandatory=$true)] [string] $Link, [Parameter(Position=1, Mandatory=$true)] [string] $Target ) Invoke-MKLINK -Link $Link -Target $Target -Junction } function Invoke-MKLINK { <# .SYNOPSIS Creates a symbolic link, hard link, or directory junction. #> [CmdletBinding(DefaultParameterSetName = "Symlink")] param ( [Parameter(Position=0, Mandatory=$true)] [string] $Link, [Parameter(Position=1, Mandatory=$true)] [string] $Target, [Parameter(ParameterSetName = "Symlink")] [switch] $Symlink = $true, [Parameter(ParameterSetName = "HardLink")] [switch] $HardLink, [Parameter(ParameterSetName = "Junction")] [switch] $Junction ) # Ensure target exists. if (-not(Test-Path $Target)) { throw "Target does not exist.`nTarget: $Target" } # Ensure link does not exist. if (Test-Path $Link) { throw "A file or directory already exists at the link path.`nLink: $Link" } $isDirectory = (Get-Item $Target).PSIsContainer $mklinkArg = "" if ($Symlink -and $isDirectory) { $mkLinkArg = "/D" } if ($Junction) { # Ensure we are linking a directory. (Junctions don't work for files.) if (-not($isDirectory)) { throw "The target is a file. Junctions cannot be created for files.`nTarget: $Target" } $mklinkArg = "/J" } if ($HardLink) { # Ensure we are linking a file. (Hard links don't work for directories.) if ($isDirectory) { throw "The target is a directory. Hard links cannot be created for directories.`nTarget: $Target" } $mkLinkArg = "/H" } # Capture the MKLINK output so we can return it properly. # Includes a redirect of STDERR to STDOUT so we can capture it as well. $output = cmd /c mklink $mkLinkArg `"$Link`" `"$Target`" 2>&1 if ($lastExitCode -ne 0) { throw "MKLINK failed. Exit code: $lastExitCode`n$output" } else { Write-Output $output } } #定義一個(gè)Flag跳出循環(huán) $flag=$true while($flag){ $oldName=read-host "Please input the old user name" write-host 'Searching user profile..' -ForegroundColor Cyan #測試該用戶是否已經(jīng)登錄,這里有個(gè)小技巧把quser的字符串結(jié)果轉(zhuǎn)換為對象,具體解釋參考博客 http://beanxyz.blog.51cto.com/5570417/1906162 if (Test-Path "c:\users\$oldName"){ write-host "User Profile c:\users\$oldName found." -ForegroundColor Cyan #Check if the user is currently logged In $quser = (quser) -replace '\s{2,17}', ',' | ConvertFrom-Csv $sessionId = $quser | Where-Object { $_.Username -eq $newName } | select -ExpandProperty id #如果已經(jīng)登錄,那么強(qiáng)行退出這個(gè)用戶 foreach($id in $sessionId){ if($id -ne $null){ write-host "Detected User $newName still login" -ForegroundColor red Write-Host "Force logoff the user" -ForegroundColor red logoff $id } } $newName=read-host "Please input the new name" $oldpath="c:\users\$oldName" $newpath="c:\users\$newName" #重命名文件夾 rename-item $oldpath $newpath -Confirm -ErrorAction Stop write-host "Searching Registry Information " -ForegroundColor Cyan #查詢對應(yīng)的注冊表Key Get-ChildItem "hklm:\software\microsoft\windows nt\currentversion\profilelist" | foreach{ #Get the username from SID $sid=$_.Name.Split('\')[-1]; #根據(jù)SID來匹配用戶,如果用戶匹配成功,那么修改對應(yīng)的ProfileList try{ $objSID = New-Object System.Security.Principal.SecurityIdentifier ($sid) $objUser = $objSID.Translate( [System.Security.Principal.NTAccount]) $username=$objUser.Value } catch{} #change registry keys if(($username -eq "omnicom\$oldName") -or ($username -eq "omnicom\$newName")){ write-host "Found Registry Information of user profile $newName" -ForegroundColor Cyan $keys=Get-ItemProperty "hklm:\software\microsoft\windows nt\currentversion\profilelist\$sid" $keys.ProfileImagePath=$newpath write-host "Registry key profile list is changed to $newpath" -ForegroundColor Cyan #調(diào)用上面的方法,創(chuàng)建Symbolink #Create new symbolink #New-Item -Path $oldpath -ItemType Junction -Value $newpath New-Symlink -Link $oldpath -Target $newpath break; } else{ write-host "$username Name not match...skip" -ForegroundColor Yellow } } $flag=$false } else { write-host "Profile is not found. Please try again" -ForegroundColor red } }
執(zhí)行效果,我直接把這個(gè)文件扔到一個(gè)遠(yuǎn)程電腦的C盤下測試,然后以本地管理員身份登錄,執(zhí)行這個(gè)腳本,成功!
用Powershell 腳本如何修改用戶配置文件就先給大家講到這里,對于其它相關(guān)問題大家想要了解的可以持續(xù)關(guān)注我們的行業(yè)資訊。我們的板塊內(nèi)容每天都會捕捉一些行業(yè)新聞及專業(yè)知識分享給大家的。