Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combine Different .vbs into one

Status
Not open for further replies.

deaconf19

IS-IT--Management
Jan 14, 2004
9
US
First off this forum has been a major help in me learning .vbs thank you so much to all the Mods and users who contribute.

I have 3 different .vbs scripts pulled and modified slightly from the internet to fit my needs.

I have a .gho on a bootable DVD image that deploys. once the user logs in I need a .vbs to run once (This I know how to do already). I use sysprep to change the SID on the machine. Then I need a .vbs to run and do the following steps.
1. Change the IP, GW, SubNet, DNS (InputBox required)
2. Output the New settings and additional settings of other adapters
3. Change the hostname of the machine (InputBox required)
4. delete a folder in c:
5. Reboot the machine
6. Have the .vbs self delete

I have everything together although I feel that it can been a lot cleaner and I am not sure how to do this. If I can’t clean it up the one problem I am running into in combining all these scripts together is when the change IP portion runs the NIC ifnormation starts and converts to cscript but then the .vbs loops and runs the change IP again and it stops runn the .vbs. I am not sure why it is doing this. Any help would be great I love learning so if someone can even point me in the right direction that would be helpful as well.

Code:
strcomputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration Where Description = " & _
"'Broadcom BCM5708C NetXtreme II GigE (NDIS VBD Client)'")

strCount = 1

For Each objitem in colitems
If strCount = 1 Then
strIPAddress = Join(objitem.IPAddress, ",")
IP = stripaddress
strCount = strCount + 1
Else
End If

next

strAnswer=msgbox ("Use " & IP & "?" & vbcrlf & vbcrlf & "If not, you''ll be prompted to specify", vbYesNoCancel)

if strAnswer = vbYes then

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colNetAdapters = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration Where Description = " & _
"'Broadcom BCM5708C NetXtreme II GigE (NDIS VBD Client)'")
strIPAddress = Array("0.0.0.0")
strSubnetMask = Array("0.0.0.0")
strGateway = Array("0.0.0.0")
strGatewayMetric = Array(1)
For Each objNetAdapter in colNetAdapters
errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
If errEnable = 0 Then
WScript.Echo "The IP address has been changed."
Else
WScript.Echo "The IP address was not changed."
End If
Next

elseif strAnswer = vbNo then

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colNetAdapters = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration Where Description = " & _
"'Broadcom BCM5708C NetXtreme II GigE (NDIS VBD Client)'")
strIPAddress = array(inputbox("IP Address"))
'' Array("0.0.0.0")
strSubnetMask = array(inputbox("Subnet Mask"))
'' Array("0.0.0.0")
strGateway = array(inputbox("Gateway"))
'' Array("0.0.0.0")
strGatewayMetric = Array(1)
arrDNSServers = array(inputbox("DNS"))
'' Array("0.0.0.0")
For Each objNetAdapter in colNetAdapters
errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
intSetDNSServer = objNetAdapter.SetDNSServerSearchOrder(arrDNSServers)
If errEnable = 0 Then
Else
WScript.Echo "The IP address could not be changed."
End If
Next
else
msgbox ("Operation Cancelled by User")
End if

WScript.Sleep(5000)

Code:
'Force Script to run using CSCRIPT
Dim oShell
Set oShell = CreateObject("Wscript.Shell")

forceUseCScript

Sub forceUseCScript()
If Not WScript.FullName = WScript.Path & "\cscript.exe" Then
oShell.Popup "Launched using wscript. Relaunching...",3,"WSCRIPT"
oShell.Run "cmd.exe /k " & WScript.Path & "\cscript.exe //NOLOGO " & Chr(34) & WScript.scriptFullName & Chr(34),1,False
WScript.Quit 0
End If
End Sub 

  strComputer = "."

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set nics = objWMIService.ExecQuery _
    ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")

' set a counter to number the NICs, in case you need to display info
' for more than one.
n = 1
WScript.Echo

For Each nic in nics
   WScript.Echo "Network Interface Card: " & n
   WScript.Echo "================="
   WScript.Echo "  Description: " & nic.Description

   WScript.Echo "  Physical (MAC) address: " & nic.MACAddress
   WScript.Echo "  Host name:              " & nic.DNSHostName

  ' display all IP addresses configured for this adapter
   If Not IsNull(nic.IPAddress) Then
      For x = 0 To UBound(nic.IPAddress)
         WScript.Echo "  IP address:             " & nic.IPAddress(x)
      Next
   End If

   If Not IsNull(nic.IPSubnet) Then
      For x = 0 To UBound(nic.IPSubnet)
         WScript.Echo "  Subnet:                 " & nic.IPSubnet(x)
      Next
   End If

   If Not IsNull(nic.DefaultIPGateway) Then
      For x = 0 To UBound(nic.DefaultIPGateway)
         WScript.Echo "  Default gateway:        " & _
             nic.DefaultIPGateway(x)
      Next
   End If

  ' now display DNS configuration information

   WScript.Echo
   WScript.Echo "  DNS Settings"
   WScript.Echo "  ------------"
   WScript.Echo "    DNS search order:"

   If Not IsNull(nic.DNSServerSearchOrder) Then
      For x = 0 To UBound(nic.DNSServerSearchOrder)
         WScript.Echo "      " & nic.DNSServerSearchOrder(x)
      Next
   End If

   WScript.Echo "    DNS domain: " & nic.DNSDomain

   If Not IsNull(nic.DNSDomainSuffixSearchOrder) Then
      For x = 0 To UBound(nic.DNSDomainSuffixSearchOrder)
         WScript.Echo "    DNS suffix search list: " & _
             nic.DNSDomainSuffixSearchOrder(x)
      Next
   End If

  ' now display DHCP information

   WScript.Echo
   WScript.Echo "  DHCP Settings"
   WScript.Echo "  -------------"
   WScript.Echo "    DHCP enabled:        " & nic.DHCPEnabled
   WScript.Echo "    DHCP server:         " & nic.DHCPServer

   If Not IsNull(nic.DHCPLeaseObtained) Then
      utcLeaseObtained = nic.DHCPLeaseObtained
      strLeaseObtained = WMIDateStringToDate(utcLeaseObtained)
   Else
      strLeaseObtained = ""
   End If

   WScript.Echo "    DHCP lease obtained: " & strLeaseObtained

   If Not IsNull(nic.DHCPLeaseExpires) Then
      utcLeaseExpires = nic.DHCPLeaseExpires
      strLeaseExpires = WMIDateStringToDate(utcLeaseExpires)
   Else
      strLeaseExpires = ""
   End If
   WScript.Echo "    DHCP lease expires:  " & strLeaseExpires

  ' now display WINS configuration information
   WScript.Echo
   WScript.Echo "  WINS settings"
   WScript.Echo "  -------------"
   WScript.Echo "    Primary WINS server:   " & nic.WINSPrimaryServer
   WScript.Echo "    Secondary WINS server: " & nic.WINSSecondaryServer
   WScript.Echo

   ' move to the next adapter, if applicable
   n = n + 1
Next

' takes a UTC date string and displays it in a friendlier format
Function WMIDateStringToDate(utcDate)
  WMIDateStringToDate = CDate(Mid(utcDate, 5, 2)  & "/" & _
    Mid(utcDate, 7, 2)  & "/" & _
    Left(utcDate, 4)    & " " & _
    Mid (utcDate, 9, 2) & ":" & _
    Mid(utcDate, 11, 2) & ":" & _
    Mid(utcDate, 13, 2))
End Function
WScript.Sleep(5000)

Code:
Dim FSO, Folder
set FSO=CreateObject("Scripting.FileSystemObject") 
Folder="\Sysprep"
FSO.DeleteFolder(Folder)
wscript.echo Folder,"now deleted"
WScript.Sleep(5000)

Code:
Dim ChangeNameMsg, ChangeNameTitle
ChangeNameMsg = "Please enter New Computername"
ChangeNameTitle = "Rename local Client"
sNewName = InputBox(ChangeNameMsg,ChangeNameTitle)
Set oShell = CreateObject ("WSCript.shell") 
sCCS = "HKLM\SYSTEM\CurrentControlSet\" 
sTcpipParamsRegPath = sCCS & "Services\Tcpip\Parameters\" 
sCompNameRegPath = sCCS & "Control\ComputerName\" 
With oShell 
.RegDelete sTcpipParamsRegPath & "Hostname" 
.RegDelete sTcpipParamsRegPath & "NV Hostname" 
.RegWrite sCompNameRegPath & "ComputerName\ComputerName", sNewName 
.RegWrite sCompNameRegPath & "ActiveComputerName\ComputerName", sNewName 
.RegWrite sTcpipParamsRegPath & "Hostname", sNewName 
.RegWrite sTcpipParamsRegPath & "NV Hostname", sNewName 
End With ' oShell 
MsgBox "Computer name changed"

Code:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate,(Shutdown)}!\\" & _
        strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
    ("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
    ObjOperatingSystem.Reboot()
Next

Set objFSO = CreateObject ( "Scripting.FileSystemObject" )
objFSO.DeleteFile WScript.ScriptFullName
WScript.Quit
 
i would advise you not to combine your scripts. i would advise maintaining one script for every function, one script does one task. you loose flexibility when you combine them, you create yourself more testing when you need to change one little thing if they are combined.
 
Should I use one .vbs to call out each .vbs one at a time?
 
have you not got a task sequence engine / service available to you? something like ADS, SCCM Task Seq, altiris etc?
if not then you should find a simple batch file will do the trick
 
Altiris gets installed after some other files.

I have to use what is available in windows by default after reboot.

This will deploy on a system of sensitive nature and I can't compromise the integrity of the system with other installations not approved.

I will find a way to deploy each .vbs or combine them.

Thank you for your help and suggestions
 
with regards the user input. try and get this right at the start of the build process. have it stored somewhere centrally, on a file share, which the newly built server / client has access to. have your scripts read the input data from the central respository. that way your build process can be fully automated / matic and you dont require a user to logon to the machine and input a load of data for your build to progress
 
I can't do that either. preset data is not allowed. I can't get into details with that I am creating, but it is classified system. the user input is needed since no preset data can be stored since the system is a isolated at the start until the very end.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top