欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

如何用VBS脚本收集远程计算机或本地计算机安装的软件

程序员文章站 2022-06-10 07:52:54
目标用vbs脚本收集域中远程计算机或本地计算机安装的软件,windows版本。并将收集的结果保存到计算机名为文件名的文本文件中。文本文件可以保存到网络路径中或当前vbs文件所在目录。同时支持32位和6...

目标

用vbs脚本收集域中远程计算机或本地计算机安装的软件,windows版本。并将收集的结果保存到计算机名为文件名的文本文件中。文本文件可以保存到网络路径中或当前vbs文件所在目录。同时支持32位和64位系统。
并过滤到一些补丁包、office组件、nvidia、intel®的驱动等。

制作vbs脚本

保存下面的vbs程序代码到vbs文件中

on error resume next  

const hkcu   = &h80000001
const hklm   = &h80000002
const strkeypath = "software\microsoft\windows\currentversion\uninstall\"
const str64keypath = "software\wow6432node\microsoft\windows\currentversion\uninstall\"
const forreading = 1
const forwriting = 2
const forappending = 8

'filepath  = "\\server-file\pcsoftlist\"
filepath  = createobject("scripting.filesystemobject").getfolder(".").path & "\"
set wshell   = createobject("wscript.shell")
set objfso   = createobject("scripting.filesystemobject")

'set collected computers name
set argus=wscript.arguments
if argus.count=0 then
 strcomputername = wshell.regread("hkey_local_machine\system\currentcontrolset\services\tcpip\parameters\hostname")
else
 strcomputername = argus(0)
end if

set textwritefile = objfso.opentextfile(filepath & ucase(strcomputername) &".txt",forwriting,true,true)

set objreg = getobject("winmgmts://" & strcomputername & "/root/default:stdregprov")

'get os version
intret = objreg.getstringvalue(hklm, "software\microsoft\windows nt\currentversion","productname",strosversion)
if intret = 0 then
 intret = objreg.getstringvalue(hklm, "software\microsoft\windows nt\currentversion","csdversion",strosservicepack)
 intret = objreg.getstringvalue(hklm, "software\wow6432node\microsoft\windows nt\currentversion","productname",str64bitosversion)
 if intret = 0 then
  strosversion = strosversion & " 64bit"
 end if
 intret = objreg.getstringvalue(hklm, "system\currentcontrolset\control\nls\language","installlanguage",oslanguagecode)
 if intret = 0 then
  select case oslanguagecode
  case "0804" '中文
   strosversion = strosversion & " chinese version"
  case "0411" '日文
   strosversion = strosversion & " japanese version"
  case "0409" '英文
   strosversion = strosversion & " english version"
  case else '未知语言
   strosversion = strosversion & " unknownlanguage version"
  end select
 end if
else
 strosversion = "os get failed"
 strosservicepack = "nofind"
end if
if instr(lcase(strosversion),"windows")>0 then
 textwritefile.writeline("""" & ucase(strcomputername) & """" & vbtab & """" & strosversion & """" & vbtab & """" & strosservicepack & """")
end if

'display user software.
objreg.enumkey hkcu, strkeypath,arrsubkeys
for each strsubkey in arrsubkeys
 intget = objreg.getdwordvalue(hkcu, strkeypath & strsubkey,"systemcomponent",intsystemcomponent)
 if isnull(intsystemcomponent) then
  intsystemcomponent = 0
 end if
 intret = objreg.getstringvalue(hkcu, strkeypath & strsubkey,"parentdisplayname",strname)
 if intsystemcomponent = 0 and intret > 0 then
  intret = objreg.getstringvalue(hkcu, strkeypath & strsubkey,"displayname",strname)
  if strname <> "" and intret = 0 and ignorepgm(strname) then
   strname = replace(replace(strname,vbcrlf,""),vbtab,"")
   intret = objreg.getstringvalue(hkcu, strkeypath & strsubkey,"displayversion",strversion)
   textwritefile.writeline("""" & ucase(strcomputername) & """" & vbtab & """" & strname & """" & vbtab & """" & strversion & """")
  end if
 end if
next

'display machine 32bit software.
objreg.enumkey hklm, strkeypath,arrsubkeys
for each strsubkey in arrsubkeys
 intget = objreg.getdwordvalue(hklm, strkeypath & strsubkey,"systemcomponent",intsystemcomponent)
 if isnull(intsystemcomponent) then
  intsystemcomponent = 0
 end if
 intret = objreg.getstringvalue(hklm, strkeypath & strsubkey,"parentdisplayname",strname)
 if intsystemcomponent = 0 and intret > 0 then
  intret = objreg.getstringvalue(hklm, strkeypath & strsubkey,"displayname",strname)
  if strname <> "" and intret = 0 and ignorepgm(strname) then '
   strname = replace(replace(strname,vbcrlf,""),vbtab,"")
   intret = objreg.getstringvalue(hklm, strkeypath & strsubkey,"displayversion",strversion)
   textwritefile.writeline("""" & ucase(strcomputername) & """" & vbtab & """" & strname & """" & vbtab & """" & strversion & """")
  end if
 end if
next

'display machine 64bit software.
objreg.enumkey hklm, str64keypath,arrsubkeys
for each strsubkey in arrsubkeys
 intget = objreg.getdwordvalue(hklm, str64keypath & strsubkey,"systemcomponent",intsystemcomponent)
 if isnull(intsystemcomponent) then
  intsystemcomponent = 0
 end if
 intret = objreg.getstringvalue(hklm, str64keypath & strsubkey,"parentdisplayname",strname)
 if intsystemcomponent = 0 and intret > 0 then
  intret = objreg.getstringvalue(hklm, str64keypath & strsubkey,"displayname",strname)
  if strname <> "" and intret = 0 and ignorepgm(strname) then
   strname = replace(replace(strname,vbcrlf,""),vbtab,"")
   intret = objreg.getstringvalue(hklm, str64keypath & strsubkey,"displayversion",strversion)
   textwritefile.writeline("""" & ucase(strcomputername) & """" & vbtab & """" & strname & """" & vbtab & """" & strversion & """")
  end if
 end if
next

textwritefile.close

function ignorepgm(strpgm)
 if instr(1,strpgm,"microsoft office ",1)<=0 then
  '不输出security update、.net framework、microsoft visual c++、nvidia、intel(r)的程序
  ignorepgm = instr(1,strpgm,"security update",1)<=0 _
   and instr(1,strpgm,".net framework",1)<=0 _
   and instr(1,strpgm,"microsoft visual c++",1)<=0 _
   and instr(1,strpgm,"nvidia",1)<=0 _
   and instr(1,strpgm,"intel(r)",1)<=0
 else
  '让个版本的office能正常输出
  ignorepgm = instr(1,strpgm,"microsoft office ",1)>0 _
     and (instr(1,strpgm," 2000 ",1)>0 _
      or instr(1,strpgm," 2003 ",1)>0 _
      or (instr(1,strpgm,"microsoft office access ",1)=1 and instr(1,strpgm," mui",1)<=0) _
      or strpgm="microsoft office professional plus 2007" _
      or strpgm="microsoft office professional plus 2010" _
      or strpgm="microsoft office professional plus 2016" _
      or strpgm="microsoft office standard 2007" _
      or strpgm="microsoft office standard 2010" _
      or strpgm="microsoft office standard 2016" _
      or strpgm="microsoft office standard 2019")

 end if
end function

假设保存的文件名为installedsoftlist.vbs。保存在d:\

修改结果文件保存路径。

请修改下列代码

'filepath  = "\\server-file\pcsoftlist\"
filepath  = createobject("scripting.filesystemobject").getfolder(".").path & "\"

当前默认是保存到installedsoftlist.vbs文件所在目录。可以不修改。

修改过滤条件(设置不想显示的程序名)

请修改下列代码

 '不输出security update、.net framework、microsoft visual c++、nvidia、intel(r)的程序
  ignorepgm = instr(1,strpgm,"security update",1)<=0 _
   and instr(1,strpgm,".net framework",1)<=0 _
   and instr(1,strpgm,"microsoft visual c++",1)<=0 _
   and instr(1,strpgm,"nvidia",1)<=0 _
   and instr(1,strpgm,"intel(r)",1)<=0

默认不显示security update、.net framework、microsoft visual c++、nvidia、intel®的程序
测试前可以不修改

测试

测试方法1

收集当前计算机安装的软件

直接双击installedsoftlist.vbs

假设计算机名为pc-name01,会在d:\或指定目录下生成一个名为pc-name01.txt的文件。

测试方法2

收集远程计算机上安装的软件。
在cmd窗口中运行下列命令。(假设计算机名为pc-name02)

cscript d:\installedsoftlist.vbs pc-name02

会在d:\或指定目录下生成一个名为pc-name02.txt的文件。

测试方法2的注意事项

如果运行后得到的pc-name02.txt文件为空文件。请确认下列两项内容。

  • 远程计算机必须于运行脚本的计算机是同一个域的成员计算机
  • 远程计算机的防火墙为关闭状态,或设置了【入站规则】【windows management instrumentation(wmi)】允许

到此这篇关于如何用vbs脚本收集远程计算机或本地计算机安装的软件的文章就介绍到这了,更多相关vbs脚本收集计算机安装的软内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!