【发布时间】:2016-08-07 14:20:07
【问题描述】:
我正在重写的登录脚本中有以下If 块:
If ($distinguishedname -match 'Joe Bloggs') {
Map-Drive 'X' "\\path\to\drive"
}
If ($distinguishedname -match 'Steve Bloggs') {
Map-Drive 'X' "\\path\to\drive"
}
If ($distinguishedname -match 'Joe Jobs') {
Map-Drive 'X' "\\path\to\drive"
}
这显然需要重写为If/Else 语句(因为每个用户只有一个名字!)但是,我更喜欢以下switch -Regex 方法的外观:
switch -Regex ($distinguishedname) {
'Joe Bloggs' {Map-Drive 'X' "\\path\to\drive"; break}
'Steve Bloggs' {Map-Drive 'X' "\\path\to\drive"; break}
'Joe Jobs' {Map-Drive 'X' "\\path\to\drive"; break}
}
我的问题是 - 以这种方式使用开关是否会影响此功能的性能?它必须比上述(if/if/if)更好,因为不是每次都评估所有可能性,但switch 会比ifelse/ifelse/else 快吗?
【问题讨论】:
-
您可能希望使用组而不是逐个测试用户,但这不适用于您的情况
-
@sodawillow 感谢您的评论 - 我正在脚本中的其他地方测试组,但这里执行的设置对于这些用户来说是独一无二的,所以这似乎是我的最佳方式
标签: performance powershell if-statement switch-statement