In the previous two articles, the basic syntax of static arrays and dynamic arrays is briefly introduced, and an example is illustrated in this article for the use of arrays in daily operations.
First, the demand
In the customer organization has replaced the antivirus software vendor, need to scan the server or whether the client installed anti-virus software, or whether the installation of excessive anti-virus software.
Second, the solution of ideas
We put the names of all the antivirus software that may be installed into an array, and then through the WMI interface we can get all the software that is installed on the computer, and determine whether the software is present in the array.
Third, the script
#****************** defines an array of virus libraries, adding all the software names that may be installed to the array ****************
$AntivirusList = New-object System.Collections.ArrayList
#----NOTE: The full name of the soft kill should be added to the actual use, and the full display is not added
$AntivirusList. ADD ("Symentec") | Out-null
$AntivirusList. ADD ("Nod32") | Out-null
$AntivirusList. ADD ("360") | Out-null
#***************************** Gets the software inventory that is currently installed on the computer *****************************
$SoftwareInstalled = Get-wmiobject-class Win32_Product | Select-object-property Name
#---$Count used to measure the number of installed anti-virus software
$Count = 0
#----to cycle through the installed software to check if any software in the antivirus array has been installed-----
Foreach ($Software in $SoftwareInstalled)
{
if ($AntivirusList-contains ($Software. Name)
{$Count + +}
}
#*************** according to the situation of installing anti-virus software pop-up prompt results ********************
$ws = New-object-comobject Wscript.Shell
If ($Count-eq 0) {
$WSR = $ws. Popup ("Your computer does not have antivirus software installed! ", 5," Anti-virus software installation Check ", 0 + 16)
} elseif ($Count-gt 1)
{
$WSR = $ws. Popup ("It is not recommended that you have too many antivirus software installed on your computer. ", 5," Anti-virus software installation Check ", 0 + 48)
} else
{
$WSR = $ws. Popup ("Your computer has anti-virus software installed. ", 5," Anti-virus software installation Check ", 0 + 48)
}
#************************* Script End *******************************
This article is from the "Essence of the" blog, please be sure to keep this source http://simy88.blog.51cto.com/7370552/1680332
Use of the Powershell Array (c) "script Example a"