基本上我编写了一个脚本,它将输入一个用户名或全名输入,它将输出用户的全名,用户名和计算机名。在AD中,找到用户名称或用户名的唯一位置在计算机的描述范围内。这由变量$ new。
显示我遇到的问题是代码适用于某些人,但不适用于其他人。它仍将提供用户名和全名,但将计算机名留空。我不知道这是否是我的代码的问题,或者它是否在AD中。以下是我正在使用的代码:
$input = Read-Host "Enter username or full name"
if ($input -like "* *") #Used if the user types in the full name
{
$blah = Get-ADUser -Filter{name -like $input}
$new = $input.ToUpper() + "; " + $blah.SamAccountName.ToUpper()
$name = $input.ToUpper()
$username = $blah.SamAccountName.ToUpper()
}
else #Used if the user types in a username
{
$blah = Get-ADUser $input
$new = $blah.name.ToUpper() + "; " + $input.ToUpper()
$name = $blah.name.ToUpper()
$username = $input.ToUpper()
}
$new = $name.ToUpper() + "; " + $username
$output = Get-ADComputer -Filter{description -like $new} -Properties name
$strComputer = $output.Name
Write-Host " "
Write-Host "Name: "$name
Write-Host "Username: "$username
Write-Host "Computer Name: "$strComputer
Write-Host " "
我为我的奇怪变量名称提前道歉。谢谢!
答案 0 :(得分:0)
Get-ADComputer
上的过滤器使用-like
,但不使用任何通配符。这是故意的吗?
由于您要获得多个值,请尝试以下操作:
$strComputer = [System.String]::Join(", ", $output.Name)
答案 1 :(得分:0)
谢谢你们的帮助,你们都指出了我正确的方向。这是我最后根据你们的建议做的,它运作得很好!
$input = Read-Host "Enter username or full name"
if ($input -like "* *") #Used if the user types in the full name
{
$blah = Get-ADUser -Filter{name -like $input}
$new = $input.ToUpper() + "; " + $blah.SamAccountName.ToUpper()
$name = $input.ToUpper()
$username = $blah.SamAccountName.ToUpper()
}
else #Used if the user types in a username
{
$blah = Get-ADUser $input
$new = $blah.name.ToUpper() + "; " + $input.ToUpper()
$name = $blah.name.ToUpper()
$username = $input.ToUpper()
}
$new = $name.ToUpper() + "; " + $username
$outputs = Get-ADComputer -Filter{description -eq $new} -Properties name
foreach ($output in $outputs)
{
$strComputer = $output.Name
Write-Host " "
Write-Host "Name: "$name
Write-Host "Username: "$username
Write-Host "Computer Name: "$strComputer
Write-Host " "
}