![Powershell:get-qadcomputer 命令](https://linux22.com/image/618584/Powershell%EF%BC%9Aget-qadcomputer%20%E5%91%BD%E4%BB%A4.png)
Get-QADComputer -NotLoggedOnFor 90 -SearchRoot 'doman.name/OU1/OU2'
我想对最后一个 OU 使用一个变量,例如:
Get-QADComputer -NotLoggedOnFor 90 -SearchRoot 'doman.name/OU1/$OU2'
我收到上述代码的错误“无法解析目录对象”。有没有办法以这种方式使用变量,或者有更好的方法来做到这一点?
答案1
在 Powershell 中,单引号'
表示变量扩展不应发生在字符串内。您应该尝试使用双引号"
:
Get-QADComputer -NotLoggedOnFor 90 -SearchRoot "doman.name/OU1/$OU2"
来自文档:
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.