【问题标题】:Powershell script that gives me any overview of several remote systemsPowershell 脚本,让我了解几个远程系统
【发布时间】:2020-08-17 19:01:08
【问题描述】:

所以我正在尝试编写一个脚本,让我对我们网络上的几台机器进行概览。到目前为止,我已经拼凑了一些东西,但仍然无法让它发挥作用。我想要一个包含以下信息的 CSV 文件:

资产/计算机名称(通常我需要用它来记录) 离线/在线状态(我可以只定位在线机器) 当前IP地址(如果名称解析不正确,可以远程使用IP) 当前登录用户(如果没有人登录,请插入 N/A) 操作系统名称(查找较旧的 Windows 7 机器) 操作系统版本(需要更新到我们的标准的内容) 也许是模特(这可能会有所帮助) 特定软件(如 Adob​​e、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


【解决方案1】:

我喜欢 FoxDeploy 的回答......但是,当他回答时,我正在编写脚本,所以为了完整起见,我仍然想发布。它包括 Fox 提到的许多建议。一个区别是处理“软件”部分。我有几个脚本可以做这种事情,为了便于分类,您需要确保每一位软件都在自己的行中。以下脚本的结果将在每台 PC 上包含几行。然后,您可以使用 Excel 创建一个数据透视表,以便更好地对您的信息进行排序,例如,如果您只需要 PC 的名称。

根据我的经验,我发现这会产生最有用的结果。下面的脚本与您最初编写的脚本有很大不同,但我希望它可以作为您调整自己的脚本的一个很好的参考。

# Source Text File For List of Computers
$PCs = Get-Content "listofcomputers.txt"

# Define the array that will hold your results
$results = @()

foreach ($pc in $pcs)
{
    if (Test-Connection -ComputerName $pc -Count 1 -ErrorAction SilentlyContinue)
    {
        $ip = (Test-Connection -ComputerName $pc -Count 1 -ErrorAction SilentlyContinue).IPV4Address.IPAddresstoString
        $OS = Invoke-Command -ComputerName $pc -ScriptBlock {Get-CimInstance -ClassName Win32_OperatingSystem}
        $software = Invoke-Command -ComputerName $pc -ScriptBlock {Get-CimInstance -ClassName Win32_Product -Filter 'Name like "%Microsoft%"' -Property Name,Version}
        foreach ($ware in $software)
        {
            
            $results += [pscustomobject]@{
                Name = $pc
                IP = $ip
                OSName = $os.Caption
                Version = $os.Version
                SoftwareName = $ware.Name
                SoftwareVersion = $ware.Version
            }
        }
    }
    else
    {
        $results += [pscustomobject]@{
            Name = $pc
            IP = "Offline"
            OSName = ""
            Version = ""
            Software = ""

        }
    }
}

$results | export-csv -Path "C:\stairway\toheaven.csv" -notypeinformation

【讨论】:

    【解决方案2】:

    在你废弃这个并使用别人的脚本之前,让我说你自己做这件事很有价值,而且你已经接近完成了!

    事实上,我看到的唯一问题是:如果这会影响很多 PC,请不要向Win32_Product 查询软件信息。查看 Win32_Product 会触发 Windows Installer 重新评估该软件是否已实际安装。它会生成大量 Windows 事件日志消息,并且速度明显缓慢/可能会启动 CPU 风扇。事实上,它太糟糕了,一位受人尊敬的社区成员为此写了这篇博文:"Win32_Product is Evil!"

    如果您在设备上安装了 ConfigurationManager,请改用 CCM_InstalledProduct。如果没有,有很多替代方案,但不要让这阻碍你的进步。

    您的脚本看起来很有前途,我只需反转 ForEach-Object 的结构并将其移动到独立的 ForEach($item in $collection) 模式循环中。

    我也会为集合中的每个项目添加一个跟踪项目。否则,它看起来还不错。

    # Source Text File For List of Computers
        Get-Content "listofcomputers.txt"  | ForEach-Object{
    

    变成……

    # Source Text File For List of Computers
    $computerList = Get-Content "listofcomputers.txt"  
    ForEach($computer in $computerList){
    
    #do all of your lookup stuff here
    }
    

    任何对$_ 的引用都应替换为$computer

    接下来,在循环的底部,我将为每台计算机生成一个新对象,例如 [psCustomObject]@{ColumnName=ColumnValue}。因此,要使用您检索到的一些属性,它看起来像:

    $computerList = Get-Content "listofcomputers.txt"  
    ForEach($computer in $computerList){
    
    #do all of your lookup stuff here...
    
    #lookupstuff done, generate output object to show results
    
    $thisComputer = [psCustomObject]@{
     ComputerName=$computer;
     IP =$ip_address;
     OnlineStatus = $pingStatus
     }
    
    $thisComputer #show the output
    }
    

    从这里开始,您可能希望将每个新的输出对象添加到一个数组中,然后将整个数组一次发送到Export-Csv,我想您会发现这会容易得多。

    如果您遇到困难,请发布一个新问题,我们会带您去您想去的地方。

    【讨论】:

      猜你喜欢
      • 2013-10-15
      • 1970-01-01
      • 2011-05-01
      • 2018-08-16
      • 1970-01-01
      相关资源
      最近更新 更多