Powershell - 过滤 WMIObject 显示两个脚本结果
Powershell - filtering WMIObject show two scripts result
我是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的新手,现在我有两个脚本,一个是获取远程服务器的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
}
}