Link to home
Start Free TrialLog in
Avatar of Porffor
PorfforFlag for United Kingdom of Great Britain and Northern Ireland

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.flg - 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.
Avatar of oBdA
oBdA

Try this; it's in test mode and will not actually create the files. Remove the -WhatIf in line 8 to run it for real.
$FileName = 'Pin2016.flg'
$LogFile = 'C:\Temp\FlagFileCreate.csv'
Get-ChildItem -Directory -Path C:\Temp |
	Where-Object {Get-Item -Path "$($_.FullName)\Flags" -ErrorAction SilentlyContinue} |
	ForEach-Object {
		$Return = $_ | Select-Object -Property @{n='Path'; e={$_.FullName}}, 'Result'
		Try {
			New-Item -Path "$($_.FullName)\Flags" -Name $FileName -ItemType File -ErrorAction Stop -WhatIf | Out-Null
			$Return.Result = 'OK'
		} Catch {
			$Return.Result = $_.Exception.Message
		}
		$Return
	} | Export-Csv -NoTypeInformation -Path $LogFile

Open in new window

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 }
}

Open in new window

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 }
}

Open in new window

Avatar of Porffor

ASKER

Thank you both.

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

Open in new window



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
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Porffor

ASKER

That does exactly what I need.  Thanks.
Avatar of Porffor

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.
$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

Open in new window

Avatar of Porffor

ASKER

Excellent stuff - thanks a lot!