[玩转系统] Iron Scripter 热身解决方案
作者:精品下载站 日期:2024-12-14 08:07:02 浏览:13 分类:玩电脑
Iron Scripter 热身解决方案
我们刚刚结束了 2022 年 PowerShell+DevOps 全球峰会。再次与充满热情的 PowerShell 专业人士在一起真是太棒了。该活动的高潮是 Iron Scripter 挑战赛。您可以在此处了解有关今年活动和获奖者的更多信息。但对于钢铁脚本家来说,还有比这个事件更多的事情。全年,您都可以在 https://ironscripter.us 找到脚本编写挑战来测试您的 PowerShell 技能。我们鼓励您在评论中分享您的解决方案的链接。今天,我针对最近的挑战提出了解决方案。
热身挑战都是关于操纵弦乐的。仅此一点听起来可能并不多。但发现如何做到这一点并将其包装在 PowerShell 函数中的过程才是真正的价值所在。
初级水平:编写 PowerShell 代码以获取类似“PowerShell”的字符串并将其反向显示。
中级水平:采用类似这样的句子:“这就是您如何可以提高您的 PowerShell 技能。”并编写 PowerShell 代码以反转显示整个句子,并且每个单词也反转。您应该能够对文本进行编码和解码。理想情况下,您的函数应该采用管道输入。 为了获得奖励积分,请在反转单词时切换大小写。
反转文本
PowerShell 将每个文本字符串视为一个数组。
PS C:\> $w = "PowerShell"
PS C:\> $w[0]
P
使用范围运算符,我可以反向获取元素。
PS C:\> $w[-1..-($w.length)]
l
l
e
h
S
r
e
w
o
P
使用连接运算符将字符重新组合在一起。
PS C:\> $w[-1..-($w.length)] -join ''
llehSrewoP
通过验证这个核心概念,将其包装在函数中就足够简单了。
Function Invoke-ReverseWord {
[cmdletbinding()]
[OutputType("string")]
Param(
[Parameter(
Position = 0,
Mandatory,
ValueFromPipeline,
HelpMessage = "Enter a word."
)]
[ValidateNotNullOrEmpty()]
[string]$Word
)
Begin {
Write-Verbose "[$((Get-Date).TimeofDay) BEGIN ] Starting $($myinvocation.mycommand)"
} #begin
Process {
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Processing $Word"
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Word length is $($Word.length)"
#get the characters in reverse and join into a string
($Word[-1.. -($Word.length)]) -join ''
} #process
End {
Write-Verbose "[$((Get-Date).TimeofDay) END ] Ending $($myinvocation.mycommand)"
} #end
}
反转句子
句子中的单词反转遵循相同的原则。我可以将句子拆分为单词数组,然后反向访问元素。
PS C:\> $t = "This is how you can improve your PowerShell skills"
PS C:\> $words = $t.split()
PS C:\> $words[-1..-($words.count)]
skills
PowerShell
your
improve
can
you
how
is
This
同样,我可以用空格将单词连接起来。如果我愿意,我也可以颠倒每个单词。这就是这个函数的作用。
Function Invoke-ReverseText {
[CmdletBinding()]
[OutputType("string")]
Param(
[Parameter(
Position = 0,
Mandatory,
ValueFromPipeline,
HelpMessage = "Enter a phrase."
)]
[ValidateNotNullOrEmpty()]
[ValidatePattern("\s")]
[string]$Text
)
Begin {
Write-Verbose "[$((Get-Date).TimeofDay) BEGIN ] Starting $($myinvocation.mycommand)"
} #begin
Process {
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Processing $Text"
#split the phrase on white spaces and reverse each word
$words = $Text.split() | Invoke-ReverseWord
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Reversed $($words.count) words"
($words[-1.. - $($words.count)]) -join " "
} #process
End {
Write-Verbose "[$((Get-Date).TimeofDay) END ] Ending $($myinvocation.mycommand)"
} #end
}
PS C:\> Invoke-ReverseText "This is how you can improve your PowerShell skills"
slliks llehSrewoP ruoy evorpmi nac uoy woh si sihT
PS C:\> Invoke-ReverseText "This is how you can improve your PowerShell skills" | invoke-reversetext
This is how you can improve your PowerShell skills
PS C:\>
切换大小写
弄清楚如何切换大小写有点棘手。从技术上讲,单词中的每个字母都是一个 [Char] 类型对象。
PS C:\> $t = "PowerShell"
PS C:\> $t[0]
P
PS C:\> $t[0].gettype()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Char System.ValueType
[Char] 对象具有数值。
PS C:\> $t[0] -as [int]
80
在我的 en-US 系统上,大写字母字符的范围为 65-90。小写字母为 97-112。
PS C:\> [char]80
P
PS C:\> 100 -as [char]
d
要切换大小写,我需要获取每个字符的数值,然后根据需要调用 toLower() 或 toUpper() 方法。
Function Invoke-ToggleCase {
[cmdletbinding()]
[OutputType("String")]
Param(
[Parameter(
Position = 0,
Mandatory,
ValueFromPipeline,
HelpMessage = "Enter a word."
)]
[ValidateNotNullOrEmpty()]
[string]$Word
)
Begin {
Write-Verbose "[$((Get-Date).TimeofDay) BEGIN ] Starting $($myinvocation.mycommand)"
} #begin
Process {
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Processing $Word"
$Toggled = $Word.ToCharArray() | ForEach-Object {
$i = $_ -as [int]
if ($i -ge 65 -AND $i -le 90) {
#toggle lower
$_.ToString().ToLower()
}
elseif ($i -ge 97 -AND $i -le 122) {
#toggle upper
$_.ToString().ToUpper()
}
else {
$_.ToString()
}
} #foreach-object
#write the new word to the pipeline
$toggled -join ''
} #process
End {
Write-Verbose "[$((Get-Date).TimeofDay) END ] Ending $($myinvocation.mycommand)"
} #end
}
PS C:\> invoke-togglecase $t
pOWERsHELL
把它们放在一起
这是一个可以完成这一切的函数。
Function Invoke-ReverseTextToggle {
[CmdletBinding()]
[OutputType("string")]
Param(
[Parameter(
Position = 0,
Mandatory,
ValueFromPipeline,
HelpMessage = "Enter a phrase."
)]
[ValidateNotNullOrEmpty()]
[ValidatePattern("\s")]
[string]$Text
)
Begin {
Write-Verbose "[$((Get-Date).TimeofDay) BEGIN ] Starting $($myinvocation.mycommand)"
} #begin
Process {
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Processing $Text"
#split the phrase on white spaces and reverse each word
$words = $Text.split() | Invoke-ToggleCase | Invoke-ReverseWord
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Reversed $($words.count) words"
($words[-1.. - $($words.count)]) -join " "
} #process
End {
Write-Verbose "[$((Get-Date).TimeofDay) END ] Ending $($myinvocation.mycommand)"
} #end
}
PS C:\> Invoke-ReverseTextToggle "This is how YOU can improve your PowerShell skills"
SLLIKS LLEHsREWOp RUOY EVORPMI NAC uoy WOH SI SIHt
PS C:\>
PS C:\> Invoke-ReverseTextToggle "This is how YOU can improve your PowerShell skills" | Invoke-ReverseTextToggle
This is how YOU can improve your PowerShell skills
将它们放在一起之后,我意识到有人可能不想要所有的转换。也许他们只想切换大小写和反转单词。这是一个提供灵活性的函数。
Function Convert-Text {
<#
This function relies on some of the previous functions or the functions could be nested
inside the Begin block
Order of operation:
toggle case
reverse word
reverse text
#>
[cmdletbinding()]
Param(
[Parameter(Position = 0, Mandatory, ValueFromPipeline)]
[ValidateNotNullOrEmpty()]
[string]$Text,
[Parameter(HelpMessage = "Reverse each word of text")]
[switch]$ReverseWord,
[Parameter(HelpMessage = "Toggle the case of the text")]
[switch]$ToggleCase,
[Parameter(HelpMessage = "Reverse the entire string of text")]
[switch]$ReverseText
)
Begin {
Write-Verbose "[$((Get-Date).TimeofDay) BEGIN ] Starting $($myinvocation.mycommand)"
} #begin
Process {
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Converting $Text"
$words = $text.Split()
if ($ToggleCase) {
Write-Verbose "toggling case"
$words = $words | Invoke-ToggleCase
}
If ($reverseWord) {
Write-Verbose "reversing words"
$words = $words | Invoke-ReverseWord
}
if ($ReverseText) {
Write-Verbose "reversing text"
$words = ($words[-1.. - $($words.count)])
}
#write the converted text to the pipeline
$words -join " "
} #process
End {
Write-Verbose "[$((Get-Date).TimeofDay) END ] Ending $($myinvocation.mycommand)"
} #end
} #close Convert-Text
正如所写,这个函数依赖于一些早期的函数。但现在,用户有选择。
PS C:\> convert-text "I AM an Iron Scripter!" -ToggleCase
i am AN iRON sCRIPTER!
PS C:\>
PS C:\> convert-text "I AM an Iron Scripter!" -ReverseWord
I MA na norI !retpircS
PS C:\>
PS C:\> convert-text "I AM an Iron Scripter!" -ReverseWord -ToggleCase
i ma NA NORi !RETPIRCs
PS C:\>
PS C:\> convert-text "I AM an Iron Scripter!" -Reversetext -ToggleCase -ReverseWord
!RETPIRCs NORi NA ma i
PS C:\>
PS C:\> convert-text "I AM an Iron Scripter!" -Reversetext -ToggleCase -ReverseWord | convert-text -ReverseWord -ToggleCase -ReverseText
I AM an Iron Scripter!
挑战自己
如果您对我的代码示例有任何疑问,请随时发表评论。我还想强调,我的解决方案并不是实现这些结果的唯一方法。将您的工作与其他人进行比较是有价值的。我强烈鼓励您关注 Iron Scripter 未来的挑战并尽可能多地应对。但是,您不必等待。该网站上存在针对所有 PowerShell 脚本编写级别的大量挑战。评论可能会被关闭,因此您无法分享您的解决方案,但这不应妨碍您提高技能。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag