About Scriptcop Fixers

Several of the problems that ScriptCop uncovers can be fixed automatically. It's possible to write "Fixers", small bits of code that can repair issues. Like Rules, Fixers are an extensible part of ScriptCop Fixers have a much simpler structure: A fixer should be either a function or a script named like Repair-* and containing only the following 4 parameters: param( # The Rule that flagged the problem [Parameter(Mandatory=$True,ValueFromPipelineByPropertyName=$True)] [ValidateScript({ if ( -isnot [Management.Automation.CommandInfo] -and -isnot [Management.Automation.PSModuleInfo] ) { throw 'Must be a CommandInfo or a PSModuleInfo' } return $True })] [PSObject] ,
# The Problem 
# The Item with the Problem 
if (  -isnot [Management.Automation.CommandInfo] -and 
-isnot [Management.Automation.PSModuleInfo] 
) { 
throw 'Must be a CommandInfo or a PSModuleInfo'             
return $True 
[PSObject] , 

If you'll notice, three of these parameters map to the three pieces of  
information you get when you run Test-Command.  The fourth parameter, -NonInteractive,  
is passed down from Repair-Command. 
If -NotInteractive is passed, your fixer should not interact with the user in any way. 
If is it not, a fixer is free to interact with a user. 
This allows you to write a fixer to get information you could not possibly know automatically  
(like getting a description of what the command does) 
If the fixer doesn't know what to do with the problem, it should ignore it. 
Fixers should identify errors by an ErrorID, which is a property on  . 
In this way, multiple specific errors from a rule can be handled by the same fixer. 
Where as Rules simply need to report errors, fixers need to provide a little more information. 
Since Fixers need to provide this information in a consistent way, they use 2 private functions 
to do the trick:  CouldNotFixProblem and TriedToFixProblem 
There are three different outcomes from a fixer: 
| The problem could not be identified | Do Nothing                            | 
| The problem cannot be fixed         | return CouldNotFixProblem      | 
| The fixer tried to fix the problem  | return TriedToFixProblem       |