0

I am using this script to add users to folder permission. How can I output the result of this script (success or failure) to a text file?

I tried adding 2> "OutputPath" at the end to stream the output to this location but it did not work.

$ProjectName = "ProjetName"
$Folder = "FolderPath" + $ProjectName
Get-Acl $Folder
$ColRights = [System.Security.AccessControl.FileSystemRights]"Modify, ListDirectory"
$InheritanceFlagSetting = @("ContainerInherit","ObjectInherit")
$PropagationFlagSetting = "None"
$InheritanceFlag = [System.Security.AccessControl.InheritanceFlags]$InheritanceFlagSetting
$PropagationFlag = [System.Security.AccessControl.PropagationFlags]$PropagationFlagSetting
$objType =[System.Security.AccessControl.AccessControlType]::Allow
$objUser = New-Object System.Security.Principal.NTAccount("Username")
$objACE = New-Object System.Security.AccessControl.FileSystemAccessRule ($objUser, $colRights, $InheritanceFlag, $PropagationFlag, $objType)

#Get the ACL for the folder (so we can add to it)
$objACL = Get-Acl $Folder
$objACL.AddAccessRule($objACE)

#publish the new ACL with additional rule
Set-Acl $Folder $objACL
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
RajanNepal209
  • 21
  • 1
  • 1
  • 1
  • 3
    Possible duplicate of [Write output to a text file in PowerShell](https://stackoverflow.com/questions/18469104/write-output-to-a-text-file-in-powershell) – Daniel Mann Apr 20 '19 at 16:33
  • 2
    What output are you expecting? Your invocations of `Get-ACL`, `Set-ACL`, and `$objACL.AddAccessRule()` (which doesn't return a value) are the only expressions not captured in variables, so that's all you should see. Why are you using [`2>`](https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_redirection) specifically? – Lance U. Matthews Apr 20 '19 at 16:34

2 Answers2

5

You may use Start-Transcript and the beginning of your script and stop transcript at the end. This will capture your script execution in a file you mention.

Start-Transcript -Path "C:\transcripts\transcript.txt"

at the end of your script use Stop-Transcript

3

Using:

| Out-File -FilePath C:\FileName.log

At your point of execution will create a new file or overwrite an existing file with the same name. It is similar to using

> C:\FileName.log

These commands are used when you only have a single point of execution to capture or when you want to overwrite an existing file.

Using:

| Out-File -FilePath C:\FileName.log -Append

At your point of execution will create a new file if it doesn't already exist, or if it does exist, it writes to the end (Appending) of the file. It is similar to using

>> C:\FileName.log

These methods are used when you want to continue using the same file.

The other option is to use:

Start-Transcript -Path C:\FileName.log 

(which can also be used with -Append) at the start of your script. When you start this, it will log everything that follows, including the code in your script. To turn it off you need to use the "Stop-Transcript" command or end the session.

DBADon
  • 449
  • 5
  • 9