Jump to content

Script not functioning on W7 machine


Recommended Posts

Hello,

I created this script for my windows xp 64-bit users and we are now moving them onto windows 7 64 bit. However, when the script runs on logon, it skips the section which its suppose to run and thinks its a 32-bit machine. Seems to run this bit instead:

 Else

' A batch file is used on a 32-Bit OS
Dim wshell
set wshell = CreateObject("WScript.Shell")
wshell.run "cbc_32Bitversion.bat"
set wshell = nothing
End if

CAN YOU SEE A PROBLEM WITH THE SCRIPT? SEE FULL CODE BELOW:

   
Option Explicit

Dim objNetwork,colDrives,i,strComputer, objWMIService, colOperatingSystems, objOperatingSystem, strCaption, colInstalledPrinters, objPrinter, wshShell, strUserName

' Use current machine
strComputer = "."

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

Set colOperatingSystems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")

For Each objOperatingSystem in colOperatingSystems
strCaption = LCase(objOperatingSystem.Caption)

If (InStr(strCaption, "x64") > 0) Then

Set objNetwork = CreateObject("Wscript.Network")
Set colDrives = objNetwork.EnumNetworkDrives
' Force Removal of network drives and remove from user profile
For i = 0 to colDrives.Count-1 Step 2
objNetwork.RemoveNetworkDrive colDrives.Item(i),TRUE,TRUE
Next
' Map network drives to user profile
objNetwork.MapNetworkDrive "P:", "\\server\public", True
objNetwork.MapNetworkDrive "V:", "\\server\J", True
objNetwork.MapNetworkDrive "S:", "\\server\E", True
objNetwork.MapNetworkDrive "G:", "\\server\ca", True
objNetwork.MapNetworkDrive "R:", "\\server\data", True
objNetwork.MapNetworkDrive "I:", "\\server\Inv", True
' Find local printers
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * From Win32_Printer Where Network = True")

' Force Removal of printers from user profile
For Each objPrinter in colInstalledPrinters
objPrinter.Delete_
Next
' Connect printers to user profile
Dim multiPrinter
Set multiPrinter = CreateObject("WScript.Network")
multiPrinter.AddWindowsPrinterConnection "\\server\eng01-64"
multiPrinter.AddWindowsPrinterConnection "\\server\inte01-64"
multiPrinter.AddWindowsPrinterConnection "\\server\cad01-64"
multiPrinter.AddWindowsPrinterConnection "\\server\cad02-64"
multiPrinter.SetDefaultPrinter "\\server\cad02-64"


'Assign users different default printers and mapped drives
Set wshShell = WScript.CreateObject( "WScript.Shell" )
strUserName = wshShell.ExpandEnvironmentStrings( "%USERNAME%" )

If strUserName = "mark" or strUserName = "john" Then
objNetwork.MapNetworkDrive "T:", "\\server\5000", True
End If


Else

' A batch file is used on a 32-Bit OS
Dim wshell
set wshell = CreateObject("WScript.Shell")
wshell.run "cbc_32Bitversion.bat"
set wshell = nothing


End if
Next
WScript.Quit

Edited by gunsmokingman
Added Code Tags
Link to comment
Share on other sites


The first problem you have is

1:\


strCaption = LCase(objOperatingSystem.Caption)

strCaption = Microsoft Windows 7 Ultimate

2:\This will always return the 32 bit


If (InStr(strCaption, "x64") > 0) Then

Perhaps something like this would be better


'-> Without Using strComputer = "."
Dim Wmi :Set Wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")

'-> Using strComputer = "."
'Dim strComputer : strComputer = "."
'Dim Wmi :Set Wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
'strComputer & "\root\cimv2")


Dim Obj, X64 :X64 = False
For Each Obj In Wmi.ExecQuery("SELECT * FROM Win32_Processor")
If Obj.AddressWidth = 64 Then X64 = True
Next

If X64 = True Then
WScript.Echo "Confirm X64"
Else
WScript.Echo "Confirm X32"
End If

I will post a better script later, but the above code should help out.

Here is a updated script


Option Explicit

'-> Objects For Script
Dim Act :Set Act = CreateObject("Wscript.Shell")
Dim Net :Set Net = CreateObject("Wscript.Network")
'-> Without Using strComputer = "."
Dim Wmi :Set Wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")

'-> Using strComputer = "."
'Dim strComputer : strComputer = "."
'Dim Wmi :Set Wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
'strComputer & "\root\cimv2")

'-> Varibles And Check
Dim i, Obj, X64 :X64 = False
For Each Obj In Wmi.ExecQuery("SELECT * FROM Win32_Processor")
If Obj.AddressWidth = 64 Then X64 = True
Next
'-> Confirm x64 Or x32 OS
If X64 = True Then
' WScript.Echo "Confirm X64"
Drives()
Printer()
Else
WScript.Echo "Confirm X32"
End If

Function Drives()
' WScript.Echo "Code Here For Re Map Network Drives"
For i = 0 to Net.EnumNetworkDrives.Count -1 Step 2
Net.RemoveNetworkDrive colDrives.Item(i),TRUE,TRUE
Next
'-> Map network drives to user profile
Net.MapNetworkDrive "P:", "\\server\public", True
Net.MapNetworkDrive "V:", "\\server\J", True
Net.MapNetworkDrive "S:", "\\server\E", True
Net.MapNetworkDrive "G:", "\\server\ca", True
Net.MapNetworkDrive "R:", "\\server\data", True
Net.MapNetworkDrive "I:", "\\server\Inv", True
End Function

Function Printer()
' WScript.Echo "Code Here For Printers"
'-> Remove Printers From User Profile
For Each Obj In Wmi.ExecQuery("Select * From Win32_Printer Where Network = True")
Obj.Delete_
Next
Net.AddWindowsPrinterConnection "\\server\eng01-64"
Net.AddWindowsPrinterConnection "\\server\inte01-64"
Net.AddWindowsPrinterConnection "\\server\cad01-64"
Net.AddWindowsPrinterConnection "\\server\cad02-64"
Net.SetDefaultPrinter "\\server\cad02-64"
'-> Assign Users Different Default Printers And Mapped Drives
Dim User :User = Act.ExpandEnvironmentStrings("%UserName%")
If InStr(1,User, "John",1) Or _
InStr(1,User, "Mark",1) Then
Net.MapNetworkDrive "T:", "\\server\5000", True
End If
End Function

Could you post the batch file code,because you can most likely have it coded to the script, instead of a separae Batch File.

Link to comment
Share on other sites

Please see batch file below, i use this on the users which use 32-bit and 64-bit machine. Ill try the vbs script above, thank you :-)


ECHO OFF
cls
ECHO Initialising user environment ...
echo Running old script
if exist G: net use G: /delete >nul
net use G: \\server\cam /persistent:no

if exist P: net use P: /delete >nul
net use P: \\server\pub /persistent:no

if exist S: net use s: /delete >nul
net use S: \\server\Share/persistent:no

if exist R: net use R: /delete >nul
net use R: \\server\radan/persistent:no

con2prt /f

con2prt /c \\server\print1

con2prt /c \\server\print2

con2prt /c \\server\print3

con2prt /cd \\server\print4

Edited by gunsmokingman
Added Code Box
Link to comment
Share on other sites

Hello,

I only used the batch file because i thought this VBS script wouldnt work on the 32-Bit operating system

When i run the script on a 64-Bit OS i get the following error:

"Line 34

Char 7

Error Variable is undefined: 'colDrives'

Code 800A01F4

Source Microsoft VBScript runtime error "

Thank you,

Richard

Edited by 1cainr
Link to comment
Share on other sites

Ive got the script working by putting two lines in:


Option Explicit

'-> Objects For Script
Dim Act :Set Act = CreateObject("Wscript.Shell")
Dim Net :Set Net = CreateObject("Wscript.Network")
'-> Without Using strComputer = "."
Dim Wmi :Set Wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
[color="#FF0000"]dim coldrives[/color]

'-> Using strComputer = "."
'Dim strComputer : strComputer = "."
'Dim Wmi :Set Wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
'strComputer & "\root\cimv2")

'-> Varibles And Check
Dim i, Obj, X64 :X64 = False
For Each Obj In Wmi.ExecQuery("SELECT * FROM Win32_Processor")
If Obj.AddressWidth = 64 Then X64 = True
Next
'-> Confirm x64 Or x32 OS
If X64 = True Then
' WScript.Echo "Confirm X64"
Drives()
Printer()
Else
WScript.Echo "Confirm X32"
Drives()
Printer()
End If

Function Drives()
Set colDrives = Net.EnumNetworkDrives()

Those two lines make the script work :-) However when i run the script on a 32-bit OS it brings up the error: The parameter is incorrect for "Set colDrives = Net.EnumNetworkDrives()"

Edited by gunsmokingman
Added Code Box
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...