Nuta
Dostęp do tej strony wymaga autoryzacji. Możesz spróbować się zalogować lub zmienić katalog.
Dostęp do tej strony wymaga autoryzacji. Możesz spróbować zmienić katalogi.
Poziom ważności: ostrzeżenie
Opis
Jeśli polecenie cmdlet deklaruje atrybut, należy również wywołać metodę SupportsShouldProcessShouldProcess. Naruszenie jest dowolną funkcją, która deklaruje atrybut, SupportsShouldProcess ale nie wykonuje wywołań ShouldProcess ani nie wywołuje ShouldProcess , ale nie deklaruje SupportsShouldProcess.
Aby uzyskać więcej informacji, zobacz następujące artykuły:
- about_Functions_Advanced_Methods
- about_Functions_CmdletBindingAttribute
- Wszystko, co chciałeś wiedzieć o Aplikacji ShouldProcess
Jak
Aby naprawić naruszenie tej reguły, wywołaj ShouldProcess metodę, gdy polecenie cmdlet deklaruje SupportsShouldProcess atrybut. Możesz też dodać SupportsShouldProcess argument atrybutu podczas wywoływania metody ShouldProcess.
Przykład
Nieodpowiednim
function Set-File
{
[CmdletBinding(SupportsShouldProcess=$true)]
Param
(
# Path to file
[Parameter(Mandatory=$true)]
$Path
)
'String' | Out-File -FilePath $Path
}
Odpowiedź prawidłowa
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
}
}