我正在编写一些代码来比较某些测试计算机中安装的软件版本,目前我正在使用PowerShell PS1脚本创建文本文件并与之前创建的基线文本文件进行比较
为了方便最终用户,我想在excel文件中自动执行这些操作,按一下按钮就会得到与基线不匹配的结果
我当前的ps1脚本是
:CreateDefaultApps.ps1 - 在“我的文档”文件夹中创建默认基线DefApp32.txt或DefApp64.txt
cls
$CurDir = $myinvocation.InvocationName | split-path -parent
$DOCDIR = [Environment]::GetFolderPath("MyDocuments")
$TARGETDIR = "$DOCDIR\AppLog"
$COMPNAME = $env:computername
if(!(Test-Path -Path $TARGETDIR ))
{
New-Item -ItemType directory -Path $TARGETDIR
}
cls
if ([Environment]::Is64BitOperatingSystem)
{
Write-Host "64bit Windows Detected"
Write-Host "Collecting Product Information for"$COMPNAME
If (Test-Path $TARGETDIR\DefApp64.txt)
{
Remove-Item $TARGETDIR\DefApp64.txt
}
Else
{
Get-WmiObject -Class Win32_Product | Select-Object -Property Name, Version > $TARGETDIR\DefApp64.txt
}
write-host "File"$TARGETDIR"\DefApp64.txt Created"
}
else
{
Write-Host "32bit Windows Detected"
Write-Host "Collecting Product Information for"$COMPNAME
If (Test-Path $TARGETDIR\DefApp32.txt)
{
Remove-Item $TARGETDIR\DefApp32.txt
}
Else
{
Get-WmiObject -Class Win32_Product | Select-Object -Property Name, Version > $TARGETDIR\DefApp32.txt
}
write-host "File " $TARGETDIR"\DefApp32.txt Created"
}
:CompareApps.ps1创建当前的应用程序列表并与Baseline进行比较
cls
$CurDir = $myinvocation.InvocationName | split-path -parent
Function Abort
{
Write-Host "Script Aborted"
}
$DOCDIR = [Environment]::GetFolderPath("MyDocuments")
$TARGETDIR = "$DOCDIR\AppLog"
$COMPNAME = $env:computername
if(!(Test-Path -Path $TARGETDIR ))
{
Write-Host $TARGETDIR" Folder does not Exist"
Abort
}
Function Finish
{
write-host "Comparing Products Completed"
}
Function CreateCompNameFile
{
Get-WmiObject -Class Win32_Product | Select-Object -Property Name, Version > $TARGETDIR\$COMPNAME.txt
write-host "File"$TARGETDIR\$COMPNAME".txt Created"
}
Function Compare64
{
write-host "Comparing Products with file"$TARGETDIR\$COMPNAME".txt"
Compare-Object -ReferenceObject (Get-Content $TARGETDIR\DefApp64.txt) -DifferenceObject (Get-Content $TARGETDIR\$COMPNAME.txt)
Finish
}
Function Compare32
{
write-host "Comparing Products with file"$TARGETDIR\$COMPNAME".txt"
Compare-Object -ReferenceObject (Get-Content $TARGETDIR\DefApp32.txt) -DifferenceObject (Get-Content $TARGETDIR\$COMPNAME.txt)
Finish
}
cls
if ([environment]::Is64BitOperatingSystem)
{
Write-Host "64bit Windows Detected"
Write-Host "Collecting Product Information for"$COMPNAME
If (Test-Path $TARGETDIR\$COMPNAME.txt)
{
Remove-Item $TARGETDIR\$COMPNAME.txt
CreateCompNameFile
}
else
{
CreateCompNameFile
}
Compare64
}
else
{
Write-Host "32bit Windows Detected"
Write-Host "Collecting Product Information for"$COMPNAME
If (Test-Path $TARGETDIR\$COMPNAME.txt)
{
Remove-Item $TARGETDIR\$COMPNAME.txt
CreateCompNameFile
}
else
{
CreateCompNameFile
}
Compare32
}
我更喜欢通过excel运行此代码并捕获工作表中的列表,然后比较工作表中已有的基线。如何在VBA中执行或使用PowerShell中的命令?
之前我曾经使用过(见How to list all installed applications in to excel)
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & StrComputer & "\root\cimv2")
Set objAllSoftwares = objWMIService.ExecQuery("Select * from Win32_Product")
由于某种原因,它无法检索所有已安装的软件(您可以在“添加/删除程序”中看到),例如7-Zip(64位),GPL Ghost脚本等等 任何帮助将不胜感激。
由于
Dumidu