User Profile Disk unlock script - Powershell

A powershell script that bring up a gridview where you can select user profile disk and dismount them. Useful if the user is getting a temp profile due to locked user profile disks. For use on the file server hosting the UPD. Replace <fileserverwheretheUPDsarestored> with the server name and <driverletterwherethevhdshare> with the physical drive letter where the vhds are.

$vhdarray = @()


## Get list of all VHDs and all Open VHDs for comparison

$vhds = Invoke-Command -ComputerName <fileserverwheretheUPDsarestored> {gci "E:\Data\UserProfileDisks" |

Where-Object {($_.Name -like "*.vhdx") -and ($_.name -notlike "*template*")}}


$openfiles = Invoke-Command -ComputerName <fileserverwheretheUPDsarestored> {get-smbopenfile | where {$_.path -Like "*.vhdx"}}


## Check list of VHDs against open VHDs to determine if the disk is connected to on of the hosts

foreach($vhd in $vhds)

{

$openfiletest = ($openfiles | Where {$_.path -eq $vhd.FullName}) | select -Unique

IF ($openfiletest)

{

$vhdopened = $true

$vhdrdshostip = $openfiletest.clientcomputername | select -Unique

$vhdrdshost = ([System.Net.Dns]::gethostentry($vhdrdshostip)).hostname

}

ELSE

{

$vhdopened = $false

$vhdrdshost = "Not Connected"

}

## Get user SID from VHD file name - then convert SID to username

try {

$usersid = ([io.path]::GetFileNameWithoutExtension($vhd.Name)).trim("UVHD-")

$objSID = (New-Object System.Security.Principal.SecurityIdentifier ($usersid) -ErrorAction SilentlyContinue).Translate([System.Security.Principal.NTAccount])

$username = $objSID.value

}

Catch{}


## Order the data into a hash table and add to the array

$hash = [ordered]@{

'Username' = $username

'VHD Connected' = $vhdopened

'RDS Host' = $vhdrdshost

'VHD Path' = $vhd.FullName

}

$vhdproperties = New-Object -TypeName PSObject -Property $hash

$vhdarray += $vhdproperties

}


$getinfofromvhd = $vhdarray | sort username | ogv -Title "VHD List" -PassThru

write-host $getinfofromvhd.'VHD Path'

$trimmedpath = "\\<fileserverwheretheUPDsarestored>\<driveletterwherethevhdshare>$\"+($getinfofromvhd.'VHD Path').Trim("E:\")

dismount-vhd -path $trimmedpath

Power in Numbers

0

Programs

Locations

Volunteers

Project Gallery