验证共享名称
Validate share name
如果输入的用户共享名是 c$ 而没有其他内容,我如何使用 powershell 脚本验证?例如从 \\hostname\c$
中提取共享名称
$sharename = Read-Host "Enter path"
if ( $sharename -eq "c$")
{
"execute script"
}
else
{
"ShareName must be c$"
}
对正则表达式使用 -match
运算符,或像这样对通配符使用 -like
,
if ( $sharename -match "\\\w+\c$") {
"execute script"
}
正则表达式是这样构建的,
\ -> \
\ -> \
\w+ -> at least one word character
\ -> \
c -> letter 'c'
$ -> dollar sign
测试用例
$sharename = '\nomatter\c$'
$sharename -match "\\\w+\c$"
True
$sharename = '\nomatter\C$'
$sharename -match "\\\w+\c$"
True
$sharename = '\nomatter\d$'
$sharename -match "\\\w+\c$"
False
$sharename = '\nomatter\cee$'
$sharename -match "\\\w+\c$"
False
如果输入的用户共享名是 c$ 而没有其他内容,我如何使用 powershell 脚本验证?例如从 \\hostname\c$
中提取共享名称$sharename = Read-Host "Enter path"
if ( $sharename -eq "c$")
{
"execute script"
}
else
{
"ShareName must be c$"
}
对正则表达式使用 -match
运算符,或像这样对通配符使用 -like
,
if ( $sharename -match "\\\w+\c$") {
"execute script"
}
正则表达式是这样构建的,
\ -> \
\ -> \
\w+ -> at least one word character
\ -> \
c -> letter 'c'
$ -> dollar sign
测试用例
$sharename = '\nomatter\c$'
$sharename -match "\\\w+\c$"
True
$sharename = '\nomatter\C$'
$sharename -match "\\\w+\c$"
True
$sharename = '\nomatter\d$'
$sharename -match "\\\w+\c$"
False
$sharename = '\nomatter\cee$'
$sharename -match "\\\w+\c$"
False