воскресенье, 28 июня 2009 г.

Скрипт, подключение принтера в зависимости от настроек сетевого адаптера

"Dim dbg: dbg = True
Dim networkIDs: Set networkIDs = CreateObject("Scipting.Dictionary")
Dim Nacs, Nac
Set Nacs = GetObject("winmgmts://./root/cimv2").ExecQuery(_ "Select IPAddress,IPSubnet from Win32_NetworkAdapterConfiguration" _ & "where IPEnabled = true")
For each Nac in Nacs
Dim N
If ( Not IsNull(Nac.IPAddress)) And (Not IsNull(Nac.IPSubnet)) Then
For N = 0 To UBound(Nac.IPAddress)
networkIDs(NetworkID(Nac.IPAddress(N),Nac.IPSubnet(N))) = vbNullString
Next
End If
Next
Dim IDList: IDList = networkIDs.keys

Dim Locations: Set Locations = CreateObject("Scripting.Dictionary")
Locations("HQ") = "192.168.254.0"
Locations("Br_Moscow") = "192.168.177.0"

Dim SystemLocation
SystemLocation = vbNullString
Dim MultipleMatches
MultipleMatches = False

Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
Dim Session
Session = WshShell.ExpandEnvironmentStrings("%SessionName%")
If Session = "Console" or Session = "%SessionName%" Then
Dim Location
For Each Location in Locations.Keys
Dim curentID
For Each currentID in IDList
"If dbg Then WScript.Echo "ID:", "CurrentID
If currentID = Locations(Location) Then
If dbg Then WScript.Echo "matched", CurrentID
If SystemLocation <> vbNullString Then
MultiplleMatches = True
Else
SystemLocation = Location
End If
End If
Next
Next
End IF

If Not MultipleMatches Then
Dim WshNetwork
Set WshNetwork = CreateObject("WScript.Network")

Select Case SystemLocation
Case "HQ"
`Code to map HQ printers. Example:
`WshNetwork.AddPrinterConnection "Default","\\SRVPRIN\printer"
Case "BR_Moscow"
Case Else
"If dbg then
"Wscript.Echo "No matching location found."
"WScript.Echo "SystemLocation:", SystemLocation
"End If
End Select
End If

Function NetworkID(Address,Mask)
Dim AddressOctets, MaskOctets, Result, N
AddressOctets = Split(Address, ".")
MaskOctets = Split(Mask, ".")
ReDim Result(UBound(AddressOctets))
For N = 0 To UBound(AddressOctets)
Result(N) = AddressOctets(N) And MaskOctets(N)
Next
NetworkID = Join(Result,".")
End Function

Комментариев нет: