PowerShell 区分大小写变量 [英] PowerShell case sensitivity variables
问题描述
我想编写一个脚本来对我公司的用户进行操作.
I want to write a script that does a manipulation on users in my company.
用户名可以是大写/小写字母,有时域名也可以用大写字母分配给他们,所以用户名可以是这样的:域\用户名、域\用户名、域\用户名或域\用户名.
Usernames can be with capital letters/small letters, and also the domain name is sometimes assigned to them with capital letters, so a username can be like: domain\username, DOMAIN\USERNAME, DOMAIN\username or domain\USERNAME.
我这样要求用户名:
$user = Read-Host "Please insert username"
如何使 $user
不区分大小写以及公司名称?
How can I make $user
non case sensitive and also the company name?
用户名需要像 $company\$user
一样,不区分大小写.
The username needs to be like $company\$user
without case sensitivity.
推荐答案
默认情况下,PowerShell 中的字符串比较通常不区分大小写.
String comparisons in PowerShell are typically case-insensitive by default.
字符串本身感知,这意味着它们知道A
与a
是不同的字形,并且它会记住使用的是哪个,但是普通的比较运算符(-eq
、-match
、-like
、-lt
、-in
等)都是不区分大小写的.
Strings themselves are case aware, meaning they know that an A
is a different glyph than an a
and it will remember which was used, but the normal comparison operators (-eq
, -match
, -like
, -lt
, -in
, etc.) are all case-insensitive.
您必须为区分大小写的比较指定区分大小写的版本(-ceq
、-cmatch
、-clike
、-clt
、-cin
等).您还可以指定显式不区分大小写的运算符(-ieq
、-imatch
、-ilike
、-ilt
、-iin
等).
You have to specify the case-sensitive versions for case-sensitive comparisons (-ceq
, -cmatch
, -clike
, -clt
, -cin
, etc.). You can also specify the explicitly case-insensitive operators (-ieq
, -imatch
, -ilike
, -ilt
, -iin
, etc.).
如果要强制字符为特定大小写,可以这样做:
If you want to force the characters to a specific case, you can do this:
#Set characters to lower case
$user = $user.ToLower();
#Set characters to upper case
$user = $user.ToUpper();
但是没有字符串的属性将它们标记为天生不区分大小写.
But there is no property of strings that marks them as inherently case-insensitive.
这篇关于PowerShell 区分大小写变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!