Porffor
asked on
Could anyone write me a powershell script that does the following...?
All our users' home folders are located on the file sevrer, in E:\Folder\%USERNAME%\. Inside this is a folder called Flags. What I want is a PowerShell script that created an empty file E:\Folder\%USERNAME%\Flags \Pin2016.f lg - recursively for every user E:\Folder\%USERNAME%
It's PowerShell Version 4 on Windows Server 2008 R2
Also it would be good to have the result for each file creation dumped to a log file, because there might be one or two Flags folders in there that has weird permissions.
Thank you.
It's PowerShell Version 4 on Windows Server 2008 R2
Also it would be good to have the result for each file creation dumped to a log file, because there might be one or two Flags folders in there that has weird permissions.
Thank you.
If you do not need a sophisticated analysis of failures, a simple brute-force approach is best:
set-location E:\Folder
Get-ChildItem -recurse -filter flags -Directory | % {
try { "" | Set-Content "$($_.FullName)\Pin2016.flg -force }
catch { "The flag in $($_.FullName) could not be created" | Add-Content flaglog.txt }
}
This will, of course, overwrite existing flags. Adding a test for existence is easy:set-location E:\Folder
Get-ChildItem -recurse -filter flags -Directory |
? { !(Test-Path "$($_.FullName)\Pin2016.flg") } |
% {
try { "" | Set-Content "$($_.FullName)\Pin2016.flg -force }
catch { "The flag in $($_.FullName) could not be created" | Add-Content flaglog.txt }
}
ASKER
Thank you both.
Qlemo, I'm getting...
Also, I think both of the scripts are written to... "Find a 'Flags' folder, then create a file inside it", but this doesn't catch the user folders where the folder doesn't exist, e.g. if it's been deleted by the user. Could you adjust it so that it just goes through all the folders within E:\Folder, then creates file Flags\Pin2016.flg inside each one? Then the log file would report on the file creation success according to each user folders, rather than each Flags folder.
Thanks.
Qlemo, I'm getting...
At E:\DFSRoots\2016.ps1:4 char:59
+ ... g in $($_.FullName) could not be created" | Add-Content flaglog.txt }
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The string is missing the terminator: ".
At E:\DFSRoots\2016.ps1:3 char:7
+ try { "" | Set-Content "$($_.FullName)\Pin2016.flg -force }
+ ~
Missing closing '}' in statement block or type definition.
At E:\DFSRoots\2016.ps1:5 char:2
+ }
+ ~
The Try statement is missing its Catch or Finally block.
At E:\DFSRoots\2016.ps1:2 char:53
+ Get-ChildItem -recurse -filter flags -Directory | % {
+ ~
Missing closing '}' in statement block or type definition.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : TerminatorExpectedAtEndOfString
Also, I think both of the scripts are written to... "Find a 'Flags' folder, then create a file inside it", but this doesn't catch the user folders where the folder doesn't exist, e.g. if it's been deleted by the user. Could you adjust it so that it just goes through all the folders within E:\Folder, then creates file Flags\Pin2016.flg inside each one? Then the log file would report on the file creation success according to each user folders, rather than each Flags folder.
Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That does exactly what I need. Thanks.
ASKER
Sorry, I've found something in the script that I'd like to adjust. Would you mind having the script add Full Control permissions to the Everyone group to the file as it's created?
Thank you very much.
Thank you very much.
$Root = 'E:\Folder'
$FileName = 'Pin2016.flg'
$LogFile = 'C:\Temp\FlagFileCreate.csv'
Get-ChildItem -Directory -Path $Root |
ForEach-Object {
$Return = $_ | Select-Object -Property @{n='Path'; e={$_.FullName}}, 'Result'
Try {
$FlagsFolder = "$($_.FullName)\Flags"
If (-not (Test-Path -Path $FlagsFolder -PathType Container)) {
New-Item -Path $FlagsFolder -ItemType Directory -ErrorAction Stop | Out-Null
}
$FileItem = New-Item -Path $FlagsFolder -Name $FileName -ItemType File -ErrorAction Stop
$Acl = $FileItem.GetAccessControl('Access')
$Acl.AddAccessRule((New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule -ArgumentList 'Everyone', 'Full', 'Allow'))
$FileItem.SetAccessControl($Acl)
$Return.Result = 'OK'
} Catch {
$Return.Result = $_.Exception.Message
}
$Return
} | Export-Csv -NoTypeInformation -Path $LogFile
ASKER
Excellent stuff - thanks a lot!
Open in new window