gpt4 book ai didi

powershell - Powershell-过滤WMIObject显示两个脚本结果

转载 作者:行者123 更新时间:2023-12-03 00:15:33 24 4
gpt4 key购买 nike

我是Powershell的新手,现在我有两个脚本,一个是获取远程服务器的IP,另一个是获取远程服务器的特定服务的启动时间,我需要显示远程服务器的IP和特定服务的启动时间,有人可以指导我如何合并这两个脚本。

下面是我的两个脚本。

$servers = gc -path D:\Ted\Computers.txt

$Job = get-wmiobject win32_networkadapterconfiguration -computer $servers -filter "IPEnabled='True'" -asjob

$results = $job | receive-job

$results


get-job | wait-job

receive-job job* | select IPAddress

另一个用于获取服务开始时间的是
$servers = gc -path D:\Ted\Computers.txt

$check = get-wmiobject win32_process -computer $servers -Filter "Name='aspnet_state.exe'" -asjob

$results = $check | receive-job

$results


get-job | wait-job

receive-job job* | Select-Object name, processId, @{Name="StartTime"; Expression={ $_.ConvertToDateTime( $_.CreationDate )}}

最后,我需要知道一件事,如果我对该脚本使用asjob,那意味着它是多线程执行?

对不起,我的英语不好,谢谢您的帮助。

最佳答案

可能有一种更清洁的方法来执行此操作,但这是我对您的问题的看法。似乎您需要某种方式将每台计算机与两个WMI查询的输出相关联。如果需要使用作业并行运行此程序,则将需要更多的工作,但这是串行版本。

Get-Content -Path D:\Ted\Computers.txt | ForEach-Object {
$ip = Get-WmiObject Win32_NetworkAdapterConfiguration -Computer $_ -Filter "IPEnabled='True'" | Select-Object IPAddress
$process = Get-WmiObject Win32_Process -Computer $_ -Filter "Name='aspnet_state.exe'" | Select-Object Name, ProcessId, @{ Name="StartTime"; Expression = { $_.ConvertToDateTime($_.CreationDate) } }

@{
Computer = $_
Ip = $ip
Name = $process.Name
ProcessId = $process.ProcessId
StartTime = $process.StartTime
}
}

并行版本可能与此类似:
# A collection that stores all the jobs
$AllJobs = @()

# A collection that stores jobs correlated with the computer
$ComputerJobs = Get-Content -Path D:\Ted\Computers.txt | ForEach-Object {
$ipJob = Get-WmiObject Win32_NetworkAdapterConfiguration -Computer $_ -Filter "IPEnabled='True'" -AsJob
$AllJobs += $ipJob

$processJob = Get-WmiObject Win32_Process -Computer $_ -Filter "Name='aspnet_state.exe'"
$AllJobs += $processJob

@{
Computer = $_
IpJob = $ipJob
ProcessJob = $processJob
}
}

# Wait for everything to complete
Wait-Job -Job $AllJobs

# Iterate the correlated collection and expand the results
$ComputerJobs | ForEach-Object {
$ip = Receive-Job -Job $_.IpJob | Select-Object IPAddress
$process = Receive-Job -Job $_.ProcessJob | Select-Object Name, ProcessId, @{ Name="StartTime"; Expression = { $_.ConvertToDateTime($_.CreationDate) } }

@{
Computer = $_.Computer
Ip = $ip
Name = $process.Name
ProcessId = $process.ProcessId
StartTime = $process.StartTime
}
}

关于powershell - Powershell-过滤WMIObject显示两个脚本结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35594347/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com