简体   繁体   English

用变量替换字符串

[英]Replacing a string with a variable

I'm trying to use PowerShell to search AD for group names.我正在尝试使用 PowerShell 在 AD 中搜索组名。

Why don't either of these work, the param or the read-host?为什么这些都不起作用,参数或读取主机? Both are passing strings, but the results are empty.两者都在传递字符串,但结果为空。 However, if I replace the variable $ADGroup in the command with an actual group name (a string) and run the command Get-ADGroup... results are provided as expected.但是,如果我将命令中的变量 $ADGroup 替换为实际的组名(字符串)并运行命令 Get-ADGroup...,则会按预期提供结果。 I tried to replace the double quotes with single quotes and get the same results, the command works alone but neither read-host or param provide information.我尝试用单引号替换双引号并获得相同的结果,该命令单独工作,但 read-host 或 param 均不提供信息。 I can't figure out why the string isn't being passed when it's a variable ($ADGroup).我无法弄清楚为什么当它是一个变量($ADGroup)时字符串没有被传递。 Thanks.谢谢。

         param(
            [Parameter(Mandatory=$true)]
            [string]$ADGroup
            )
    
     #one or the other param or read-host
       
        $ADGroup = Read-Host "enter group name"
        
  Get-ADGroup -Filter {name -like "*$ADGroup*"} -Properties * | select -Property Name

PS C:\Windows\system32> Get-ADGroup -Filter {name -like ' GroupName '} -Properties * | PS C:\Windows\system32> Get-ADGroup -Filter {name -like ' GroupName '} -Properties * | select -Property Name select - 属性名称

Name姓名

Results
Results
Results
Results
Results 

This is one of the reasons why using a scriptblock on the -Filter parameter of ActiveDirectory module cmdlets is not recommended.这是建议在ActiveDirectory模块 cmdlet 的-Filter参数上使用脚本块的原因之一。

-Filter
Specifies a query string that retrieves Active Directory objects.指定检索 Active Directory 对象的查询字符串。 This string uses the PowerShell Expression Language syntax.此字符串使用 PowerShell 表达式语言语法。 The PowerShell Expression Language syntax provides rich type-conversion support for value types received by the Filter parameter. PowerShell 表达式语言语法为 Filter 参数接收的值类型提供了丰富的类型转换支持。 The syntax uses an in-order representation, which means that the operator is placed between the operand and the value.该语法使用有序表示,这意味着运算符位于操作数和值之间。

  • Using query string:使用查询字符串:
Get-ADGroup -Filter "name -like '*$ADGroup*'"
  • Using LDAP query string:使用 LDAP 查询字符串:
Get-ADGroup -LDAPFilter "(name=*$ADGroup*)"

Note: Name is a default attribute returned by Get-ADGroup , there is no need to call all attributes ( -Properties * ) as this is highly inefficient.注意: NameGet-ADGroup返回的默认属性,不需要调用所有属性( -Properties * ),因为这是非常低效的。

maybe it doesn't recognize it as a string or the filter is not correct.也许它无法将其识别为字符串或过滤器不正确。

 param(
            [Parameter(Mandatory=$true)]
            [string]$ADGroup
            )
#one or the other param or read-host
$ADGroup = Read-Host "enter group name"
$ADGroup = $ADGroup.ToString()
Get-ADGroup -Filter {name -like "*$ADGroup*"} -Properties * | select -Property Name

or this should do it..或者这应该这样做..

$ADGroup = $ADGroup.ToString()
Get-ADGroup -Filter {name -like "*$ADGroup*"} -Properties * | Select-Object -expandProperty Name

If you try without the properties flag, do you get an error?如果您尝试不使用属性标志,您会收到错误消息吗?

Get-ADGroup -Filter {Name -like "*$ADGroup*"} | Select-Object -expandProperty Name

or或者

Get-ADGroup -Filter {Name -like "*Admin*"} | Select-Object -expandProperty Name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM