本文主要給大家簡(jiǎn)單講講用Powershell 腳本如何修改用戶配置文件,相關(guān)專業(yè)術(shù)語(yǔ)大家可以上網(wǎng)查查或者找一些相關(guān)書(shū)籍補(bǔ)充一下,這里就不涉獵了,我們就直奔主題吧,希望用Powershell 腳本如何修改用戶配置文件這篇文章可以給大家?guī)?lái)一些實(shí)際幫助。

以其他管理員身份登錄計(jì)算機(jī);
確認(rèn)該用戶abc已經(jīng)退出登錄狀態(tài),可以通過(guò)任務(wù)管理器或者quser來(lái)操作
修改C:\users\abc 的文件名為新的用戶名C:\users\abc1
修改注冊(cè)表,這個(gè)里面有一堆根據(jù)SID命名的key,需要找到對(duì)應(yīng)的,然后修改對(duì)應(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)建,但是早期的版本沒(méi)有原生的PS命令,只能間接調(diào)用cmd,或者自己寫(xiě)一個(gè)方法
上面的操作都可以通過(guò)PS腳本來(lái)實(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
#測(cè)試該用戶是否已經(jīng)登錄,這里有個(gè)小技巧把quser的字符串結(jié)果轉(zhuǎn)換為對(duì)象,具體解釋參考博客
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
#查詢對(duì)應(yīng)的注冊(cè)表Key
Get-ChildItem "hklm:\software\microsoft\windows nt\currentversion\profilelist" | foreach{
#Get the username from SID
$sid=$_.Name.Split('\')[-1];
#根據(jù)SID來(lái)匹配用戶,如果用戶匹配成功,那么修改對(duì)應(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盤下測(cè)試,然后以本地管理員身份登錄,執(zhí)行這個(gè)腳本,成功!


用Powershell 腳本如何修改用戶配置文件就先給大家講到這里,對(duì)于其它相關(guān)問(wèn)題大家想要了解的可以持續(xù)關(guān)注我們的行業(yè)資訊。我們的板塊內(nèi)容每天都會(huì)捕捉一些行業(yè)新聞及專業(yè)知識(shí)分享給大家的。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
當(dāng)前標(biāo)題:用Powershell腳本如何修改用戶配置文件-創(chuàng)新互聯(lián)
文章URL:http://chinadenli.net/article46/shphg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、網(wǎng)站排名、動(dòng)態(tài)網(wǎng)站、網(wǎng)站設(shè)計(jì)、響應(yīng)式網(wǎng)站、服務(wù)器托管
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容