Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Batch Vbscript / Text Reading Writing help needed

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,2568
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Batch Vbscript / Text Reading Writing help needed
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Batch Vbscript / Text Reading Writing help needed - 4/4/2005 10:52:43 PM   
  disktone

 

Posts: 9
Score: 0
Joined: 4/4/2005
From:
Status: offline
Hi I need some help with the following, I am trying to setup a script to do some work on virtual servers on vmware gsx.

I need to get a list of servers, suspend them while i backup the host and start them up again after, however if the server was not running before backup i do not want to start it up after.

So far i have used a batch file to get the list of servers and the states,
these are in two separate text files, all details are on corresponding lines.
I would ideally like to do this whole script in vbscript but am a bit stuck, I now need to read the status from state.txt,
If status is not 'OFFÒ or 'SUSPENDEDÒ get the same line from servers.txt and write that to power.txt

Power.txt will then be used to suspend the servers during backup and then start the servers after backup

My batch file so far

REM this generates list of vmware machines into servers.txt
call c:\program files\vmware\vmware gsx server\vmware-cmd -l > c:\batch\servers.txt

REM go through server.txt line by line and then get status into state.txt adding each entry as a new line
for /f "delims=" %%1 in (C:\batch\test.txt) do "C:\program files\vmware\vmware gsx server\vmware-cmd" "%%1" getstate >> c:\batch\state.txt


SERVERS.TXT
D:\Virtual Machines\SHTWSMT01\winNetStandard.vmx
D:\Virtual Machines\SHTWSMT02\winNetStandard.vmx
D:\Virtual Machines\SHTWSMT04\win2000Serv.vmx
D:\Virtual Machines\SHTQLMT01\winNetStandard.vmx
D:\Virtual Machines\SHTPPMT07\winNetStandard.vmx
D:\Virtual Machines\SHTPPMT06\winNetStandard.vmx
D:\Virtual Machines\SHTEBLT01\redhat.vmx
D:\Virtual Machines\SHTPPLT01\redhat.vmx
D:\Virtual Machines\SHTRALT01\rhel2.vmx
D:\Virtual Machines\SHTPPMT01\winNetStandard.vmx

STATE.TXT
getstate() = on
getstate() = suspended
getstate() = on
getstate() = on
getstate() = on
getstate() = on
getstate() = on
getstate() = off
getstate() = suspended
getstate() = suspended

I found an example of split below and played about with this to return ON,

Dim s
Dim lst
s = " getstate() = on "
lst = Split(s) ' returns an array
MsgBox lst(2)

how do I read and process state.txt line by line, if the line is not 'OFFÒ or 'SUSPENDEDÒ write the same line from servers.txt to power.txt

Power.txt should then read (these are the only servers I need to suspend and power up)Lines 1,3,4,5,6,7

D:\Virtual Machines\SHTNIWSMT01\winNetStandard.vmx
D:\Virtual Machines\SHTNIWSMT04\win2000Serv.vmx
D:\Virtual Machines\SHTNSQLMT01\winNetStandard.vmx
D:\Virtual Machines\SHTNAPPMT07\winNetStandard.vmx
D:\Virtual Machines\SHTNAPPMT06\winNetStandard.vmx
D:\Virtual Machines\SHTNWEBLT01\redhat.vmx

The dos command to suspend would be

for /f "delims=" %%1 in (C:\batch\power.txt) do call "c:\program files\vmware\vmware gsx server\vmware-cmd" "%%1" suspend trysoft


My command to restart the servers in batch would be

for /f "delims=" %%1 in (C:\batch\power.txt) do call "c:\program files\vmware\vmware gsx server\vmware-cmd" "%%1" start trysoft

Clean up text file for next backup run
del power.txt
del servers.txt
del state.txt

Anyhelp with converting what i have done to vbscript would be greatly appreciated and any held reading and writing the text files would be greatly appreciated [B)]
 
 
Post #: 1
 
 Re: Batch Vbscript / Text Reading Writing help needed - 4/5/2005 12:30:41 AM   
  mbouchard


Posts: 1916
Score: 16
Joined: 5/15/2003
From: USA
Status: offline
You can try something along these lines. We may need to change the second WshShell.Run to WshShell.Exec, if the below code doesn't work, let me know and I will make the change.


      

(in reply to disktone)
 
 
Post #: 2
 
 Re: Batch Vbscript / Text Reading Writing help needed - 4/5/2005 1:42:31 AM   
  disktone

 

Posts: 9
Score: 0
Joined: 4/4/2005
From:
Status: offline
Mike, Many thanks that was quick, i will have a look when i have some down time and get back to you

Many Thanks

Nigel

(in reply to disktone)
 
 
Post #: 3
 
 Re: Batch Vbscript / Text Reading Writing help needed - 4/5/2005 3:45:29 AM   
  disktone

 

Posts: 9
Score: 0
Joined: 4/4/2005
From:
Status: offline
Mike, the script doesn't seem to like the spaces in the path, so i have added the path to the server path.
I remove cmd /c, as it cause the servers.txt to be 0 bytes,

so the script now looks like this

Const ForAppending = 8
Const ForWriting = 2
Const ForReading = 1

Dim Fso : Set Fso = CreateObject("Scripting.FileSystemObject")
Dim WshShell : Set WshShell = CreateObject("WScript.Shell")

WshShell.Run "vmware-cmd -l >c:\batch\server.txt",1,true

Set f = fso.OpenTextFile("c:\batch\server.txt", ForReading)
TheArray = Split(f.ReadAll,vbCrLf)
f.Close

Set f = fso.OpenTextFile("c:\batch\server.txt", ForWriting,TRUE)

for each arrdatas in TheArray
'You may need to work on the quotes
TheState = WshShell.Run("cmd /C vmware-cmd " & """" strServer & """" & " getstate",1,TRUE)
f.WriteLine(arrdatas & "," & TheState)
next
f.Close

The second Wshell complains that it needs ) line 29 char 38, the start of strServer, have tried tweaking with no success

In dos the command would be

Vmware-cmd ÓD:\Virtual Machines\SHTNIWSMT01\winNetStandard.vmxÔ getstate

Many Thanks in Advance Nige

(in reply to disktone)
 
 
Post #: 4
 
 Re: Batch Vbscript / Text Reading Writing help needed - 4/5/2005 4:49:21 AM   
  mbouchard


Posts: 1916
Score: 16
Joined: 5/15/2003
From: USA
Status: offline
Try this,

WshShell.Run "cmd /c Vmware-cmd ""D:\Virtual Machines\SHTNIWSMT01\winNetStandard.vmx"" getstate"

Also, to see if the command is running correctly, change /c /k, this will leave the cmd window open until you close it.

(in reply to disktone)
 
 
Post #: 5
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Batch Vbscript / Text Reading Writing help needed Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts