问题描述
因此,我正在尝试收集一个脚本,该脚本将概述网络中的多台计算机。到目前为止,我已经整理了一些东西,但是仍然无法使它正常工作。我想要一个包含以下信息的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"
有人可以帮助我或指导我使用类似的脚本,该脚本可以为我提供最终的结果吗?
解决方法
在您将此内容废弃并使用其他人的脚本之前,我要说您自己做具有很大的价值,您如此接近完成!
事实上,我看到的唯一问题是:如果这要碰到很多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