[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 372
  • Last Modified:

Logging to txt file

Dear experts,

I have a code made by Chris Dent (many thanks again :)) to set permissions from a file.
What I need is more control during the code execution. Is it possible to log into a txt file if the permissions are set or not please?

The csv file looks like this:
UNCpath,Users,AccessControlType,FileSystemRights,InheritanceFlags
\\fileSRv1\IT\Finance,domain\user2,Deny,FullControl,"This folder, subfolders and Files"
\\fileSRv1\IT\Test,domain\group1,Deny,FullControl,"This folder, subfolders and Files"
\\fileSRv1\MIKE\Test,domain\group1,Deny,FullControl,"This folder, subfolders and Files

exp of log file:
\\fileSRv1\IT\Finance,domain\user2,Deny,FullControl,"This folder, subfolders and Files" | DONE
\\fileSRv1\IT\Test,domain\group1,Deny,FullControl,"This folder, subfolders and Files" | NOT DONE
\\fileSRv1\MIKE\Test,domain\group1,Deny,FullControl,"This folder, subfolders and Files | DONE

Thanks in advance
Import-Csv YourFile.csv | ForEach-Object {

  # Some defaults
  $InheritanceFlags = "containerInherit, ObjectInherit"
  $PropagationFlags = "None"

  # Override defaults if necessary
  $Inheritance = $_.InheritanceFlags
  Switch ($Inheritance) {
    "Subfolders and Files only"                      { $PropagationFlags = "InheritOnly" }
    "This Folder, One level of Subfolders and Files" { $PropagationFlags = "NoPropagateInherit" }
    "This folder and subfolders"                     { $InheritanceFlags = "ContainerInherit" }
    "Subfolders only"                                { $InheritanceFlags = "ContainerInherit"; $PropagationFlags = "InheritOnly" }
    "This folder and files"                          { $InheritanceFlags = "ObjectInherit" }
    "This folder and one level of files"             { $InheritanceFlags = "ObjectInherit"; $PropagationFlags = "InheritOnly" }
    "This folder only"                               { $InheritanceFlags = "None" }
  }

  # Build an access rule based on the line we read from the file
  $AccessRule = New-Object Security.AccessControl.FileSystemAccessRule(
    $_.Users, $_.FileSystemRights, $InheritanceFlags, $PropagationFlags, $_.AccessControlType)

  # Get the current access list
  $Acl = Get-Acl $_.UNCPath
  # Add the rule
  $Acl.AddAccessRule($AccessRule)
  # Save changes
  Set-Acl -Path $_.UNCPath -AclObject $Acl
}

Open in new window

0
DigitBoy
Asked:
DigitBoy
  • 6
  • 6
1 Solution
 
Neil RussellTechnical Development LeadCommented:
After any powershell command executes you can test for its completion status using the $? opperator.

i.e.

command to execute here
if ($? -eq $True ) {
#last command execution returned no error
} else {
#last comman exection returned error
}


So after your set acl...... line you can add

if ( $? -eq $True ) {
Add-Content  $OutputFileVariable ( $_.UNCPath + ":Done")

} else {
Add-Content  $OutputFileVariable ( $_.UNCPath + ": NOT Done")

}
0
 
DigitBoyAuthor Commented:
Hi Neilsr,


thanks for your reply, but I'm real bad coder. Can you help me out where I have to put your lines inside the existing code please?

Thanks in advance,
D
0
 
Neil RussellTechnical Development LeadCommented:
Change

# Get the current access list  
  $Acl = Get-Acl $_.UNCPath  
  # Add the rule  
  $Acl.AddAccessRule($AccessRule)  
  # Save changes  
  Set-Acl -Path $_.UNCPath -AclObject $Acl  
}

into
 
# Get the current access list  
  $Acl = Get-Acl $_.UNCPath  
  # Add the rule  
  $Acl.AddAccessRule($AccessRule)  
  # Save changes  
  Set-Acl -Path $_.UNCPath -AclObject $Acl  
  if ( $? -eq $True ) {
    Add-Content  $OutputFileVariable ( $_.UNCPath + ":Done")
  } else {
    Add-Content  $OutputFileVariable ( $_.UNCPath + ": NOT Done")
  }

}

Open in new window

0
 The Evil-ution of Network Security Threats

What are the hacks that forever changed the security industry? To answer that question, we created an exciting new eBook that takes you on a trip through hacking history. It explores the top hacks from the 80s to 2010s, why they mattered, and how the security industry responded.

 
Neil RussellTechnical Development LeadCommented:
And at the beginning of your file declare $outputFileVariable something like this.....

$OutputFileVariable = "C:\Path\To\Mylogfile.log"
0
 
DigitBoyAuthor Commented:
Thanks a lot Neilsr I will try it now and will come back to you.
0
 
DigitBoyAuthor Commented:
Hi Neilsr,

it worked perfectly. Is it also possible to add line nr and date in the log please?

Regards.
0
 
Neil RussellTechnical Development LeadCommented:
Line number of what? The line of the log file? Waste of time, get a copy of Notepad++ instead of notepad and that will show you the line numbers anyway :D

The date and time? Easy just change

  if ( $? -eq $True ) {  
    Add-Content  $OutputFileVariable ( $_.UNCPath + ":Done")  
  } else {  
    Add-Content  $OutputFileVariable ( $_.UNCPath + ": NOT Done")  
  }

To

if ( $? -eq $True ) {  
    Add-Content  $OutputFileVariable ( now + ": " + $_.UNCPath + ":Done")  
  } else {  
    Add-Content  $OutputFileVariable ( now + ": " +  $_.UNCPath + ": NOT Done")  
  }

Open in new window

0
 
Neil RussellTechnical Development LeadCommented:
sorry, if that errors then use  [System.DateTime]::Now  instead of just the word now
0
 
DigitBoyAuthor Commented:
Hi Neilsr,

 [System.DateTime] didnt work neither "now". See error below

Cannot convert value ": " to type "System.Int32". Error: "Input string was not in a correct format."
At C:\test.ps1:32 char:61
+             Add-Content  $OutputFileVariable ( [System.DateTime] + <<<<  ": " + $_.UNCPath + ":Done")
    + CategoryInfo          : NotSpecified: (:) [], RuntimeException
    + FullyQualifiedErrorId : RuntimeException
0
 
Neil RussellTechnical Development LeadCommented:
No you misread an i didnt test :P ... You nead

  Add-Content  $OutputFileVariable ([string]( [System.datetime]::now) + ": " + $_.UNCPath + ":Done")  
0
 
DigitBoyAuthor Commented:
Thanks that did the trick :D
0
 
DigitBoyAuthor Commented:
Great support from Neilsr
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now