【问题标题】:Running process AsJob losing information运行进程 AsJob 丢失信息
【发布时间】:2018-05-03 00:56:51
【问题描述】:

我有一个脚本可以获取任何给定服务标签的最后登录信息。我几乎每天都运行它以进行部署。在阅读了有关工作的信息后,我认为将流程写成工作是值得的。我编写的脚本可以返回信息。在比较两个脚本的结果时,我意识到将命令作为作业运行的脚本不会返回计算机列表中每台计算机的信息。

这是我遇到的错误。它似乎会针对每台机器返回此信息,但仍会为其中大多数机器提供信息。 Error

旧脚本返回列表中每台计算机的信息。

#Load Active Directory 
Import-Module activedirectory
#Load list of computers
$Results = @()
$Computer = Get-Content -path 'C:\Scripts\computers.txt'

#Check each computer in the list 
ForEach($ComputerName in $Computer)
    {
    $results += Get-ADComputer -Filter " Name -Like '*$ComputerName*' " -Properties name, samaccountname, lastlogondate, DNSHostName; Start-Sleep -Seconds 3 
     } 

#Export to CSV file
$Results | Select Name, SamAccountName, LastLogonDate, DNSHostName | Export-Csv -NoTypeInformation -Path 'C:\Scripts\output.csv'

新脚本不会返回所有机器上的信息,即使它实际上运行的是相同的命令,只是不同。

#Check each computer in the list 
Write-Host -Foreground Green "Getting last logon information"
foreach($ComputerName in $Computer)
    {
    Start-Job C:\Scripts\GetADComputer.ps1 -ArgumentList $ComputerName
}
    #Wait for jobs
    Get-Job | Wait-Job

    #Get job results
    $Results += Get-Job | Receive-Job

#Export to CSV file
$Results | Select Name, SamAccountName, LastLogonDate, DNSHostName | Export-Csv -NoTypeInformation -Path 'C:\Scripts\output1.csv'

它启动作业时调用的脚本是:

#Allows the $ComputerName variable from the initial script to be used. 
param(
    [Parameter(Mandatory=$True,
    ValueFromPipeline=$True)]
    [string[]]$ComputerName
)

#Load Active Directory 
Import-Module activedirectory

#Retrieves information about the ADComputers
Get-ADComputer -Filter " Name -Like '*$ComputerName*' " -Properties name, samaccountname, lastlogondate, DNSHostName; Start-Sleep -Seconds 3

我可以做些什么来防止信息丢失,或者这是作为工作运行某事的副作用?

【问题讨论】:

  • 好奇:为什么要针对 AD 运行 -Filter 查询而不是搜索 -Identity?
  • 你为多少台电脑做这个?为什么你使用作业而不是生成的过滤器?不能根据 OU、ADGroup 或 OS 进行搜索吗?我在下面提供了一个示例,它显示了另一种方法来做你想做的事。
  • @TheIncorrigible1 由于我们使用的命名约定(它将站点位置和机器用途与服务标签结合起来),我目前正在使用过滤器。尚未确认的机器的所有服务标签,但不是计算机名称,都在我可以从中提取的数据库中*。 *我希望我能够直接从数据库中提取,但我可以得到一个包含所有标签的提取文件。
  • @SteloNLD 这并不是我要针对的机器的静态数量。它每周都在变化,具体取决于对新机器的请求数量。最小值趋于 20 左右,最大值约为 50。

标签: powershell jobs


【解决方案1】:

问题中提供的代码可以在不使用 Jobs 的情况下进行优化。

关于这个问题,我过去在创建许多作业和进行大量远程处理时遇到了一些问题,这会耗尽我的网络端口或超出某些应用程序或协议可能具有的某些最大连接限制。

没有作业的代码示例:

### Script Settings/Params

#Computer Names
$ComputerNames = Get-Content -path '.\ComputerNames.txt'

#Computer Properties to retrieve from AD which we want to export.
$ComputerProperties = @('Name', 'SamAccountName', 'LastLogonDate', 'DNSHostName')

#File Path and Name for our export file.
$ExportFilePath = ".\export.csv"

### Script Execution

#AD Query
$ComputerNames_ADFilter = ($ComputerNames | ForEach-Object {"(Name -like '*$($_)*')"}) -join ' -or '
$ADComputers = Get-ADComputer -filter $ComputerNames_ADFilter -Properties $ComputerProperties

#Export
$ADComputers | Select $ComputerProperties | Export-Csv -NoTypeInformation -Path $ExportFilePath

例如,如果您的 $ServerNames 变量中有以下 @('P-ADDS-','P-SQLS-'),您将获得此过滤器:(Name -like '*P-ADDS-*') -or (Name -like '*P-SQLS-*') 这将获得生产环境中的所有 Active Directory 和 Microsoft SQL 相关服务器。

【讨论】:

  • 感谢您的回复。关于上面的示例:我尝试运行此版本,但出现错误。 Get-ADComputer : The search filter cannot be recognized At line:19 char:16 + ... Computers = Get-ADComputer -Filter $ComputerNames_ADFilter -Propertie ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Get-ADComputer], ADException + FullyQualifiedErrorId : ActiveDirectoryServer:8254,Microsoft.ActiveDirectory.Management.Commands.GetADComputer
  • 我更改了:$ComputerNames_ADFilter = ($ComputerNames | ForEach-Object {"(Name -eq '$($_)')"}) -join ' -or ' 到:$ComputerNames_ADFilter = ($ComputerNames | ForEach-Object {"(Name -Like '$($_)')"}) -join ' -or ' 因为我的计算机文件没有这些计算机的全名(请参阅前面关于命名约定的评论)虽然我尝试了两种方式并得到了同样的错误。
  • @jfmc 不要忘记在使用-like 时将* 字符添加到过滤器例如:"(Name -like '*$($_)*')" 我还更改了答案以更好地反映您的问题,我在powershell 5+ 上对此进行了测试你是哪个版本的?
  • 好收获。我确实忘记了。即使在修复之后。 Get-ADComputer : The search filter cannot be recognized At line:20 char:16 + ... Computers = Get-ADComputer -filter $ComputerNames_ADFilter -Propertie ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Get-ADComputer], ADException + FullyQualifiedErrorId : ActiveDirectoryServer:8254,Microsoft.ActiveDirectory.Management.Commands.GetADComputer
  • 我在第 5 版上像这样运行它$ComputerNames_ADFilter = ($ComputerNames | ForEach-Object {"(Name -like '*$($_)*')"}) -join ' -or ' $ADComputers = Get-ADComputer -Filter $ComputerNames_ADFilter -Properties $ComputerProperties
猜你喜欢
  • 2023-03-04
  • 1970-01-01
  • 2022-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-20
  • 2016-05-05
相关资源
最近更新 更多