Get-ADComputer -Filter名称不带变量进行计算机搜索

时间:2016-04-06 11:00:07

标签: powershell

我试图创建一个PowerShell脚本,它允许我通过它在AD中的服务标签找到正确的计算机。 到目前为止,我可以通过在脚本中编写服务标记来实现,但是当我想用变量传递它时,它不允许我这样做,并显示此错误:

  

Get-ADComputer:找不到接受的位置参数   参数' GCX0YY1"'。在行:2 char:1   + Get-ADComputer -Filter' Name-like" *' $ name'"'

$count = 0;
$total_distance = 0;

// the header row
echo "<tr>" . PHP_EOL;

    // Apply the space on the left
    echo "\t<th colspan='" . (count($record) - $count)  . "' class='noBorder' style='text-align:right;'></th>" . PHP_EOL;

    // Provide headers on the last three columns
    echo "\t<th class='border' style='text-align:center;'>Total Distance</th>" . PHP_EOL;
    echo "\t<th class='border' style='text-align:center;'>Stop Name</th>" . PHP_EOL;
    echo "\t<th class='border' style='text-align:center;'>Inter-Distance</th>" . PHP_EOL;

echo "</tr>" . PHP_EOL;

foreach($record as $row) 
{

    // get the values
    $pre_stop = $row['pre_stop'];
    $current_stop_id = $row['current_stop'];
    $inter_distance = $row['inter_distance'];
    $total_distance += $inter_distance;

    // Assume the last stop in the results has the desired stop name
    $cur_stop_name=mysql_query("SELECT stop_name_urd FROM stop WHERE stop_id = '$current_stop_id'");
    while($cur_stop=mysql_fetch_array($cur_stop_name))
    {
        $stop_name = $cur_stop[0]; 
    }

    // Start the row
    echo "<tr>" . PHP_EOL;

    // Apply the space on the left
    echo "\t<td colspan='" . (count($record) - $count) . "' class='noBorder' style='text-align:right;'>" . $stop_name . "</td>" . PHP_EOL;

    // Apply the last three columns
    echo "\t<td class='border' style='text-align:center;'>".$total_distance."</td>" . PHP_EOL;
    echo "\t<td class='border' style='text-align:center;'>" . $stop_name . "</td>" . PHP_EOL;
    echo "\t<td class='border' style='text-align:center;'>" . $inter_distance . "</td>" . PHP_EOL;

    // End the row
    echo "</tr>" . PHP_EOL;

    // increment the count
    $count++;
}

2 个答案:

答案 0 :(得分:2)

如果在字符串上使用单引号,则$name变量无法解析。您可以使用string format(别名-f)来获取变量:

$name = Read-Host 'Write the computers service tag'
Get-ADComputer -Filter ('Name -like "{0}"' -f $name)

答案 1 :(得分:2)

Get-ADComputer -Filter "Name -like '*$name'"