VBScript mysterium - Why isn't network drives connected at logon? - vbscript

This is one of the strangest things I've ever seen. I've got this logon script that basically disconnects a set of Network drives and then reconnects them. Previously, all users had a batch file set in their AD profile to run. After I wrote the vbscript, I just run that via the batch script. And it's been working just fine.
Now however, I tried to create a GPO and set the vbscript directly in there. And strangely enough, no network drives are connected. So I began poking around, puttinga msgbox right before the drives are connected. And one right after. Immediatly after login I can see the first textbox appear. After I click OK, the drives are supposed to be connected. But they don't. And right after, I can see the second text box.
What's really weird is that if I run the script manually directly after, everything works just fine! I even tried to put a sleep command on the top of the script now, just in case there's some mismatch in the replication of the domain controllers. But that didn't do anything either.
Here's the script as it is right now:
'Run the script
mapNetworkdrives
Public Sub mapNetworkdrives()
' Lag WScript.Network-objekt
Set objNetwork = CreateObject("WScript.Network")
Set objFso = CreateObject("Scripting.FileSystemObject")
'On Error Resume Next
' Fjern eksisterende nettverksdrev først
removeNetworkDrives objFSO, objnetwork
Dim userName
userName = objNetwork.UserName
Dim computerName
computerName = objNetwork.ComputerName
' Sjekk om det er Citrix som blir logget på
If computerName = "JBC" Then
If Not isDriveConnected("S", objFso, objNetwork) = True Then
objNetwork.MapNetworkDrive "S:", "\\sharepoint.ourcompany.no\prosj"
End if
End if
' Sjekk om nettverksdrev er allerede koblet opp
'objNetwork.MapNetworkDrive "Z:", "\\ourcompany.local\files\Brukere\" & username
objNetwork.MapNetworkDrive "P:", "\\ourcompany.local\files\felles"
objNetwork.MapNetworkDrive "Q:", "\\ourcompany.local\files\maler"
objNetwork.MapNetworkDrive "R:", "\\ourcompany\DIY"
objNetwork.MapNetworkDrive "N:", "\\ourcompany\felles\navn"
Set objNetwork = Nothing
Set objFSO = Nothing
End Sub
Public Sub removeNetworkDrives(ByVal objFSO, ByVal objNetwork)
'On Error Resume Next
If isDriveConnected("Z", objFSO) Then
objNetwork.RemoveNetworkDrive "Z:", True, True
End if
If isDriveConnected("P", objFSO) = True Then
objNetwork.RemoveNetworkDrive "P:", True, True
End if
If isDriveConnected("Q", objFSO) = True Then
objNetwork.RemoveNetworkDrive "Q:", True, True
End if
If isDriveConnected("R", objFSO) = True Then
objNetwork.RemoveNetworkDrive "R:", True, True
End if
If isDriveConnected("N", objFSO) = True Then
objNetwork.RemoveNetworkDrive "N:", True, True
End if
Set objNetwork = Nothing
End Sub
Can anybody see anything that I cannot? Am I missing something here? The very same script works just fine if I run the batch file first, which again runs this very same script. The only thing I can think of is that some DNS server might not be ready or something at the time the script is run.

Turns out, this is "normal behaviour" as described here:
http://pcloadletter.co.uk/tag/launchapp-wsf/

It is worth trying checking the privileges of the script.
I've encountered a lot of network shared mapping issues with scheduled/automatic scripts.
Personally, I use batch's "net use" and it solved many weird issues.

Related

VBS If file is open

I've got a simple program than scans data into a spreadsheet along with a timestamp, then you can either update the data by saving, or quit and exit and save.
The only issue I've been stuck on for a day or so is to work around the error handling of the case of the spreadsheet being already open. Id like to have something like this;
if file is open THEn msgbox("File is open, close file and start again")
WScript.Quit
Option Explicit
DIM oFs: Set oFs = CreateObject("Scripting.FileSystemObject")
DIM objExcel, strExcelPath, objSheet
DIM ib
DIM msg1
DIM msg2
strExcelPath = "c:\temp\Example.xls"
Set objExcel = CreateObject("Excel.Application")
objExcel.WorkBooks.Open strExcelPath
Set objSheet = objExcel.ActiveWorkbook.Worksheets(1)
DO
ib=inputbox("SCAN NAME, SCAN LOTS"&vbCrLf&"TO UPDATE,SCAN ""UPDATE."""&vbCrLf&"TO EXIT, SCAN ""QUIT.""","Picklot Passout Database")
IF ib="" THEN
msg1=MsgBox("You must scan either a NAME or LOT NUMBER."&vbCrLf&"If you want to exit, scan QUIT."&vbCrLf&"Click OK to continue.",vbokonly,"Cannot Insert Blank Data")
ELSEIF ib= "QUIT" OR ib= "quit" THEN
objExcel.ActiveWorkbook.Save
objExcel.ActiveWorkbook.Close
objExcel.Application.Quit
set objExcel = Nothing
Set oFs = Nothing
ELSEIF ib="update" OR ib="UPDATE" THEN
objExcel.ActiveWorkbook.Save
msg2=MsgBox("Update Complete.",vbokonly,"Database Updated")
ELSE
objSheet.Range("A2").EntireRow.Insert
objSheet.Cells(2, 1).Value = ib
objSheet.Cells(2, 2).Value=(now)
END IF
LOOP WHILE NOT ib="quit" AND NOT ib="QUIT"
This may help point you in the right direction. Sorry for the rushed, lowercase syntax and unconventional indentations (do not follow my bad practice - keep yours! :D), I wrote it in notepad you see - but it has been tested successfully.
Anyhow, with reference to your code, I have restructured it in a bad manner, familiar to me, adding the functionality you specify. Essentially the task manager application list is checked for a running instance of the "example" Excel file (depending on what version of excel you're using the syntax will differ).
If found it will make it the active window (thereby preventing a read only duplicate instance initiating). If no instance is found it will open "example.xlsx", in this case using a relative path to the script itself. A subroutine is then called to do the business with the cells...
I have written it in such a way to try keep your specs as well as maintain the "OK" and "Cancel" buttons explicitly functional. Please feel free to tinker with this, you may need to address the path and instr lines differently. I hope it helps! All the best.
path=createobject("scripting.filesystemobject").getparentfoldername(wscript.scriptfullname)
excelpath=path&"\example.xlsx"
set objword=createobject("word.application")
set coltasks=objword.tasks
i=0
for each objtask in coltasks
name=lcase(objtask.name)
if instr(name, "microsoft excel - example") then
i=1
end if
next
if i=1 then
wscript.echo "An active instance of ""example.xlsx"" has been found"
set objexcel=getobject(excelpath)
call UPDATER
else
set objexcel=createobject("excel.application")
objexcel.workbooks.open(excelpath)
set objsheet=objexcel.activeworkbook.worksheets(1)
objexcel.visible=true
call UPDATER
end if
sub UPDATER
do
data=inputbox("Please enter data" &vbcrlf&vbcrlf& "To save data & continue, type ""update""" &vbcrlf& "To save data & exit, type ""quit""","Excel DB Updater")
if isempty(data) then
objexcel.activeworkbook.close
objexcel.application.quit
wscript.quit()
elseif lcase(data)="quit" then
objexcel.activeworkbook.save
objexcel.activeworkbook.close
objexcel.application.quit
quit=msgbox("DB Updating complete",vbokonly,"Excel DB Updater")
wscript.quit
elseif lcase(data)="update" then
objexcel.activeworkbook.save
update=msgbox("Data save complete, press OK to continue",vbokonly,"Excel DB Updater")
elseif len(data)<>0 then
objsheet.range("A1").entirerow.insert
objsheet.cells(1, 1).value=data
objsheet.cells(1, 2).value=(now)
add=msgbox("Data added, press OK to continue",vbokonly,"Excel DB Updater")
end if
loop while len(data)>=0 and not lcase(data)="quit"
end sub

VBScript/Classic ASP permission denied with fso.OpenTextFile after several uses

I have a process that loops through a moderate amount of data (1MB) storing it in an array and then periodically writes it to the disk. After several iterations, the script fails at fso.OpenTextFile() in my else section as though the file has not been closed or finished closing from the previous time the function was called. The iteration # doesn't seem to be specific as it's happened anywhere between the 2nd and 10th iteration that I can tell. The file is actually created and being appended to so it doesn't appear to be a permissions issue. I considering adding a time delay to the process but don't want to necessarily add overhead to an already long process.
OS: Windows 2012 R2
Any thoughts or suggestions appreciated.
'Write array to disk
sub writeFile()
'on error resumenext
set fso = Server.CreateObject("Scripting.FileSystemObject")
if needToCreateFile then
set objTextFile = fso.CreateTextFile(server.mappath("google/linklist.html"),true)
objTextFile.writeLine("<!DOCTYPE html>")
objTextFile.writeLine("<html>")
objTextFile.writeLine("<title>")
objTextFile.writeLine("Content Listing")
objTextFile.writeLine("</title>")
needToCreateFile = false
else
' OpenTextFile Method needs a Const value
' ForAppending = 8 ForReading = 1, ForWriting = 2
Set objTextFile = fso.OpenTextFile (filename, ForAppending, True)
end if
'Write contents of array to file
for each link in linkList
if link <>"" and not isNull(link) then
objTextFile.writeLine(link & "<br>")
end if
next
objTextFile.writeLine("</html>")
objTextFile.Close
set fso = nothing
set objTextFile = nothing
'on error goto 0
end sub
Follow Up - Solved
Adding a 3 second delay solved the problem, but significantly delayed the processing time. So, rather than opening and closing the file each time I wanted to write to it, I simply left it open until the entire script was done and thus I didn't need the delay.
sub writeFile()
if needToCreateFile then
set objTextFile = fs.CreateTextFile(server.mappath("google/linklist.html"),true)
objTextFile.writeLine("<!DOCTYPE html>")
objTextFile.writeLine("<html>")
objTextFile.writeLine("<title>")
objTextFile.writeLine("Content Listing")
objTextFile.writeLine("</title>")
needToCreateFile = false
end if
'Write contents of array to file
for each link in linkList
if link <>"" and not isNull(link) then
objTextFile.writeLine(link & "<br>")
end if
next
objTextFile.writeLine("</html>")
' objTextFile.Close
' set fso = nothing
' set objTextFile = nothing
end sub
Adding a 3 second delay solved the problem, but significantly delayed the processing time. So, rather than opening and closing the file each time I wanted to write to it, I simply left it open until the entire script was done and thus I didn't need the delay. See modified script above.

Verify what website I am currently on

Does anyone know of a quick and easy way to verify what website I'm currently on?
I have made some script that logs me into a website, but if the user is already logged on, it will create an error.
If anyone knows a good way to tell if they are already logged in, please let me know!
The script is as follows:
Username = InputBox("Please input username")
Password = InputBox("Please input password")
Set objShell = WScript.CreateObject("WScript.Shell")
Dim IE
Set IE = GetObject("new:{D5E8041D-920F-45e9-B8FB-B1DEB82C6E5E}")
IE.Visible = 1
IE.navigate "http://wsmmart.itg.ti.com/"
Do
WScript.Sleep 250
Loop While IE.ReadyState < 4 And IE.Busy
IE.Document.All.Item("fld2").Value = Username
IE.Document.All.Item("fld5").Value = Password
'IE.Document.All.Item("Submit").Click
The following code will loop through all open IE windows and look to see if any one of them has the term "wsmmart.itg.ti." in the url. If it finds a match it will control that window and proceed to insert your username, pw, etc. If it doesn't find a match then you could run your code to open a new window
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For x = 0 To (IE_count - 1)
On Error Resume Next ' sometimes more web pages are counted than are open
my_url = objShell.Windows(x).Document.Location
If my_url Like "*" & "wsmmart.itg.ti." & "*" Then 'find an existing wsmmart web page
Set ie = objShell.Windows(x)
Exit For
Else
if x=(IE_count - 1) then
' do your stuff to open a new window
end if
End If
Next
on error goto 0
now try to insert your username, pw, etc. , put it in an if statement. if you get an error back, then you're already logged in

QTP Link object does not support the Exist property

I have a weird problem with QTP 11. The following piece of code worked so far, but suddenly stopped working and throws Object does not support this property or method.: 'objPage.Link' for the line with 'if link exist'
Set objBrowser = Browser("creationtime:=" & Desktop.ChildObjects(oDesc).Count - 1 & "")
Set objPage = objBrowser.Page("title:=.*")
If objPage.Link("class:=menu_link_tab_.*", "html id:=.*DesktopTab").Exist(3) Then
msgbox "ok"
End If
Can anyone tell me what is wrong and/or how to do it right?
EDIT: I solved this but still have no idea what happened. I just cut this part from QTP script and pasted it into Notepad and then copied it from Notepad to QTP. Everything works fine. I did not change anything... Any ideas what the hell happened are welcomed.
ANOTHER EDIT: The problem reappears randomly. With no changes to anything I can just run the test 10 times to have it fail randomly with the 'Object does not support this property or method' message
I have a startBrowser function where I set the objPage and objBrowser :
Function startBrowser(vURL)
Dim oDesc
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate vURL
Window("hwnd:=" & IE.HWND).Maximize
Set oDesc = Description.Create
oDesc( "micclass" ).Value = "Browser"
If Desktop.ChildObjects(oDesc).Count > 0 Then
Set objBrowser = Browser("creationtime:=" & Desktop.ChildObjects(oDesc).Count - 1 & "")
End If
Set objPage = objBrowser.Page("title:=.*")
End Function
I have added lots of msgboxes with GetROProperty to verify whether the objects are ok. They seem to be.
Now my function to click the link looks like this :
Function clickMainMenuLink(vIdentifier)
Set objLink = objPage.Link("class:=menu_link_tab_.*", "html id:=.*" & vIdentifier, "index:=0")
If objLink.Exist(timeOut) Then
objLink.Click
Else
reporter.ReportEvent micFail, "Click the " & vIdentifier & " menu link", "Cannot find the " & vIdentifier & " menu link"
ExitTestIteration
End If
End Function
So at the moment my test just opens the browser and clicks a link as I try to debug the thing, but it still fails or passes randomly. The run error indicates line with 'Set objLink'. Any ideas for further debugging? I'm on QTP11 and IE8 if it matters.
From your error does not support this property or method: objPage.Link it would appear that the problem isn't with the .Exist part of line but the .Link part (you can verify this by separating the line into two lines and see which fails
set objLink = objPage.Link("class:=menu_link_tab_.*", "html id:=.*DesktopTab")
If objLink.Exist(3) Then
From your comment it seems that you're creating objPage in a different location from where you're using it, I suggest making sure that the object arrives OK.
Thanks Motti, your reasoning was right. The problem was with the objPage part. Despite the fact that I could read every property of objPage in a function QTP sometimes just did not see this objPage as a Page object. I guess it has something to do with not declaring type explicitly, but that's just a guess. As a dirty workaround I set up the objBrowser and objPage in every function now and it works 100%.

VBS remove printe -- on error resume next

When I run the script below I get an error that the first printer does not exist. This is true, the first printer is not connected, so the output gives me this:
"c:\temp\rmprintlong.vbs(4, 1) WSHNetwork.RemoveNetworkDrive: This network connection does not exist." and then the script dies...this is the issue: the script dies!!!
I still need it to remove the second printer in this script! If it doesn't find one of the printers in the script, I still need it to resume with the next remove printer command!
Here is the code I am using:
Set WshNetwork = WScript.CreateObject("WScript.Network")
PrinterPath = "\\p1\(SW)_HP_LaserJet_8150_Series_PCL"
WshNetwork.RemovePrinterConnection PrinterPath, true, true
PrinterPath = "\\p1\(NE)_HP_LaserJet_5M"
WshNetwork.RemovePrinterConnection PrinterPath, true, true
Wscript.Quit(1)
Do I simply add "On Error Resume Next" under the first line of the code..?
That depends on the quality requirements of your script. If it doesn't matter whether it succeeds or fails, put the OERN in the first line (and meet the standards of the Scripting Guys).
A compromise/minimal impact/pythonic variation of your script:
Set WshNetwork = WScript.CreateObject("WScript.Network")
PrinterPath = "\\p1\(SW)_HP_LaserJet_8150_Series_PCL"
On Error Resume Next ' don't care if this fails
WshNetwork.RemovePrinterConnection PrinterPath, true, true
On Error GoTo 0 ' open eyes again
PrinterPath = "\\p1\(NE)_HP_LaserJet_5M"
WshNetwork.RemovePrinterConnection PrinterPath, true, true
Wscript.Quit 1 ' no parameter list () when calling a sub
This would restrict the "I don't care" section to one single operation.

Resources