Forum Discussion
Sriram10069
Jul 20, 2023Copper Contributor
Compare txt data with folder name in SharedDirectory if found modify permissions
Hi All, I am struggling to write a Powershell script for the Last Few Days with no success. Hence, I am asking for help here. I have a HomeFolder Directory Named "Shared_Folder_Name" and It contain...
LeonPavesic
Jul 20, 2023Silver Contributor
Hi Sriram10069,
I tried to combine your scripts that you provided in your question into one PowerShell Script:
# Read data from the .txt file and compare it with folder names in the shared directory
$FilePath = "C:\Users\admin\Desktop\Migration_Completed_Users.txt"
$HomeFolders = Get-ChildItem D:\Shared_Folder_Name -Directory
$HDPath = Get-Content -Path $FilePath
foreach ($HDUsername in $HDPath) {
$MatchedFolder = $HomeFolders | Where-Object { $_.Name -eq $HDUsername }
if ($MatchedFolder) {
Write-Host "Found Migrated Folder: $($MatchedFolder.Name)"
# Modify permissions for the matched folders in the shared directory
$SharePath = "\\servername\USERS"
foreach ($HomeFolder in $HomeFolders) {
$Path = $HomeFolder.FullName
$Username = $HomeFolder.Name
$IdentityReferrence = "domain\$Username"
$Ar = New-Object System.Security.AccessControl.FileSystemAccessRule($IdentityReferrence, 'Modify', 'ContainerInherit,ObjectInherit', 'None', 'Allow')
$Acl = (Get-Item $Path).GetAccessControl('Access')
foreach ($aclitem in $acl.Access) {
if ($aclitem.IdentityReference -eq $ar.IdentityReference) {
if ($aclitem.FileSystemRights -ne $ar.FileSystemRights) {
Write-Host "$($HomeFolder.FullName) has permission of $($aclitem.FileSystemRights)"
$Acl.SetAccessRule($Ar)
Write-Host "Correcting permissions on $($homefolder.fullname)"
(Get-Item $HomeFolder.FullName).SetAccessControl($acl)
}
}
}
}
}
else {
Write-Host "Not Found Migrated Folder for User: $HDUsername"
}
}
This script should read the usernames from the .txt file and compares them with the folder names in the shared directory. If it finds a match, it displays a message indicating that it found the migrated folder.
Then, it should proceeds to modify the permissions for the matched folders in the shared directory.
Remember to replace `"\\servername\USERS"` with the correct path to your shared directory in the script.
Please click Mark as Best Response & Like if my post helped you to solve your issue.
This will help others to find the correct solution easily. It also closes the item.
If the post was useful in other ways, please consider giving it Like.
Kindest regards,
Leon Pavesic