Name ConfirmImpact SupportsShouldProcess -------------------------------------- Get-Random Medium False Remove-Item Medium True Stop-Process Medium True
PS> Get-Command-Verb Remove | Get-ConfirmInfo | Where-Object ConfirmImpact -eq High
Name ConfirmImpact SupportsShouldProcess -------------------------------------- Remove-CIAccessControlRule High True Remove-CIVApp High True Remove-CIVAppNetwork High True Remove-CIVAppTemplate High True Remove-BCDataCacheExtension High True Remove-ClusterAffinityRule High True Remove-ClusterFaultDomain High True (...)
默认情况下,当 $ConfirmImpact 设置为 “High“ 时,PowerShell 将在你运行设置了 ConfirmImpact 为 High 的cmdlet 或函数时自动要求确认(所以你只会在运行像 AD 账户这样不能轻易恢复的东西的 cmdlets 时看到确认对话框弹出)。
作为用户,您可以调整这个风险缓解系统。如果你在一个十分敏感的生产系统上工作,你可能想把 $ConfirmPreference 降低到 Medium 或甚至 Low,以在运行 PowerShell 命令或脚本时获得更多的确认。当设置为 “Low“时,即使创建一个新文件夹也会触发自动确认:
1 2 3 4 5 6 7 8 9 10
PS> $ConfirmPreference = 'Low'
PS> New-Item-Path c:\testfolder
Confirm Are you sure you want to perform this action? Performing the operation "Create File" on target "Destination: C:\testfolder". [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):
$text = "thIS is A TEST teXT" # split text in words $words = $text-split'\s{1,}' | # use ForEach-Object to break down the problem to solving ONE instance of your problem # regardless of how many words there are, the following script block deals with # one word at a time: ForEach-Object { $theWord = $_ # use .NET string methods on the object to solve your issue: $theWord.SubString(0,1).toUpper() + $theWord.SubString(1).ToLower() }
# the result is a string array. Use the -join operator to turn it into ONE string: $result = $words-join' ' $result
process { # split text in words $words = $Text-split'\s{1,}' | # use ForEach-Object to break down the problem to solving ONE instance of your problem # regardless of how many words there are, the following script block deals with # one word at a time: ForEach-Object { $theWord = $_ # use .NET string methods on the object to solve your issue: $theWord.SubString(0,1).toUpper() + $theWord.SubString(1).ToLower() }
# the result is a string array. Use the -join operator to turn it into ONE string: $result = $words-join' ' $result } }
# you get automatic prompts when you forget to submit mandatory arguments: PS> Convert-CapitalizeWord cmdlet Convert-CapitalizeWord at command pipeline position 1 Supply values for the following parameters: Text: heLLO WOrld Hello World
# you can submit your text to the -Text parameter: PS> Convert-CapitalizeWord-Text'this iS a LONG teXT' This Is A Long Text
# you can pipe as many texts as you like (scalable) via the pipeline: PS> 'Hello world!', 'someTHING else' | Convert-CapitalizeWord Hello World! Something Else
$text = "thIS is A TEST teXT" [CultureInfo]::InvariantCulture.TextInfo.ToTitleCase($text) Words that are ALL CAPITALIZED will remain untouched:
This Is A TEST Text
如果您不喜欢有些例外的单词没有被转成首字母大写,那么先将文本转为全小写然后传给该方法:
1 2 3 4 5
PS> [CultureInfo]::InvariantCulture.TextInfo.ToTitleCase('TEST remains aLL uppER Case') TEST Remains All Upper Case
PS> [CultureInfo]::InvariantCulture.TextInfo.ToTitleCase('TEST remains aLL uppER Case unless you lowerCASE YOUR text beFORE'.ToLower()) Test Remains All Upper Case Unless You Lowercase Your Text Before
$text = "thIS is A TEST teXT" # title convert and then replace two or more spaces with one space only: [CultureInfo]::InvariantCulture.TextInfo.ToTitleCase($text.ToLower()) -replace'\s{2,}', ' ' Now this approach returns the exact same result as in our previous parts:
process { [CultureInfo]::InvariantCulture.TextInfo.ToTitleCase($text.ToLower()) -replace'\s{2,}', ' ' } }
当您运行该函数,它将和之前的版本一样灵活和可扩展:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# you get automatic prompts when you forget to submit mandatory arguments: PS> Convert-CapitalizeWord cmdlet Convert-CapitalizeWord at command pipeline position 1 Supply values for the following parameters: Text: heLLO WOrld Hello World
# you can submit your text to the -Text parameter: PS> Convert-CapitalizeWord-Text'this iS a LONG teXT' This Is A Long Text
# you can pipe as many texts as you like (scalable) via the pipeline: PS> 'Hello world!', 'someTHING else' | Convert-CapitalizeWord Hello World! Something Else
$text = "thIS is A TEST teXT" # split text in words $words = $text-split'\s{1,}' | # use ForEach-Object to break down the problem to solving ONE instance of your problem # regardless of how many words there are, the following script block deals with # one word at a time: ForEach-Object { $theWord = $_ # use .NET string methods on the object to solve your issue: $theWord.SubString(0,1).toUpper() + $theWord.SubString(1).ToLower() }
# the result is a string array. Use the -join operator to turn it into ONE string: $result = $words-join' ' $result
$text = "thIS is A TEST teXT" # split text in words $words = $text-split'\s{1,}' | # use ForEach-Object to break down the problem to solving ONE instance of your problem # regardless of how many words there are, the following script block deals with # one word at a time: ForEach-Object { $theWord = $_ # use .NET string methods on the object to solve your issue: $theWord.SubString(0,1).toUpper() + $theWord.SubString(1).ToLower() }
# the result is a string array. Use the -join operator to turn it into ONE string: $result = $words-join' ' $result