溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

用Powershell 腳本如何修改用戶配置文件

發(fā)布時(shí)間:2020-05-07 16:58:15 來(lái)源:億速云 閱讀:386 作者:三月 欄目:系統(tǒng)運(yùn)維

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

  1. 以其他管理員身份登錄計(jì)算機(jī);

  2. 確認(rèn)該用戶abc已經(jīng)退出登錄狀態(tài),可以通過(guò)任務(wù)管理器或者quser來(lái)操作

  3. 修改C:\users\abc 的文件名為新的用戶名C:\users\abc1

  4. 修改注冊(cè)表,這個(gè)里面有一堆根據(jù)SID命名的key,需要找到對(duì)應(yīng)的,然后修改對(duì)應(yīng)的profileImagePath

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList

    用Powershell 腳本如何修改用戶配置文件

  5. 創(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盤(pán)下測(cè)試,然后以本地管理員身份登錄,執(zhí)行這個(gè)腳本,成功!

用Powershell 腳本如何修改用戶配置文件

用Powershell 腳本如何修改用戶配置文件

用Powershell 腳本如何修改用戶配置文件就先給大家講到這里,對(duì)于其它相關(guān)問(wèn)題大家想要了解的可以持續(xù)關(guān)注我們的行業(yè)資訊。我們的板塊內(nèi)容每天都會(huì)捕捉一些行業(yè)新聞及專(zhuān)業(yè)知識(shí)分享給大家的。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI