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 contains multiple Folders. Each folder belongs to a user and that particular user has permissions to the Folder. The Folder Name is the same as the user name as shown in the below picture.
So the Folder Structure Looks like this.
Now, we have Copied the data from HomeDirectory Folders to OneDrive for Business, for Users Folder1, Folder3, and Folder5.
The Powershell Script that I am trying to create should read the migrated usernames from a .txt file and compare the folders in the home directory. If the Migrated User Folder is found modify the permissions.
So Far I have created the below scripts and trying to join them together.
Script 1 - To read the Data from Txt File and comparison with Folder names
$Filepath="C:\Users\admin\Desktop\Migration_Completed_Users.txt"
$HomeFolders = Get-Childitem D:\Shared_Folder_Name -Directory
$HDPath=Get-Content -Path $FilePath
#$HDPath
foreach ($HDPath in $FilePath){
$FolderName = $HomeFolders.Name
If($HDPath -eq $FolderName){
Write-Host "Found Migrated Folder $FolderName"
}
Else {
Write-Host "Not Found Migrated Folder $FolderName"
}
Script - 2 I got this from the internet to modify the permissions of the folders in the shared directory.
#set root share to scan
$HomeFolders = get-childitem \\servername\USERS -Directory
# loop through all folders in root
foreach ($HomeFolder in $HomeFolders) {
$Path = $HomeFolder.FullName
#set username based on folder name. Know that this is not going to be 100% accurate
# since some user shares may have access granted to other users(ie, managers)
$Username = $HomeFolder.Name
# set variable for Username
$IdentityReferrence = "domain\$Username"
# create security object specific to user
$Ar = New-Object System.Security.AccessControl.FileSystemAccessRule($IdentityReferrence, 'Modify', 'ContainerInherit,ObjectInherit', 'None', 'Allow')
# get acl of folder in loop
$Acl = (Get-Item $Path).GetAccessControl('Access')
# look through all access objects
foreach ($aclitem in $acl.Access) {
# if a matching userID is found, check the permissions against the new access rule identity reference.
if ($aclitem.IdentityReference -eq $ar.IdentityReference) {{{
# if rights do not match, set the permissions with access rule set before
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)
}
}
}
}
}
}
my thought is to combine Script 1 and 2 to get my final script.
- LeonPavesicSilver 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