Nota:
El acceso a esta página requiere autorización. Puede intentar iniciar sesión o cambiar directorios.
El acceso a esta página requiere autorización. Puede intentar cambiar los directorios.
Nivel de gravedad: Advertencia
Descripción
Esta regla detecta desajustes entre SupportsShouldProcess declaraciones y ShouldProcess llamadas.
Cuando un cmdlet declara el SupportsShouldProcess atributo, también debe llamar al ShouldProcess método.
Las infracciones ocurren cuando:
- Una función declara
SupportsShouldProcesspero no llamaShouldProcess - Una función llama
ShouldProcesspero no declaraSupportsShouldProcess
Para corregir esta violación, asegúrate de que ShouldProcess las llamadas se emparejan con la SupportsShouldProcess declaración de atributo.
Para obtener más información, consulte los artículos siguientes:
- about_Functions_Advanced_Methods
- acerca_de_Functions_CmdletBindingAttribute
- Todo lo que querías saber sobre ShouldProcess
Example
No conforme
function Set-File
{
[CmdletBinding(SupportsShouldProcess=$true)]
Param
(
# Path to file
[Parameter(Mandatory=$true)]
$Path
)
'String' | Out-File -FilePath $Path
}
Compliant
function Set-File
{
[CmdletBinding(SupportsShouldProcess=$true)]
Param
(
# Path to file
[Parameter(Mandatory=$true)]
$Path,
[Parameter(Mandatory=$true)]
[string]$Content
)
if ($PSCmdlet.ShouldProcess($Path, ("Setting content to '{0}'" -f $Content)))
{
$Content | Out-File -FilePath $Path
}
else
{
# Code that should be processed if doing a WhatIf operation
# Must NOT change anything outside of the function / script
}
}