简体   繁体   English

循环运行Powershell Get-ADComputer的问题

[英]Issues running Powershell Get-ADComputer in a loop

This is driving me crazy. 这真让我抓狂。 I'm a beginner at Powershell, but I can't see what's wrong here. 我是Powershell的初学者,但看不到这里有什么问题。

I'm basically running a script which goes off and checks the description field in AD, looks for a match for the user input and pulls back the PC name and the associated description. 我基本上正在运行一个脚本,该脚本会关闭并检查AD中的描述字段,查找与用户输入匹配的内容,并拉回PC名称和关联的描述。 As below: 如下:

import-module activedirectory

$User = Read-Host -Prompt "Enter User's First OR Surname OR partial String"
$User = '*' + $User + '*'
Get-ADComputer -Filter 'Description -like $User' -Properties Description | Select Name, Description
write-Host "================================================================="
}

This on it's own works fine. 这本身就很好。 However I want to loop this until it is closed, so the user can do another search straight away. 但是,我想循环此操作,直到将其关闭为止,以便用户可以立即进行其他搜索。 When I put this into a loops, the first time the search is ran, no results are returned. 当我将其放入循环中时,第一次运行搜索时,不会返回任何结果。 The 2nd time the first lot of results are returned with the 2nd lot. 第二次将第一批结果与第二批一起返回。 From then on it works as normal. 从那时起,它就正常工作了。

So the output looks something like: 所以输出看起来像:

Enter User's First OR Surname OR partial String: Test

=================================================================
Enter User's First OR Surname OR partial String: Test
Name      Description                
----      -----------                
COMPXXX01 Test, PS 
COMPXXX01 Test, PS   

=================================================================

So essentially the first lot of results come through on the second run. 因此,基本上,第一批结果要在第二次运行中得出。

I've tried a few ways of looping for example: 我尝试了几种循环方式,例如:

import-module activedirectory

While($true)
{
$User = Read-Host -Prompt "Enter User's First OR Surname OR partial String"
$User = '*' + $User + '*'
Get-ADComputer -Filter 'Description -like $User' -Properties Description | Select Name, Description
write-Host "================================================================="
}

Any advice would be greatly received. 任何建议将不胜感激。

Thanks 谢谢

Interesting find. 有趣的发现。 As far as I can tell there is some sort of bug with the console & Get-ADComputer. 据我所知,控制台和Get-ADComputer存在某种错误。 The Get-Adcomputer does "work" in getting the info ( put in a variable and the contents show there ) but will not output . Get-Adcomputer在获取信息( 放到变量中,内容在那里显示 )中“工作”,但不会输出 Funny if you put the output twice it will work the 2nd time: 有趣的是,如果将输出两次放置,它将第二次运行:

While($true)
{
    $User = Read-Host -Prompt "Enter User's First OR Surname OR partial String"
    $User = '*' + $User + '*'
    Get-ADComputer -Filter 'Description -like $User' -Properties Description | Select Name, Description
    Get-ADComputer -Filter 'Description -like $User' -Properties Description | Select Name, Description
    write-Host "================================================================="
}

A work around would be to have a flag for the 1st time: 解决方法是第一次设置标志:

$firstTime = $true

While($true)
{
    $User = Read-Host -Prompt "Enter User's First OR Surname OR partial String"
    $User = '*' + $User + '*'
    $computerTable = Get-ADComputer -Filter 'Description -like $User' -Properties Description | Select Name, Description    

    if($firstTime){
        $firstTime = $false           
        $computerTable 
    }

    $computerTable

    write-Host "================================================================="
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM