Salt * Wet * Bytes

June 29, 2007

Windows networking script: Set show icon for all NICs

Filed under: AdminScripts — saltwetfish @ 4:07 am
Tags: , ,

In the Internet, there is a number of examples of how to set an individual NIC to show icon in notification area, but you cannot find one if you want to set ALL the NICs by default. This is a required for my work and, hence, modified some of the scripts on the net and using WMI registry class to do the task.

You can download the VBS (please rename doc to vbs) script here

ShowIcon VBS Script

The WriteToLog() is my own function (show you some other days), you can replace it with WScript.echo.

Also note, I stopped indent after some levels so that it does not wrap around too much

‘==========================================
Sub SetShowIconValue
‘==========================================

Const HKLM = &H80000002

Dim ServerName
Dim objReg

Dim CtrlNetworkKeyPath
Dim arrAdapterGUIDs, AdapterGUID
Dim AdapterGUIDConnKeyPath
Dim AdapterNameValue
Dim AdapterNameStringValue
Dim ShowIconDWORDValue

ServerName = “.”

‘Don’t want any errors here to kill the program
On error resume next

Set objReg=GetObject(“winmgmts:{impersonationLevel=impersonate}!\\” & _
ServerName & “\root\default:StdRegProv”)

‘This path is fixed in Windows

CtrlNetworkKeyPath = “SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}”

‘grab all the subkeys, i.e. the Adapter GUIDs

objReg.EnumKey HKLM, CtrlNetworkKeyPath, arrAdapterGUIDs

for each AdapterGUID in arrAdapterGUIDs

if InStr(AdapterGUID, “{“) > 0 then ‘look for keys that looks like a GUID

AdapterGUIDConnKeyPath = CtrlNetworkKeyPath & “\” & AdapterGUID & “\Connection”

AdapterNameStringValue = “”

objReg.GetStringValue

HKLM,AdapterGUIDConnKeyPath,”Name”,AdapterNameStringValue

if AdapterNameStringValue <> “” then

WriteToLog “Looking at registry path: ” & AdapterGUIDConnKeyPath
WriteToLog “Network Name: ” & AdapterNameStringValue

‘Just set all NICs regardless of their status, no harm done

objReg.SetDWORDValue HKLM,AdapterGUIDConnKeyPath,”ShowIcon”,1

if Err.Number = 0 then
WriteToLog “Show Icon value is set to: 1″

else
WriteToLog “Error ” & CStr(Err.Number) & ” while trying to check ShowIcon value, ” & Err.Description
end if

else

WriteToLog “Cannot get name for network adapter GUID:” & AdapterGUIDConnKeyPath

end if

end if

next

On error goto 0

End Sub

June 26, 2007

Scripting Network configurations

Filed under: AdminScripts — saltwetfish @ 6:55 am
Tags: , ,

I have the most difficult task of writing a script that will perform some post-setup configuration on Windows server that should do the following:

  1. Team required NICs and set the speed/duplex accordingly
  2. Set the IP configuration information for the NIC (teamed) on pubic LAN
  3. Set all NICs to “show icon in notification area”
  4. Disable NICs that are not in use
  5. Rename all NICs to based on our standard
  6. Unbind specific NICs from network protocols and services if not needed
  7. Set the binding order of the NICs

So far the results are pretty good, I have managed to get 1, 2, 4 and 5 completed. 3 is easy, just a bit more coding that I wanted. 6 and 7 are proofing to be an impossible task if you are not a C++ or VBS programmer.

The problem with admin scripting of Windows network is that there is no one consistent interface or provider in which you can perform the various actions. You may need to visit the network via WMI, the registry, explorer|control panel, netsh, vendor specific CLIs and even DDK programming, if you need to achieve all of the tasks above.

Anyway, I will post my scripts here as I get each components completed

January 31, 2007

MS Virtual Server: WMI vs Scripting Object

Filed under: AdminScripts — saltwetfish @ 11:15 am
Tags: , ,

Need to do some work on Virtual Machines on Microsoft Virtual Server and realised that I didn’t have a clue on how to manage VS via scripting. I thought that it would be quite easy to find scripting codes, but here is the list of useful scripting pages I’ve found.

- ActiveXperts
- Techtutorials
- Windows SDK
- VMBlog
- Virtual PC Guy from MSDN
- Jose Aguilar

Anyway, I need to find out the virtual machines names for each of the virtual servers we have in the company and what states they are in. I need this, as I need to perform a routine reboot of these servers after a security patch.

My first try was to use WMI, since I did have a hard time looking for a scripting object for virtual servers. I tried out the WMI codes, but found that it only listed virtual machines that are running, can’t trust WMI:
Set objWMIService = GetObject(“winmgmts:” _
& “{impersonationLevel=impersonate}!\\” & strComputer & _
“\root\vm\virtualserver”)

Set colQuery = objWMIService.ExecQuery _
(“Select * from VirtualMachine”)

For Each objItem in colQuery

WScript.echo objItem.Name

Next

One would think that the code above will list me all virtual machines in the server. Nope, it only lists all virtual machines that are turned on!

A better way is to use scripting object instead:

Set virtualServer = CreateObject(“VirtualServer.Application”)
Set vmCollection = virtualServer.VirtualMachines

For Each vm in vmCollection

WScript.Echo “Name: ” & vm.Name

Next

And I used these codes to shutdown my Virtual servers.

VServerName = MyMSVServer

Set objVS = CreateObject(“VirtualServer.Application”,VServerName)
Set colVMS = objvs.VirtualMachines

c = 0
for each objVM in colVMS

on error resume next
set objGOS = objVM.GuestOS
objGOS.Shutdown()

on error goto 0

Next

Incidentally just found out a PowerShell way of listing virtual machines from the Virtual PC Guy:

$vs=new-object –com VirtualServer.Application –Strict

$result = SetSecurity($vs)

$vms = $vs.VirtualMachines

$result = SetSecurity($vms)

write-host
write-host “The following virtual machines are configured:”
write-host

for ($i = 1; $i –le ($vms.count); $i++) {
$vm = $vms.Item($i)
$result = SetSecurity($vm)
write-host $vm.name
}

write-host

Blog at WordPress.com.