[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 642
  • Last Modified:

VBscript code to retrieve the systemdrive of a remote server

hello Experts,
I have already created a script that will copy a file from a specific directory on several servers on my network to another specific server. This is used for data collection.

my current issue is that we have inherited several servers that actually have the systemdrive as V:\ or Z:\ instead of the normal and customary C:\.
my original script only accounts for the standard C:\ which is just hardcoded into a string.
and runs against a text file list of servers.

now, here is my problem for which I need your help:
I need to modify this script to determine the %systemdrive% of the REMOTE server (which is determined by the serverlist.txt file), and use that to populate my variable to copy the file no matter if the systemdrive is C, V or whatever....
One other thing to note is that whatever method used to determine the systemdrive of the remote server, it has to work on NT4 (yea, yea, I know!! ) and higher.
I tried to use:  
strSysDrive = oShell.ExpandEnvironmentStrings("%WINDIR%")
however, that just returns the systemdrive of the local machine that the script is being ran from. I couldnt figure out how to pull the data from my serverlist in my loop.

' ========================================================
'	
'	Title:	Copy file from remote server				
'	Author:			Christopher
'	Originally created:		08/28/09
'	Original path:			
'	Description:	This script will copy a specified file to a specified
'			destination on remote servers that is retrieved from
'			a text file.
'
' ========================================================

'=============variables===================================
Const strLogfile = "c:\scripts\NetworkFilecopy.LOG"
Const ForReading = 1
Const OverwriteExisting = True
Const intForWriting = 2
Const Createfile = True

Dim objLogfile

Set fso = CreateObject("Scripting.FileSystemObject")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set oShell = CreateObject("WScript.Shell")
'textfile name and location that contains the servers to connect to
Set objFile = objFSO.OpenTextFile("C:\Scripts\Serverlist.txt")
Set objNetwork = CreateObject("wscript.network")
strDestinationFile = "D:\EMC_Grab_Storage\"

'=============body========================================
'opens logfile for writing
On Error Resume Next
Set objLogfile=fso.Opentextfile(strLogfile,intForWriting,Createfile)
objLogfile.writeline 
objLogfile.writeline  "---------------------------------------------------------------------"
objLogfile.writeline "Script started by " & objNetwork.username & " on " & Now
objLogfile.writeline  "---------------------------------------------------------------------"
objLogfile.writeline vbCrLf 

'parses text file of servers and copies the specified file to it
Do Until objFile.AtEndOfStream
    strComputer = objFile.ReadLine
           
    strSourceFile = "\\" & strComputer & "\C$\Windows\emcreports\collection\zip\*.zip"
    objFSO.CopyFile strSourceFile, strDestinationFile, OverwriteExisting
    
    'error handling
	 If err.number <> 0 Then
    	objLogfile.writeline "** Error connecting to server " & strComputer & "error # " & err.number & err.description
			err.clear			
		Else
			objLogfile.writeline "Successfully copied file from server " & strComputer
		End If
		
Loop

'finalize and close log file
objLogfile.writeline vbCrLf 
objLogfile.writeline "---------------------------------------------------------------------"
objLogfile.writeline "Script completed at " & Now
objLogfile.writeline "---------------------------------------------------------------------"
objLogfile.close

WScript.ECHO "Script complete. Output logged to " & strLogfile

Open in new window

0
charast
Asked:
charast
  • 2
1 Solution
 
Bill PrewCommented:
I didn't validate the rest of your code since you indicated that was working, but here I added in logic to find the system drive on the remote system.

' ========================================================
'	
'	Title:	Copy file from remote server				
'	Author:			Christopher
'	Originally created:		08/28/09
'	Original path:			
'	Description:	This script will copy a specified file to a specified
'			destination on remote servers that is retrieved from
'			a text file.
'
' ========================================================

'=============variables===================================
Const strLogfile = "c:\scripts\NetworkFilecopy.LOG"
Const ForReading = 1
Const OverwriteExisting = True
Const intForWriting = 2
Const Createfile = True

Dim objLogfile

Set fso = CreateObject("Scripting.FileSystemObject")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set oShell = CreateObject("WScript.Shell")
'textfile name and location that contains the servers to connect to
Set objFile = objFSO.OpenTextFile("C:\Scripts\Serverlist.txt")
Set objNetwork = CreateObject("wscript.network")
strDestinationFile = "D:\EMC_Grab_Storage\"

'=============body========================================
'opens logfile for writing
On Error Resume Next
Set objLogfile=fso.Opentextfile(strLogfile,intForWriting,Createfile)
objLogfile.writeline 
objLogfile.writeline  "---------------------------------------------------------------------"
objLogfile.writeline "Script started by " & objNetwork.username & " on " & Now
objLogfile.writeline  "---------------------------------------------------------------------"
objLogfile.writeline vbCrLf 

'parses text file of servers and copies the specified file to it
Do Until objFile.AtEndOfStream
    strComputer = objFile.ReadLine

    ' Get system drive or remote comnputer
    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colOSes = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
    For Each objOS in colOSes
        strSystemDrive = Left(objOS.SystemDrive, 1)
    Next
    Set colOSes = Nothing
    Set objWMIService = Nothing
           
    strSourceFile = "\\" & strComputer & "\" & strSystemDrive & "$\Windows\emcreports\collection\zip\*.zip"
    objFSO.CopyFile strSourceFile, strDestinationFile, OverwriteExisting
    
    'error handling
	 If err.number <> 0 Then
    	objLogfile.writeline "** Error connecting to server " & strComputer & "error # " & err.number & err.description
			err.clear			
		Else
			objLogfile.writeline "Successfully copied file from server " & strComputer
		End If
		
Loop

'finalize and close log file
objLogfile.writeline vbCrLf 
objLogfile.writeline "---------------------------------------------------------------------"
objLogfile.writeline "Script completed at " & Now
objLogfile.writeline "---------------------------------------------------------------------"
objLogfile.close

WScript.ECHO "Script complete. Output logged to " & strLogfile

Open in new window

~bp
0
 
charastAuthor Commented:
This worked very well thank you!
 although it will not work with my NT4 servers, I was able to work with this and instead of using
strSystemDrive = Left(objOS.SystemDrive, 1)
I used
strSystemDrive = Left(objOS.WindowsDirectory, 1)
which gives me the same result for this script and also allows me to use the whole string for one of my other scripts which needs the string for the windows directory as well.

Thank you again! I knew I could depend on someone here to help me out on this one!
0
 
Bill PrewCommented:
Great, glad that helped, and thanks for the feedback.

~bp
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now