【发布时间】:2020-08-17 19:01:08
【问题描述】:
所以我正在尝试编写一个脚本,让我对我们网络上的几台机器进行概览。到目前为止,我已经拼凑了一些东西,但仍然无法让它发挥作用。我想要一个包含以下信息的 CSV 文件:
资产/计算机名称(通常我需要用它来记录) 离线/在线状态(我可以只定位在线机器) 当前IP地址(如果名称解析不正确,可以远程使用IP) 当前登录用户(如果没有人登录,请插入 N/A) 操作系统名称(查找较旧的 Windows 7 机器) 操作系统版本(需要更新到我们的标准的内容) 也许是模特(这可能会有所帮助) 特定软件(如 Adobe、WebEx 等)
如果我可以将所有这些信息提取到 CSV 中,然后另存为 Excel 电子表格,这将有助于减少从我使用的其他几个来源/软件中提取这些信息的时间。到目前为止,我有以下内容,但有些项目给我带来了问题。
# Source Text File For List of Computers
Get-Content "listofcomputers.txt" | ForEach-Object{
# Grab Current Status
$pingstatus = ""
if (Test-Connection -BufferSize 32 -Count 1 -ComputerName $_ -Quiet) {
$pingstatus = "Online"
} else {
$pingstatus = "Offline"
}
# Grab Current IP Address
$ip_address = (Test-Connection -ea stop -Count 1 -comp $_).IPV4Address).IPAddresstoString
if (!$ip_address){
$ip_address = "N/A"
}
else{
$ip_address = (Test-Connection -ea stop -Count 1 -comp $_).IPV4Address).IPAddresstoString
}
# Grab OS Name & Version
$os_name = (Get-WmiObject Win32_OperatingSystem -ComputerName $_ ).Caption
if(!$os_name){
$os_name = "The machine is unavailable"
$os_version = "N/A"
}
else{
$os_version = (Get-WmiObject Win32_OperatingSystem -ComputerName $_ ).Version
}
# Create Object Table
New-Object -TypeName PSObject -Property @{
ComputerName = $_
Status = $pingstatus
Address = $ip_address
OSName = $os_name
OSVersion = $os_version
}} | Select ComputerName,Status,Address,OSName,OSVersion |
# Scan For Specific Software
Get-WMIObject -Class win32_product -Filter {Name like "%Microsoft%"} -ComputerName $computers -ErrorAction STOP | Select-Object -Property Name,Version
}
# Export Results to CSV
Export-Csv "z_queryresults.csv" -NoTypeInformation -Encoding UTF8
# Display Results Witin PS Window
$P = Import-Csv -Path .\z_queryresults.csv
$P | Format-Table
# Pause Script & Exit
Read-Host -Prompt "Press Enter to continue"
谁能帮助我或指导我编写一个类似的脚本,让我得到我正在寻找的最终结果?
【问题讨论】:
-
似乎每个人都这样做。 [grin] 所以这是我的 ... >>> 基本远程并行 SystemInfo 演示脚本 - Pastebin.com — pastebin.com/cGL5biWH
-
在我的回答中提出了一些建议。如果您能具体说明您面临的问题,我们可以提供一些指导。
标签: powershell csv hardware remote-access