Powershell脚本,可让我大致了解几个远程系统

如何解决Powershell脚本,可让我大致了解几个远程系统

因此,我正在尝试收集一个脚本,该脚本将概述网络中的多台计算机。到目前为止,我已经整理了一些东西,但是仍然无法使它正常工作。我想要一个包含以下信息的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"

有人可以帮助我或指导我使用类似的脚本,该脚本可以为我提供最终的结果吗?

因此,需要澄清的是,最终结果将与此类似:

enter image description here

解决方法

在您将此内容废弃并使用其他人的脚本之前,我要说您自己做具有很大的价值,您如此接近完成!

事实上,我看到的唯一问题是:如果这要碰到很多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,我想您会发现这要容易得多。

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

,

我喜欢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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-