Now, when it is time to use the secrets, you need a way to convert secure strings back to plain text. This is what this script does: 现在,当需要使用这些机密信息时,您需要一种将安全字符串转换回纯文本的方法。此脚本的操作:
1 2 3 4 5 6 7
$hash = Import-Clixml-Path$Path # important: MUST cast $keys to [string[]] or else you cannot modify the hash # in the loop: [string[]]$keys = $hash.Keys $keys | ForEach-Object { $hash[$_] = [PSCredential]::new('xyz', $hash[$_]).GetNetworkCredential().Password }
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