locked
Get-ADComputer variables in SearchBase and Filter RRS feed

  • Question

  • Hi guys,

    Please help with the following question.

    I have the code in my script and it is working.

    $servers = (Get-ADComputer -SearchBase "OU=CRMBilling,OU=Servers,OU=KYIV,DC=corp,DC=ukrtelecom,DC=loc" -Filter { OperatingSystem -Like "*Windows Server*" -and dnshostname -like "kv-crm*" })

    But if I am using

    $ServersFilter    = "{ OperatingSystem -Like `"*Windows Server*`" -and dnshostname -like `"kv-crmapp*`"}"

    $SearchBaseAD = "OU=CRMBilling,OU=Servers,OU=KYIV,DC=corp,DC=ukrtelecom,DC=loc"

    $servers = (Get-ADComputer -SearchBase "'$SearchBaseAD'" -Filter "'$ServersFilter'")

    It is not work.

    How I can use variables in SearchBase and Filter?

    Friday, October 5, 2018 6:28 AM

Answers

  • Please format your code as code here in the forum. Thanks.

    Try this:

    $ServersFilter    = "OperatingSystem -Like '*Windows Server*' -and dnshostname -like 'kv-crmapp*'"
    $SearchBaseAD = 'OU=CRMBilling,OU=Servers,OU=KYIV,DC=corp,DC=ukrtelecom,DC=loc'
    $servers = Get-ADComputer -SearchBase $SearchBaseAD -Filter $ServersFilte


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    • Marked as answer by sashko0330 Friday, October 5, 2018 8:58 AM
    Friday, October 5, 2018 7:14 AM

All replies

  • Please format your code as code here in the forum. Thanks.

    Try this:

    $ServersFilter    = "OperatingSystem -Like '*Windows Server*' -and dnshostname -like 'kv-crmapp*'"
    $SearchBaseAD = 'OU=CRMBilling,OU=Servers,OU=KYIV,DC=corp,DC=ukrtelecom,DC=loc'
    $servers = Get-ADComputer -SearchBase $SearchBaseAD -Filter $ServersFilte


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    • Marked as answer by sashko0330 Friday, October 5, 2018 8:58 AM
    Friday, October 5, 2018 7:14 AM
  • Please format your code as code here in the forum. Thanks.

    Try this:

    $ServersFilter    = "OperatingSystem -Like '*Windows Server*' -and dnshostname -like 'kv-crmapp*'"
    $SearchBaseAD = 'OU=CRMBilling,OU=Servers,OU=KYIV,DC=corp,DC=ukrtelecom,DC=loc'
    $servers = Get-ADComputer -SearchBase $SearchBaseAD -Filter $ServersFilte


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    It is working. Thank you!
    • Marked as answer by sashko0330 Friday, October 5, 2018 8:58 AM
    • Unmarked as answer by sashko0330 Friday, October 5, 2018 8:58 AM
    Friday, October 5, 2018 8:58 AM