(Remote) VBS/Batch Writing/Execution

That makes me sad...I can't edit my post anymore, forcing me to double-post. :(

Oh well, I actually have another snag. ^^'

The below code is for backing up the PST mappings.

Code:
Dim currentUser
Dim strComputer
Dim batchStoreToPath
Dim fileSys
Dim exportVBS
Dim batchStorePath

currentUser = "chris.esbrandt"
strComputer = "testmachine442962"
batchStoreToPath = "\\" & strComputer & "\d$\Documents and Settings\" & currentUser
Set fileSys = CreateObject("Scripting.FileSystemObject")
Set batchStorePath = fileSys.GetFolder(batchStoreToPath)
Set exportVBS = batchStorePath.CreateTextFile("pst.vbs", True)
exportVBS.WriteLine("Const ForWriting = 2")
exportVBS.WriteLine("Const ForAppending = 8")
exportVBS.WriteLine("")
exportVBS.WriteLine("Set objFSO = CreateObject(" & Chr(34) & "Scripting.FileSystemObject" & Chr(34) & ")")
exportVBS.WriteLine("set objNetwork = createobject(" & Chr(34) & "Wscript.Network" & Chr(34) & ")")
exportVBS.WriteLine("")
exportVBS.WriteLine("strFileName = " & Chr(34) & ".\PST-Log.log" & Chr(34) & "")
exportVBS.WriteLine("")
exportVBS.WriteLine("If objFSO.FileExists(strFileName) Then")
exportVBS.WriteLine(" Set objFile = objFSO.OpenTextFile(strFileName, ForAppending)")
exportVBS.WriteLine(" CheckPST")
exportVBS.WriteLine("Else")
exportVBS.WriteLine(" Set objFile = objFSO.CreateTextFile(strFileName, ForWriting)")
exportVBS.WriteLine(" CheckPST")
exportVBS.WriteLine("End If")
exportVBS.WriteLine("objFile.Close")
exportVBS.WriteLine("")
exportVBS.WriteLine("Sub CheckPST")
exportVBS.WriteLine(" Set objOutlook = CreateObject(" & Chr(34) & "Outlook.Application" & Chr(34) & ")")
exportVBS.WriteLine(" Set objMAPI = objOutlook.GetNamespace(" & Chr(34) & "MAPI" & Chr(34) & ")")
exportVBS.WriteLine(" For Each PSTFolder In objMAPI.Folders")
exportVBS.WriteLine("  objFile.Writeline GetPath(PSTFolder.StoreID)")
exportVBS.WriteLine(" Next")
exportVBS.WriteLine("End Sub")
exportVBS.WriteLine("")
exportVBS.WriteLine("Function GetPath(input)")
exportVBS.WriteLine(" For i = 1 To Len(input) Step 2")
exportVBS.WriteLine("  strSubString = Mid(input,i,2)")
exportVBS.WriteLine("  If Not strSubString = " & Chr(34) & "00" & Chr(34) & " Then")
exportVBS.WriteLine("   strPath = strPath & ChrW(" & Chr(34) & "&H" & Chr(34) & " & strSubString)")
exportVBS.WriteLine("  End If")
exportVBS.WriteLine(" Next")
exportVBS.WriteLine("")
exportVBS.WriteLine(" Select Case True")
exportVBS.WriteLine("  Case InStr(strPath," & Chr(34) & ":\" & Chr(34) & ") > 0")
exportVBS.WriteLine("   GetPath = Mid(strPath,InStr(strPath," & Chr(34) & ":\" & Chr(34) & ")-1)")
exportVBS.WriteLine("  Case InStr(strPath," & Chr(34) & "\\" & Chr(34) & ") > 0")
exportVBS.WriteLine("   GetPath = Mid(strPath,InStr(strPath," & Chr(34) & "\\" & Chr(34) & "))")
exportVBS.WriteLine(" End Select")
exportVBS.WriteLine("End Function")
exportVBS.Close

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2:Win32_Process")
objWMIService.Create batchStoreToPath & "pst.vbs", Null, Null, intProcessID

WScript.Quit

I've set it up stand-alone, with all the information provided. Now, the script runs just fine as it is, meaning it does what it is programmed to do (create a .vbs on the remote machine and then execute it), the issue is with the credentials it is executed under. The generated script works perfectly fine when ran manually, meaning the user navigates to the designated directory where it is created and runs it. Through this script, it runs, but with the remote users credentials (administrative rights). Our admin accounts don't have e-mail, and even if they did (I tested with a dummy network where users have higher permissions) it would pull the mapped PSTs for the remote user on that machine (if I had three PSTs mapped for my e-mail on the remote machine it would list my mapped PSTs, not the target user).

That was a mouthful, without the mouth.

So, that's the basic situation. I don't figure out how to tell it to run the script with the regular users credentials without have their password (which we don't use passwords, we use Smart Cards, *sigh*). Any ideas?
 
This is why I mentioned before that a Windows service could be the best idea unfortunately :(. I mean, it could be possible some other way, but with the amount of loop holes and work arounds, how much hassle is enough to justify doing it some other obscure way?

The reason why I say this, in more detail, is because the Windows service itself would run under the credentials specified by that user token session. You wouldn't need to create, those credentials and somehow run a process handle with this tokenized data. However, the beauty of the windows service, is that it would also be able to act as the bond that ties you to every other computer on the network. It would interact with instances of itself on an external location within the same network, and you'd be able to do quite a lot as a result of that communication from computer to .

---------------------

I have to go to sleep though, it's been a long day for me, I kept this as a note to myself to reply to this thread as soon as I got home today, but my work has been overwhelmingly busy, and as well, very very interesting... Very interesting. I've got so many opportunities in this point in my life right now that it's kind of nerve racking. Tomorrow i'm going to a very wealthy friend's invitation to some Roast in celebration of his contributions, so i'm surrounded by some pretty big players in Canada tomorrow night. We just spent over $1000 on things to wear to this event too tonight.

I've got new inquiries for the technical work I do with automation, and i've even possibly got a really huge opportunity to be working on the programming for a future changing concept, which would involve literally billions of dollars for me if we could get the bugs out of their current work. I haven't been able to think straight lately.

(I cannot say much at this point though because what this is, is highly secure, but everyone will want this. This changes the future of HD displays. I would be working on the programming in basically a location like fort knox: I would only be able to work on the programming in this facility, can't leave with a USB key, no Internet, locked and padded doors, that kind of thing so i'm told.)

Forgive me if I'm not really helping, there's just too much about my life at this point in time that I could talk about, but I don't think people would like to hear it lol. It's been on my mind for the past week now, and things just keep getting worse, better, and more strange by the minute. I'm done with my technician contract too Nov. 30th working at the biggest building in my city, so i'll have to find new work, we have to find new tenants for our other house... It's insanity. But somehow I need to decide and manage my main priorities here.
 
It's not a problem, AceInfinity, believe me, I understand. Between the Marine Corps and not NMCI+school, I know all about having too much to do and not enough time or patience to do it all. ^^'

As for the script, I thought I did use a service with
Code:
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2:Win32_Process")
objWMIService.Create batchStoreToPath & "pst.vbs", Null, Null, intProcessID

Perhaps I'm just missing what your actually talking about, but I thought that creating a new process was going to be using the services to do so. I'm more likely just too tired of working on this one script for two weeks and not having it done to think straight, but I couldn't honestly say which. :S



Edit:
Got the Outlook stuff, just going to pull the entire profile from the registry and restore like the network mappings. One of our programmers is reviewing the script, thus far, to see if I'm pulling any information they don't want to have carried over, otherwise, I should be done with the back-up part of this script.
 
Not really, that's more of a WMI object that you've created, from a query to some namespace and Win32_Process. Win32_Process has a number of different values embedded there. This isn't necessarily a Windows Service itself...

You'd use the sc command to create a System service. Or you can simply create the values in the registry yourself.
 
Code:
Option Explicit
Dim strComputer
Dim objWMI

Dim colItems
Dim objItem
Dim VerBig
Dim OSystem
Dim WSHShell

Dim colItems2
Dim objItem2
Dim arrName
Dim domain
Dim currentUser

Dim colItems3
Dim objItem3
Dim SID
Dim objAccount

Dim dateMonth
Dim dateDay
Dim dateYear
Dim endDate

Dim storeToPath
Dim batchStoreToPath
Dim oldStoreToPath
Dim fileSys
Dim batchStorePath
Dim exportBatch

Dim verifyUserInfo

Dim objWMIService
Dim intProcessID

Dim objFSO
Dim oldStorePath
Dim renameBatch
' Here is where we interrogate the Operating System
' On Error Resume Next

' Get the computer name (ie . = localhost)
strComputer = InputBox("Computer Name or IP Address: ")

' Remote connection path
Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

' This is where WMI interrogates the operating system
Set colItems = objWMI.ExecQuery("Select * from Win32_OperatingSystem",,48)

' Here we filter Version from the dozens of properties
For Each objItem in colItems
 VerBig = Left(objItem.Version,3)
Next

' Spot VerBig variable in previous section
' Note the output variable is called OSystem

' This is where WMI interrogates the computer system
Set colItems2 = objWMI.ExecQuery("Select * from Win32_ComputerSystem",,48)
Set WSHShell = CreateObject("WScript.Shell")

Select Case VerBig
 Case "6.1"
  OSystem = "7"
  domain = WshShell.ExpandEnvironmentStrings("%userdomain%")
  currentUser = WshShell.ExpandEnvironmentStrings("%username%")
 Case "5.1"
  OSystem = "XP"
  ' Here we filter UserName from the dozens of properties
  For Each objItem2 in colItems2
   arrName = Split(objItem2.UserName, "\")
   domain = arrName(0)
   currentUser = arrName(1)
  Next
 Case Else OSystem = "Invalid OS"
End Select

Sub pullSID
 ' This is where WMI interrogates the user account
 Set objAccount = GetObject("winmgmts:root/cimv2:Win32_UserAccount.Domain='" & domain & "'" & ",Name='" & currentUser & "'")
 SID = objAccount.SID
End Sub

Call pullSID

If Month(Date) < 10 Then
 dateMonth = "0" & Month(Date)
Else
 dateMonth = Month(Date)
End If
If Day(Date) < 10 Then
 dateDay = "0" & Day(Date)
Else
 dateDay = Day(Date)
End If
dateYear = Right(Year(Date), 2)
endDate = dateMonth & dateDay & dateYear 
Wscript.Echo endDate

Sub setStoragePaths
 ' Spot VerBig variable in previous section
 ' Note the output variable is called storeToPath
 Select Case VerBig
  Case "6.1"
   storeToPath = "C:\Users\" & currentUser
   Select Case strComputer
    Case ".", "localhost", "127.0.0.1"
     batchStoreToPath = storeToPath
     oldStoreToPath = "C:\Users\old." & currentUser & "." & endDate
    Case Else
     batchStoreToPath = "\\" & strComputer & "\c$\Users\" & currentUser
     oldStoreToPath = "\\" & strComputer & "\c$\Users\old." & currentUser & "." & endDate
   End Select
  Case "5.1"
   storeToPath = "D:\Documents and Settings\" & currentUser
   Select Case strComputer
    Case ".", "localhost", "127.0.0.1"
     batchStoreToPath = storeToPath
     oldStoreToPath = "D:\Documents and Settings\old." & currentUser & "." & endDate
    Case Else
     batchStoreToPath = "\\" & strComputer & "\d$\Documents and Settings\" & currentUser
     oldStoreToPath = "\\" & strComputer & "\d$\Documents and Settings\old." & currentUser & "." & endDate
   End Select
 End Select
End Sub

Call setStoragePaths

verifyUserInfo = Msgbox("Is the below user information correct?" & vbCr & vbCr _
 & "Version No: " & VerBig & vbCr _
 & "OS System: " & OSystem & vbCr _
 & "User: " & domain & "\" & currentUser & vbCr _
 & "User SID: " & SID, vbYesNo)

If verifyUserInfo = vbYes Then
 Msgbox "You answered yes."
Else
 domain = InputBox("User's Domain: ")
 currentUser = InputBox("Username: ")
 Call pullSID
 Call setStoragePaths
 Wscript.Echo "Version No: " & VerBig & vbCr _
  & "OS System: " & OSystem & vbCr _
  & "User: " & domain & "\" & currentUser & vbCr _
  & "User SID: " & SID
End If

' This is the creation of a batch file on the remote machine to export the registry entries for network mappings
Set fileSys = CreateObject("Scripting.fileSystemObject")
Set batchStorePath = fileSys.GetFolder(batchStoreToPath)
Set exportBatch = batchStorePath.CreateTextFile("export.bat", True)
exportBatch.WriteLine("@ECHO OFF && SETLOCAL EnableDelayedExpansion")
exportBatch.WriteLine("")
exportBatch.WriteLine("ECHO Exporting data for '" & currentUser & "' with SID '" & SID & "' to '" & storeToPath & "'...")
exportBatch.WriteLine("REG EXPORT HKU\" & SID & "\Network " & Chr(34) & storeToPath & "\1.reg" & Chr(34))
exportBatch.WriteLine("REG EXPORT HKU\" & SID & "\Printers " & Chr(34) & storeToPath & "\2.reg" & Chr(34))
exportBatch.WriteLine("REG EXPORT " & Chr(34) & "HKU\" & SID & "\Software\Microsoft\Windows NT\CurrentVersion\Devices" & Chr(34) & " " & Chr(34) & storeToPath & "\3.reg" & Chr(34))
exportBatch.WriteLine("REG EXPORT " & Chr(34) & "HKU\" & SID & "\Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts" & Chr(34) & " " & Chr(34) & storeToPath & "\4.reg" & Chr(34))
exportBatch.WriteLine("REG EXPORT " & Chr(34) & "HKU\" & SID & "\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles" & Chr(34) & " " & Chr(34) & storeToPath & "\5.reg" & Chr(34))
exportBatch.WriteLine("echo.>>" & Chr(34) & storeToPath & "\Network Mappings.reg" & Chr(34))
exportBatch.WriteLine("type " & Chr(34) & storeToPath & "\1.reg" & Chr(34) & " | find /v " & Chr(34) & "Windows Registry Editor Version 5.00" & Chr(34) & ">>" & Chr(34) & storeToPath & "\Network Mappings.reg" & Chr(34))
exportBatch.WriteLine("type " & Chr(34) & storeToPath & "\2.reg" & Chr(34) & " | find /v " & Chr(34) & "Windows Registry Editor Version 5.00" & Chr(34) & ">>" & Chr(34) & storeToPath & "\Network Mappings.reg" & Chr(34))
exportBatch.WriteLine("type " & Chr(34) & storeToPath & "\3.reg" & Chr(34) & " | find /v " & Chr(34) & "Windows Registry Editor Version 5.00" & Chr(34) & ">>" & Chr(34) & storeToPath & "\Network Mappings.reg" & Chr(34))
exportBatch.WriteLine("type " & Chr(34) & storeToPath & "\4.reg" & Chr(34) & " | find /v " & Chr(34) & "Windows Registry Editor Version 5.00" & Chr(34) & ">>" & Chr(34) & storeToPath & "\Network Mappings.reg" & Chr(34))
exportBatch.WriteLine("type " & Chr(34) & storeToPath & "\5.reg" & Chr(34) & " | find /v " & Chr(34) & "Windows Registry Editor Version 5.00" & Chr(34) & ">>" & Chr(34) & storeToPath & "\Network Mappings.reg" & Chr(34))
exportBatch.WriteLine("del " & Chr(34) & storeToPath & "\1.reg" & Chr(34))
exportBatch.WriteLine("del " & Chr(34) & storeToPath & "\2.reg" & Chr(34))
exportBatch.WriteLine("del " & Chr(34) & storeToPath & "\3.reg" & Chr(34))
exportBatch.WriteLine("del " & Chr(34) & storeToPath & "\4.reg" & Chr(34))
exportBatch.WriteLine("del " & Chr(34) & storeToPath & "\5.reg" & Chr(34))
exportBatch.WriteLine("")
exportBatch.WriteLine("PAUSE && GOTO :EOF")
exportBatch.Close

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2:Win32_Process")
objWMIService.Create batchStoreToPath & "\export.bat", Null, Null, intProcessID

Wscript.Echo "Instruct the customer to log out of them machine." & vbCr & vbCr _
& "Click 'OK' to rename the profile, and if Win7 to remove the SID from the registry and reboot."

Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFolder batchStoreToPath, oldStoreToPath

Select Case VerBig
 Case "6.1"
  Set oldStorePath = fileSys.GetFolder(oldStoreToPath)
  Set renameBatch = oldStorePath.CreateTextFile("rename.bat", True)
  renameBatch.WriteLine("reg delete " & Chr(34) & "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\" & SID & Chr(34))
  renameBatch.WriteLine("shutdown -f -r -t 00")
  renameBatch.Close

  objWMIService.Create oldStorePath & "\rename.bat", Null, Null, intProcessID
End Select

WScript.Quit

' End of script

Everything runs fine until the last part. It creates the file, doesn't error on the location of it, but won't run the batch! I threw in the shutdown command as a way to remotely see if it ran, but it doesn't, and I don't know why! :(
 
Rawr! Stupid edit button disappearing on me!

Code:
Option Explicit
Dim strComputer, objWMI
Dim colItems, objItem, VerBig
Dim colItems2, objItem2, WSHShell
Dim OSystem, domain, currentUser, arrName
Dim objAccount, SID
Dim dateMonth, dateDay, dateYear, endDate
Dim storeToPath, batchStoreToPath, oldStoreToPath
Dim verifyUserInfo
Dim fileSys, batchStorePath, exportBatch
Dim objWMIService, intProcessID
Dim objFSO
Dim oldStorePath, renameBatch

' Here is where we interrogate the Operating System
' On Error Resume Next

' Get the computer name (ie . = localhost)
strComputer = InputBox("Computer Name or IP Address: ")

' Remote connection path
Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

' This is where WMI interrogates the operating system
Set colItems = objWMI.ExecQuery("Select * from Win32_OperatingSystem",,48)

' Here we filter Version from the dozens of properties
For Each objItem in colItems
 VerBig = Left(objItem.Version,3)
Next

' Spot VerBig variable in previous section
' Note the output variable is called OSystem

' This is where WMI interrogates the computer system
Set colItems2 = objWMI.ExecQuery("Select * from Win32_ComputerSystem",,48)
Set WSHShell = CreateObject("WScript.Shell")

Select Case VerBig
 Case "6.1"
  OSystem = "7"
  domain = WshShell.ExpandEnvironmentStrings("%userdomain%")
  currentUser = WshShell.ExpandEnvironmentStrings("%username%")
 Case "5.1"
  OSystem = "XP"
  ' Here we filter UserName from the dozens of properties
  For Each objItem2 in colItems2
   arrName = Split(objItem2.UserName, "\")
   domain = arrName(0)
   currentUser = arrName(1)
  Next
 Case Else OSystem = "Invalid OS"
End Select

Sub pullSID
 ' This is where WMI interrogates the user account
 Set objAccount = GetObject("winmgmts:root/cimv2:Win32_UserAccount.Domain='" & domain & "'" & ",Name='" & currentUser & "'")
 SID = objAccount.SID
End Sub

Call pullSID

If Month(Date) < 10 Then
 dateMonth = "0" & Month(Date)
Else
 dateMonth = Month(Date)
End If
If Day(Date) < 10 Then
 dateDay = "0" & Day(Date)
Else
 dateDay = Day(Date)
End If
dateYear = Right(Year(Date), 2)
endDate = dateMonth & dateDay & dateYear 
Wscript.Echo endDate

Sub setStoragePaths
 ' Spot VerBig variable in previous section
 ' Note the output variable is called storeToPath
 Select Case VerBig
  Case "6.1"
   storeToPath = "C:\Users\" & currentUser
   Select Case strComputer
    Case ".", "localhost", "127.0.0.1"
     batchStoreToPath = storeToPath
     oldStoreToPath = "C:\Users\old." & currentUser & "." & endDate
    Case Else
     batchStoreToPath = "\\" & strComputer & "\c$\Users\" & currentUser
     oldStoreToPath = "\\" & strComputer & "\c$\Users\old." & currentUser & "." & endDate
   End Select
  Case "5.1"
   storeToPath = "D:\Documents and Settings\" & currentUser
   Select Case strComputer
    Case ".", "localhost", "127.0.0.1"
     batchStoreToPath = storeToPath
     oldStoreToPath = "D:\Documents and Settings\old." & currentUser & "." & endDate
    Case Else
     batchStoreToPath = "\\" & strComputer & "\d$\Documents and Settings\" & currentUser
     oldStoreToPath = "\\" & strComputer & "\d$\Documents and Settings\old." & currentUser & "." & endDate
   End Select
 End Select
End Sub

Call setStoragePaths

verifyUserInfo = Msgbox("Is the below user information correct?" & vbCr & vbCr _
 & "Version No: " & VerBig & vbCr _
 & "OS System: " & OSystem & vbCr _
 & "User: " & domain & "\" & currentUser & vbCr _
 & "User SID: " & SID, vbYesNo)

If verifyUserInfo = vbYes Then
 Msgbox "You answered yes."
Else
 domain = InputBox("User's Domain: ")
 currentUser = InputBox("Username: ")
 Call pullSID
 Call setStoragePaths
 Wscript.Echo "Version No: " & VerBig & vbCr _
  & "OS System: " & OSystem & vbCr _
  & "User: " & domain & "\" & currentUser & vbCr _
  & "User SID: " & SID
End If

' This is the creation of a batch file on the remote machine to export the registry entries for network mappings
Set fileSys = CreateObject("Scripting.fileSystemObject")
Set batchStorePath = fileSys.GetFolder(batchStoreToPath)
Set exportBatch = batchStorePath.CreateTextFile("export.bat", True)
exportBatch.WriteLine("@ECHO OFF && SETLOCAL EnableDelayedExpansion")
exportBatch.WriteLine("")
exportBatch.WriteLine("ECHO Exporting data for '" & currentUser & "' with SID '" & SID & "' to '" & storeToPath & "'...")
exportBatch.WriteLine("REG EXPORT HKU\" & SID & "\Network " & Chr(34) & storeToPath & "\1.reg" & Chr(34))
exportBatch.WriteLine("REG EXPORT HKU\" & SID & "\Printers " & Chr(34) & storeToPath & "\2.reg" & Chr(34))
exportBatch.WriteLine("REG EXPORT " & Chr(34) & "HKU\" & SID & "\Software\Microsoft\Windows NT\CurrentVersion\Devices" & Chr(34) & " " & Chr(34) & storeToPath & "\3.reg" & Chr(34))
exportBatch.WriteLine("REG EXPORT " & Chr(34) & "HKU\" & SID & "\Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts" & Chr(34) & " " & Chr(34) & storeToPath & "\4.reg" & Chr(34))
exportBatch.WriteLine("REG EXPORT " & Chr(34) & "HKU\" & SID & "\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles" & Chr(34) & " " & Chr(34) & storeToPath & "\5.reg" & Chr(34))
exportBatch.WriteLine("echo.>>" & Chr(34) & storeToPath & "\Network Mappings.reg" & Chr(34))
exportBatch.WriteLine("type " & Chr(34) & storeToPath & "\1.reg" & Chr(34) & " | find /v " & Chr(34) & "Windows Registry Editor Version 5.00" & Chr(34) & ">>" & Chr(34) & storeToPath & "\Network Mappings.reg" & Chr(34))
exportBatch.WriteLine("type " & Chr(34) & storeToPath & "\2.reg" & Chr(34) & " | find /v " & Chr(34) & "Windows Registry Editor Version 5.00" & Chr(34) & ">>" & Chr(34) & storeToPath & "\Network Mappings.reg" & Chr(34))
exportBatch.WriteLine("type " & Chr(34) & storeToPath & "\3.reg" & Chr(34) & " | find /v " & Chr(34) & "Windows Registry Editor Version 5.00" & Chr(34) & ">>" & Chr(34) & storeToPath & "\Network Mappings.reg" & Chr(34))
exportBatch.WriteLine("type " & Chr(34) & storeToPath & "\4.reg" & Chr(34) & " | find /v " & Chr(34) & "Windows Registry Editor Version 5.00" & Chr(34) & ">>" & Chr(34) & storeToPath & "\Network Mappings.reg" & Chr(34))
exportBatch.WriteLine("type " & Chr(34) & storeToPath & "\5.reg" & Chr(34) & " | find /v " & Chr(34) & "Windows Registry Editor Version 5.00" & Chr(34) & ">>" & Chr(34) & storeToPath & "\Network Mappings.reg" & Chr(34))
exportBatch.WriteLine("del " & Chr(34) & storeToPath & "\1.reg" & Chr(34))
exportBatch.WriteLine("del " & Chr(34) & storeToPath & "\2.reg" & Chr(34))
exportBatch.WriteLine("del " & Chr(34) & storeToPath & "\3.reg" & Chr(34))
exportBatch.WriteLine("del " & Chr(34) & storeToPath & "\4.reg" & Chr(34))
exportBatch.WriteLine("del " & Chr(34) & storeToPath & "\5.reg" & Chr(34))
exportBatch.WriteLine("")
exportBatch.WriteLine("PAUSE && GOTO :EOF")
exportBatch.Close

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2:Win32_Process")
objWMIService.Create batchStoreToPath & "\export.bat", Null, Null, intProcessID

Wscript.Echo "Instruct the customer to log out of them machine." & vbCr & vbCr _
& "Click 'OK' to rename the profile, and if Win7 to remove the SID from the registry and reboot."

Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFolder batchStoreToPath, oldStoreToPath

Select Case VerBig
 Case "6.1"
  Set oldStorePath = fileSys.GetFolder(oldStoreToPath)
  Set renameBatch = oldStorePath.CreateTextFile("rename.bat", True)
  renameBatch.WriteLine("reg delete " & Chr(34) & "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\" & SID & Chr(34) & " /f")
  renameBatch.WriteLine("shutdown -f -r -t 00")
  renameBatch.Close

  objWMIService.Create oldStorePath & "\rename.bat", Null, Null, intProcessID
End Select

WScript.Quit

' End of script

Cleaned up the Dims, made a few minor alterations, etc. Tried editing the reg command in that batch that fails to run to reflect a forced run, thinking maybe it was getting caught at the prompt, no change. This is starting to infuriate me. No error messages, no indications at all that I can see, telling me why it won't run. :(

Got two of our regular programmers looking at it too, so far they got nothing. :S
 
I'll take a look through this in a half hour or so... See what I can find. Make sure that this is your saved version that you are running. Sometimes i've had issues where i've edited the code in my editor, but the changes weren't saved, so when I ran it, it would run differently than what I was seeing in my editor because it was still running the old version of the script. Very very very (very) frustrating. For something so simple of a fix, I have spent quite a bit of time trying to figure out why my script wouldn't work... It does happen, even to the best of us occasionally, no shame in making mistakes once in a while. :thumbsup2:

Aside from that, perhaps the script is running, but output for some reason goes to an unexpected location?

Just some food for thought, before I analyze this.

~Ace
 
Last edited:
lol, I know the feeling, do it all the time. Usually just a slip of the finger when saving.

As for my script, I know that isn't the case. I went ahead and made a "dumbed-down" version:
Code:
Dim strComputer, currentUser, endDate
Dim objWMIService, intProcessID
Dim fileSys, batchStoreToPath, oldStoreToPath, oldStorePath

strComputer = "abcdef123456"
currentUser = "chris.esbrandt.bsr"
endDate = 120512

Set fileSys = CreateObject("Scripting.fileSystemObject")
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2:Win32_Process")
batchStoreToPath = "\\" & strComputer & "\c$\Users\" & currentUser
objWMIService.Create batchStoreToPath & "\export.bat", Null, Null, intProcessID
oldStoreToPath = "\\" & strComputer & "\c$\Users\old." & currentUser & "." & endDate
Set oldStorePath = fileSys.GetFolder(oldStoreToPath)
objWMIService.Create oldStorePath & "\rename.bat", Null, Null, intProcessID

Basically, I defined the variable for generate the paths and removed everything I didn't need (including the creation of the files, just left them on the machine). Same exact issue. It will probably be easier to work with the smaller code. ;)
 
I've changed the WMI queries for better perforamance reasons. If you know what you are looking for and you don't need to retrieve multiple properties from a query then you can get the query itself to reflect that... I've also added a few Exit For's because those loops only really loop through once. Aside from that, because VBS is lame, the for loops are obviously required for OS compatibility pre-dating Vista. Anything past that we'd be able to simplify it with ItemIndex().

Have you tested this on both Windows 7 and XP? It doesn't work on either?

For a useless review though, this part:
Code:
If Month(Date) < 10 Then
	dateMonth = "0" & Month(Date)
Else
	dateMonth = Month(Date)
End If

If Day(Date) < 10 Then
	dateDay = "0" & Day(Date)
Else
	dateDay = Day(Date)
End If

Could be turned into a function to return the output value for you, then you wouldn't need to separate full-on if statements here. A single function would take care of both of these.

And instead of all these calls to the Chr() function to use a double quote, in VB.net as well as VBS, you can double up on double quotes to 'print' a single double quote in a string.

Example:
Code:
MsgBox("See that ""Apple"" is wrapped in double quotes.")

And i'm not sure about these WriteLine() methods, but I believe you don't have to have the "" to specify a nothingness string in there for it to write it's newlinefeed character based on it's interpretation to the output buffer...

Code:
exportBatch.WriteLine("")

Code:
exportBatch.WriteLine()

Haven't tested.

Result (slight modifications):
Code:
Dim dateMonth, dateDay, dateYear, endDate
Dim storeToPath, batchStoreToPath, oldStoreToPath
Dim verifyUserInfo
Dim fileSys, batchStorePath, exportBatch
Dim objWMIService, intProcessID
Dim objFSO
Dim oldStorePath, renameBatch

' Here is where we interrogate the Operating System
' On Error Resume Next

' Get the computer name (ie . = localhost)
strComputer = InputBox("Computer Name or IP Address: ")

' Remote connection path
Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

' This is where WMI interrogates the operating system
Set colItems = objWMI.ExecQuery("Select Version from Win32_OperatingSystem",,48)

' Here we filter Version from the dozens of properties
For Each objItem in colItems
	VerBig = Left(objItem.Version,3)
	Exit For
Next

' Spot VerBig variable in previous section
' Note the output variable is called OSystem

' This is where WMI interrogates the computer system
Set colItems2 = objWMI.ExecQuery("Select UserName from Win32_ComputerSystem",,48)
Set WSHShell = CreateObject("WScript.Shell")

Select Case VerBig
	Case "6.1"
		OSystem = "7"
		domain = WshShell.ExpandEnvironmentStrings("%userdomain%")
		currentUser = WshShell.ExpandEnvironmentStrings("%username%")
	Case "5.1"
		OSystem = "XP"
		' Here we filter UserName from the dozens of properties
		For Each objItem2 in colItems2
			arrName = Split(objItem2.UserName, "\")
			domain = arrName(0)
			currentUser = arrName(1)
			Exit For
		Next
	Case Else OSystem = "Invalid OS"
End Select

Sub pullSID
	' This is where WMI interrogates the user account
	Set objAccount = GetObject("winmgmts:root/cimv2:Win32_UserAccount.Domain='" & domain & "'" & ",Name='" & currentUser & "'")
	SID = objAccount.SID
End Sub

Call pullSID

If Month(Date) < 10 Then
	dateMonth = "0" & Month(Date)
Else
	dateMonth = Month(Date)
End If

If Day(Date) < 10 Then
	dateDay = "0" & Day(Date)
Else
	dateDay = Day(Date)
End If

dateYear = Right(Year(Date), 2)
endDate = dateMonth & dateDay & dateYear 
Wscript.Echo endDate

Sub setStoragePaths
	' Spot VerBig variable in previous section
	' Note the output variable is called storeToPath
	Select Case VerBig
		Case "6.1"
			storeToPath = "C:\Users\" & currentUser
			Select Case strComputer
				Case ".", "localhost", "127.0.0.1"
					batchStoreToPath = storeToPath
					oldStoreToPath = "C:\Users\old." & currentUser & "." & endDate
				Case Else
					batchStoreToPath = "\\" & strComputer & "\c$\Users\" & currentUser
					oldStoreToPath = "\\" & strComputer & "\c$\Users\old." & currentUser & "." & endDate
			End Select
		Case "5.1"
			storeToPath = "D:\Documents and Settings\" & currentUser
			Select Case strComputer
				Case ".", "localhost", "127.0.0.1"
					batchStoreToPath = storeToPath
					oldStoreToPath = "D:\Documents and Settings\old." & currentUser & "." & endDate
				Case Else
					batchStoreToPath = "\\" & strComputer & "\d$\Documents and Settings\" & currentUser
					oldStoreToPath = "\\" & strComputer & "\d$\Documents and Settings\old." & currentUser & "." & endDate
		 End Select
	End Select
End Sub

Call setStoragePaths

verifyUserInfo = Msgbox("Is the below user information correct?" & vbCr & vbCr _
	& "Version No: " & VerBig & vbCr _
	& "OS System: " & OSystem & vbCr _
	& "User: " & domain & "\" & currentUser & vbCr _
	& "User SID: " & SID, vbYesNo)

If verifyUserInfo = vbYes Then
	Msgbox "You answered yes."
Else
	domain = InputBox("User's Domain: ")
	currentUser = InputBox("Username: ")
	Call pullSID
	Call setStoragePaths
	Wscript.Echo "Version No: " & VerBig & vbCr _
		& "OS System: " & OSystem & vbCr _
		& "User: " & domain & "\" & currentUser & vbCr _
		& "User SID: " & SID
End If

' This is the creation of a batch file on the remote machine to export the registry entries for network mappings
Set fileSys = CreateObject("Scripting.fileSystemObject")
Set batchStorePath = fileSys.GetFolder(batchStoreToPath)
Set exportBatch = batchStorePath.CreateTextFile("export.bat", True)
exportBatch.WriteLine("@ECHO OFF && SETLOCAL EnableDelayedExpansion")
exportBatch.WriteLine("")
exportBatch.WriteLine("ECHO Exporting data for '" & currentUser & "' with SID '" & SID & "' to '" & storeToPath & "'...")
exportBatch.WriteLine("REG EXPORT HKU\" & SID & "\Network """ & storeToPath & "\1.reg""")
exportBatch.WriteLine("REG EXPORT HKU\" & SID & "\Printers """ & storeToPath & "\2.reg""")
exportBatch.WriteLine("REG EXPORT """ & "HKU\" & SID & "\Software\Microsoft\Windows NT\CurrentVersion\Devices""" & " """ & storeToPath & "\3.reg""")
exportBatch.WriteLine("REG EXPORT """ & "HKU\" & SID & "\Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts""" & " """ & storeToPath & "\4.reg""")
exportBatch.WriteLine("REG EXPORT """ & "HKU\" & SID & "\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles""" & " """ & storeToPath & "\5.reg""")
exportBatch.WriteLine("echo.>>""" & storeToPath & "\Network Mappings.reg""")
exportBatch.WriteLine("type """ & storeToPath & "\1.reg""" & " | find /v """ & "Windows Registry Editor Version 5.00""" & ">>""" & storeToPath & "\Network Mappings.reg""")
exportBatch.WriteLine("type """ & storeToPath & "\2.reg""" & " | find /v """ & "Windows Registry Editor Version 5.00""" & ">>""" & storeToPath & "\Network Mappings.reg""")
exportBatch.WriteLine("type """ & storeToPath & "\3.reg""" & " | find /v """ & "Windows Registry Editor Version 5.00""" & ">>""" & storeToPath & "\Network Mappings.reg""")
exportBatch.WriteLine("type """ & storeToPath & "\4.reg""" & " | find /v """ & "Windows Registry Editor Version 5.00""" & ">>""" & storeToPath & "\Network Mappings.reg""")
exportBatch.WriteLine("type """ & storeToPath & "\5.reg""" & " | find /v """ & "Windows Registry Editor Version 5.00""" & ">>""" & storeToPath & "\Network Mappings.reg""")
exportBatch.WriteLine("del """ & storeToPath & "\1.reg""")
exportBatch.WriteLine("del """ & storeToPath & "\2.reg""")
exportBatch.WriteLine("del """ & storeToPath & "\3.reg""")
exportBatch.WriteLine("del """ & storeToPath & "\4.reg""")
exportBatch.WriteLine("del """ & storeToPath & "\5.reg""")
exportBatch.WriteLine("")
exportBatch.WriteLine("PAUSE && GOTO :EOF")
exportBatch.Close

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2:Win32_Process")
objWMIService.Create batchStoreToPath & "\export.bat", Null, Null, intProcessID

Wscript.Echo "Instruct the customer to log out of them machine." & vbCr & vbCr _
& "Click 'OK' to rename the profile, and if Win7 to remove the SID from the registry and reboot."

Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFolder batchStoreToPath, oldStoreToPath

Select Case VerBig
	Case "6.1"
		Set oldStorePath = fileSys.GetFolder(oldStoreToPath)
		Set renameBatch = oldStorePath.CreateTextFile("rename.bat", True)
		renameBatch.WriteLine("reg delete """ & "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\" & SID & Chr(34) & " /f")
		renameBatch.WriteLine("shutdown -f -r -t 00")
		renameBatch.Close
		objWMIService.Create oldStorePath & "\rename.bat", Null, Null, intProcessID
End Select

WScript.Quit

Aside from all of that, I can't see what would be wrong here. I can't really test for you, but I would double check to make sure that the right output locations are being specified as well. Perhaps it is somehow being terminated before the Reg command gets a chance to finish? :S

And that would mean that it's not running under the system's shell as an independent process, but rather interpretted through wscript which when ended, ends as well the batch script processing. I know basic VBS, but otherwise I just assume similarities with VB/VB.net. And that's what gets me by. :)

Edit: I didn't see that you've already made a post, I had this thread open in a tab for a while, unrefreshed... Did you manage to get anything working? By dumbing down the script itself?
 
Alright, got one of the programmers here to take a look at it, can he can't see anything wrong either, but he did suggest an alternate method of pulling the user information that works with Win7 and XP without any issues. Just need some help if making the input value its own value.
Code:
GetUserInfo InputBox("Computer Name or IP Address: "), domain, currentUser, sSID
WScript.Echo "Computer: " & strComputer & vbCr & "User Domain: " & domain & vbCr & "Username: " & currentUser & vbCr & "SID: " & sSID
Function GetUserInfo(strComputer, ByRef domain, ByRef currentUser, ByRef strSID)
 Set ntaccount = GetObject("winmgmts://" & strComputer & "/root/cimv2:Win32_ComputerSystem.Name='" & strComputer & "'")
 strNTAccount = ntaccount.username
 sacct = Split(strNTAccount,"\")
 domain = sacct(0)
 currentUser = sacct(1)
 Set user = GetObject("winmgmts:root/cimv2:Win32_UserAccount.Domain='" & domain & "',Name='" & currentUser & "'")
 strSID = user.SID
End Function

I've killed it back to the basics for this. Once I get the variables to pull out the correct information (all are) I'll just replace the original code with this.

My issue is the I can't get the input to save as a variable too, and I don't know why I can't get it. I mean, the function is defining the input value as being the string, but it's not being able to be pulled back out of the function as the string. :S
 
That code is odd, and wrong unfortunately (not the whole thing lol, but the way that function works if you expect it to hold a return value upon being called)... There's no return value, it's not acting like a real function. In most other languages, this would be assigned via the return keyword, in VBS, you assign the name of the function to a value.

Example:
Code:
Function GetMyValue
	GetMyValue = "STRING"
End Function

MsgBox(GetMyValue)

It's acting more like a void, or a Sub, whatever you want to call it. I enjoy seeing your dedication towards getting this stuff to work though. Effort is never unrewarding, but sometimes in disguise if you don't think it is. :) Keep it up.
 
Last edited:
I'm not sure I understand how I would edit my new function to work with your set-up. It does what it needs to, in terms of it prompts for a machine name and then pulls the current users information from that machine and makes it available outside the function. My problem is that the machine name isn't carrying through as its own string. :S

VBS is just confusing me more and more, probably because I'm having to integrate PowerShell and Batch. ^^'
 
Where is this machine name in your code? What variable are you trying to make available outside of the function? The way you can do this is JUST like what it's doing with the rest of the variables, setting them from within the scope of the 'Function' but you're not utilizing your function here at all. What you could do is return the value of the machine name from the function.
 
Code:
GetUserInfo InputBox("Computer Name or IP Address: "), domain, currentUser, sSID
This line is what is calling the variables set-up in the function, it is also what is defining the computer name used by the function.

Code:
WScript.Echo "Computer: " & strComputer & vbCr & "User Domain: " & domain & vbCr & "Username: " & currentUser & vbCr & "SID: " & sSID
This line is what is providing the prompt that provides the user information pulled from the function.

Code:
Function GetUserInfo(strComputer, ByRef domain, ByRef currentUser, ByRef strSID)
This line, as I'm sure you can tell, is the start of the function and works in conjunction with the first line, defining the corresponding variable names from the function (strSID in the function becomes sSID outside the function). In this set-up, the "ByRef" makes the corresponding configuration possible. strComputer, which is missing the "ByRef", is showing that it is pulling it's value from the outside, not matching.

The rest of the code is pretty straight forward. Perhaps I'm trying to overthink the process, but I tried adding the machine name as a variable defined in the function, but I couldn't make it work outside the function. :(
 
It's because of something called scope. You may be passing it to the function, but there's no holder for it's value outside of the function, which is where it can't be referenced. Instead of passing InputBox to the function param, why not keep it OUT of the function call, and declare the InputBox return value as the strComputer, then pass that variable to the function after the fact?


Code:
GetUserInfo InputBox("Computer Name or IP Address: "), domain, currentUser, sSID

Code:
compStr = InputBox("Computer Name or IP Address: ")
GetUserInfo compStr, domain, currentUser, sSID
'compStr is available even after function call

Because InputBox is a function as well, but the difference between it and YOUR function, is that InputBox RETURNS a string value. So therefore, with the function call, it is assignable if you choose to get it's value from the return to the function call itself.
 
Alright, same code, slightly different issue.
Code:
strComputer = InputBox("Computer Name or IP Address: ")
GetUserInfo strComputer, domain, currentUser, sSID
WScript.Echo "Computer: " & strComputer & vbCr & "User Domain: " & domain & vbCr & "Username: " & currentUser & vbCr & "SID: " & sSID
Function GetUserInfo(strComputer, ByRef domain, ByRef currentUser, ByRef strSID)
 Set ntaccount = GetObject("winmgmts://" & strComputer & "/root/cimv2:Win32_ComputerSystem.Name='" & strComputer & "'")
 strNTAccount = ntaccount.username
 sacct = Split(strNTAccount,"\")
 domain = sacct(0)
 currentUser = sacct(1)
 Set user = GetObject("winmgmts:root/cimv2:Win32_UserAccount.Domain='" & domain & "',Name='" & currentUser & "'")
 strSID = user.SID
End Function
I can pull the information from a machine on the administrative domain, but none of our user domains. I get
Code:
Script: test4.vbs
Line:   5
Char:   2
Error:  The remote server machine does not exist or is unavailable: 'GetObject'
Code:   800A01CE
Source: Microsoft VBScript runtime error
I suspect it require impresonation, but I'm not familar with it, so I'm doing some reading up on it. Figured you might be able to tell me what's wrong sooner. ^^'
 
Haha, for users session impersonation, I know a bit about it. Although from my knowledge, it would require the use of some Windows API's, and I haven't gone too far into VBS to see what can be done on that front. I am thinking that Powershell or VBS may be able to do this though, but until I know for sure, I can't say. I'm not even quite sure of how you'd be able to pull that over the network if the user session was available?

If for a specific domain though, for a user session, this user was not currently logged on, and the session was non-existant at that time, then this may force the requirement for impersonation... But whether that would be an issue for you, under the circumstances you have, I'm not sure either.

Networking is not my expertise... But I wish I knew more about it sometimes. The networking portion of this, I have no doubt, that it is probably more in your favor than mine for that reason :thumbsup2:

When you received this error:
Error: The remote server machine does not exist or is unavailable: 'GetObject'

What state was the other machine in? Powered on and logged in? Or not logged in?
 
Well, what led me to think impersonation is because it works perfectly from XP->XP and XP->7, I get the error for 7->XP and 7->7, but only when going out of the local domain for the machine. All my machines are on the administrative domain, but it does me no good with the Win7 agents can't use it for the customers while the WinXP agents can. It's one of those all or nothing deals. I beginning to think it may be easier just to make two sets of code that run dependent upon the agent's OS. :S
 

Has Sysnative Forums helped you? Please consider donating to help us support the site!

Back
Top