Powershell:get-qadcomputer 命令

use*_*037 3 powershell search active-directory

Get-QADComputer -NotLoggedOnFor 90 -SearchRoot 'doman.name/OU1/OU2'
Run Code Online (Sandbox Code Playgroud)

我想为最后一个 OU ex 使用一个变量:

Get-QADComputer -NotLoggedOnFor 90 -SearchRoot 'doman.name/OU1/$OU2'
Run Code Online (Sandbox Code Playgroud)

对于上面的代码,我收到错误“无法解析目录对象”。有没有办法以这种方式使用变量或完全更好的方法来做到这一点?

jsc*_*ott 5

在 Powershell 中,单引号'表示不应在字符串内发生变量扩展。您应该尝试使用双引号"

Get-QADComputer -NotLoggedOnFor 90 -SearchRoot "doman.name/OU1/$OU2"
Run Code Online (Sandbox Code Playgroud)

从文档:

PS C:\> Get-Help about_Quoting
...
SINGLE AND DOUBLE-QUOTED STRINGS
    When you enclose a string in double quotation marks (a double-quoted
    string), variable names that are preceded by a dollar sign ($) are
    replaced with the variable's value before the string is passed to the
    command for processing.
 ...
    When you enclose a string in single-quotation marks (a single-quoted
    string), the string is passed to the command exactly as you type it.
    No substitution is performed.
Run Code Online (Sandbox Code Playgroud)