Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex ForEach对象中的PowerShell正则表达式_Regex_Powershell - Fatal编程技术网

Regex ForEach对象中的PowerShell正则表达式

Regex ForEach对象中的PowerShell正则表达式,regex,powershell,Regex,Powershell,我正在尝试使用PowerShell和Active Directory从构成机器名一部分的用户名获取机器名。 我们的电脑命名约定如下:XXXZZZ01BLOGGSJ,其中最后一个数字后面的最后一个字符是电脑所有者的用户名 我知道,要使用REGEX从PC名称派生用户名,我可以执行以下操作: "XXXZZZ01BLOGGSJ" -replace '\D*\d*(\w*)', '$1' (结果是BLOGGSJ) 我需要能够做的是通过Active directory从用户名派生计算机名,并已尝试过此版本

我正在尝试使用PowerShell和Active Directory从构成机器名一部分的用户名获取机器名。 我们的电脑命名约定如下:XXXZZZ01BLOGGSJ,其中最后一个数字后面的最后一个字符是电脑所有者的用户名

我知道,要使用REGEX从PC名称派生用户名,我可以执行以下操作:

"XXXZZZ01BLOGGSJ" -replace '\D*\d*(\w*)', '$1'
(结果是BLOGGSJ)

我需要能够做的是通过Active directory从用户名派生计算机名,并已尝试过此版本和各种版本:

"BloggsJ" | % { Get-ADComputer -Server blahblah.com -Filter {name -like "xxxzzz*"} | ? "$_.Name -replace '\D*\d*(\w*)', '$1' -eq '$_'" } | select  Name

这失败了,没有结果,我正在努力找到正确的方法,有什么帮助吗?

您必须将名称插入其中吗?这样做会更简单,比如
$UserName='BloggsJ';获取ADComputer-Server blahblah.com-Filter“name-like'xxxzzz'”|其中{$\uuz.name-match$UserName}|选择-Expand name
作为旁白:最好是。谢谢@TheMadTechnician-由于批量解析,我需要对其进行管道处理。我将尝试使用foreach objectI来调整您的方法,我是这样管理的:$UserList |%{$UserName=$|;Get-ADComputer-Server blahblah.com-Filter“name-like'xxxzzz*”?{$|.name-match$UserName}}选择-扩展名称非常感谢!:-)如果将Get-ADComputer的结果捕获到一个变量中,以便只查询Active Directory一次,您可能会发现脚本执行得更好。目前,您正在为每个用户运行您的广告查询,但几乎可以肯定每次都会得到相同的计算机列表。