2

I know it's trivial to use colours in scripting, but it does help if you have tons of outputs. I have the following script and would like to change the colour of the output depending on the result.

I added line numbers for easy navigation...

1 function Add-Group2Folder
2    {
3    param ($Url, $sourceFolder, $GroupName, $PermissionLevel)
4    try{
5            $web = Get-SPWeb -Identity $Url
6            
7            if($web -ne $null){
8                # $sourceFolderGets the folder that is located at the specified URL.
9                $folder = $web.GetFolder($sourceFolder)
10                 try{
11                        $group = $web.SiteGroups[$GroupName]
12                        $roleAssignment = new-object Microsoft.SharePoint.SPRoleAssignment($group)
13                         $roleDefinition = $web.RoleDefinitions[$PermissionLevel];
14                         $roleAssignment.RoleDefinitionBindings.Add($roleDefinition);
15              
16                         # apply the new roleassignment to the folder.
17                         $folder.Item.RoleAssignments.Add($roleAssignment);
18                         write-host -f green "Group ""$GroupName"" Successfully Added to ""$Url$sourceFolder"""
19 
20                         $folder.Item.Update();
21                         $web.Dispose()
22                     }
23                   catch [System.Exception]
24                   {
25                     write-host -f red "Folder ""$folder"" not found."
26                   }
27             }
28         }
29     catch [System.Exception]
30       {
31         write-host -f red $_.Exception.ToString()
32       }
33     write-host -f magenta "DONE!"
34 }

Lines 18 and 25 do not give the relevant colour output.

I added the last line (33) (Not required) just for testing purposes and it works fine...

Any advice? TIA

3
  • 1
    Try the following way: write-host -foregroundcolor "green" "Group " $GroupName " Successfully Added to " $Url $sourceFolder
    – Aveenav
    Aug 12, 2014 at 13:44
  • 1
    Are you sure you are even getting past the "if" statement (line 7)? If it evaluates as $null, you won't be writing anything, which would also allow line 31 to do its thing ... put a write-host statement at line 6 and see what it prints out. Just a thought. Aug 12, 2014 at 14:42
  • Thanks for the replies. Yes I have tried the -foregroundcolor option both in the front of the line and at the end as seen in many searches. It is passing the statements... I do get the correct "text" output for all three write-host statements,... but as mentioned, 18 and 25 do give their outputs under their correct conditions. The issue is the Color. i.e. - correct text, NO color. Only line 33 shows with color. Hope that makes sense.
    – RaveWolf
    Aug 13, 2014 at 8:27

3 Answers 3

1

Try the following statement :

write-host "Folder" $folder "not found." -ForegroundColor red
0

It seems that errors occur when running the script but since error messages are suppressed in PowerShell in your environment that prevents them (line 25 and 31) from being displayed.

Please make sure the system variable $ErrorActionPreference is not set to SilentlyContinue (see for an explanation below), otherwise set it to Continue (default value) and run the script again:

$ErrorActionPreference= 'continue'

About Error Action Preference

PowerShell halts execution on terminating errors, as mentioned before. For non-terminating errors we have the option to tell PowerShell how to handle these situations. This is where the error action preference comes in. Error Action Preference allows us to specify the desired behavior for a non-terminating error; it can be scoped at the command level or all the way up to the script level.

Available choices for error action preference:

  • SilentlyContinue – error messages are suppressed and execution
    continues.
  • Stop – forces execution to stop, behaving like a terminating error.
  • Continue - the default option. Errors will display and execution will continue.
  • Inquire – prompt the user for input to see if we should proceed.
  • Ignore – (new in v3) – the error is ignored and not logged to the error stream. Has very restricted usage scenarios.

References

An Introduction to Error Handling in PowerShell

0

Sorry for late response, but it seems I know the reason. Probably it's due to your color theme(Appearance tab in settings).

For me, on Solarised Dark theme some colors are just gray. Try another color with -f param (Red -> DarkRed) or another theme.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.