PowerShell script to assign symantec antivirus parent server to multiple clients based on their default gateway
Here is a powershell script translated from my previous VB script and does the same thing.
I use two closely related WMI class: Win32_NetworkAdapterconfiguration and Win32_NetworkAdapter. There is one to one correspondence between instances of the two classes and an implicit division of work between the two classes: Win32_NetworkAdapter exposes mostly hardware related properties and in contrast to Win32_NetworkAdapterconfiguration, includes no methods. There is some overlap between the two classes: for example, both have a MACAddress property that retrieves the physical address of network adapter.
The Win32_NetworkAdapter has a NetConnectionID property(available only on Windows XP and Windows Server 2003) that returns the name of the Network Connection (from Network Connections) that is bound to the network adapter.
What this script does is:
1, To get computer names from MachineList.txt
2, Get all Win32_NetworkAdapter elements into an array $colAdapters and get all Win32_AdapterConfiguration elements into $colConfig
3, Iterate through the arrays and identify specifice network connection (NetConnectionID -eq ‘TEAM) and get the corresponding IP Defaultgateway from Win32_NetworkAdapterConfiguration class.
4, Depending on the Defaultgateway, copy GRC.DAT from the approriate Symantec Antivirus Parent Server and restart the service on the target machine.
$erroractionpreference = "SilentlyContinue"
foreach ($strComputer in get-content C:\MachineList.Txt)
{
write-host $strComputer
$colAdapters = Get-WMIObject Win32_NetworkAdapter -comp $strComputer
$colConfig = Get-WMIObject Win32_NetworkAdapterConfiguration -comp $strComputer
For($i=0;$i -lt $coladapters.length;$i++)
{
if ($coladapters[$i].NetConnectionID -eq 'TEAM')
{
$strDefaultIPGateway = $colconfig[$i].DefaultIPGateway[0]
write-host $strDefaultIPGateway
$arrDefaultIPGateway = $strDefaultIPGateway.split(".")
Write-Host $arrDefaultIPGateway[2]
Switch ($arrDefaultIPGateway[2])
{
161 {write-host $strParentServer($strParentServer = "SERVER01") }
162 {write-host $strParentServer($strParentServer = "SERVER02")}
}
}
}
Function CopyGRC
{
$strSourceFile = "\\$strParentServer\VPHOME\GRC.DAT"
$strDestination = "\\$strComputer\C$\Documents and Settings\All Users\Application Data\Symantec\Symantec AntiVirus Corporate Edition\7.5\"
copy-item $strSourceFile -destination $strDestination -force
Write-host "GRC.DAT is copied over"
}
CopyGRC
Function RestartSav
{
$SavService = get-wmiobject win32_service -comp $strComputer -Filter "Name = 'Symantec Antivirus'"
$savservice.name
$SavService.Stopservice()
#start-sleep -s 30
$SavService.Startservice()
Write-host $savservice.name "Has Been Restarted"
}
RestartSav
$strParentServer=$null
}