Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

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

VBscripting assistance

I need to delete a file on the root of C: on my servers. Simple enough. Problem is, my servers are a mixture of WinXP (small app servers) as well as W2K3 Server. Any idea how I can delete the file from my servers? Can I reference all computer accounts in my Servers OU in AD? Can I create a txt file of all the server names/computer accounts and reference that? Though I can fenagle my way through a script to kinda see what it's doing, I am by no means a scripter at all. I have code that I've...(cough) borrowed to use. :) The below is what I have:

Dim objFileSys, objDelFile, strFilePath

  strFilePath = "C:\FileName.extension"

  Set objFileSys = CreateObject ("Scripting.FileSystemObject")

  If objFileSys.FileExists(strFilePath) Then
      objFileSys.DeleteFile strFilePath, True
  End If

  Set objFileSys = Nothing
  Set objDelFile = Nothing
  Set strFilePath = Nothing

My problem is the beginning part of the script. I don't know how to call/reference my servers. Thanks in advance for your assistance!

Regards,
~coolsport00
0
coolsport00
Asked:
coolsport00
  • 10
  • 9
2 Solutions
 
cfEngineersCommented:
strFilePath = "C:\FileName.extension"

change to

strFilePath = "\\yourservernameOrIP\C$\ChangeThisToYourFileNameWithExtension"
0
 
coolsport00Author Commented:
Well, I have about 50 servers, so an explicit filepath does me no good, or I could use the initial variable/string. I need to know how to call *ALL* my servers (as I stated in my orig post) so they can all do this file deletion task.

~coolsport00
0
 
RobSampsonCommented:
Hi there,

This will read the computers from an OU that you specify, and delete the file if it exists.

Regards,

Rob.
strFilePath = "C:\FileName.extension"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objRootDSE = GetObject("LDAP://RootDSE")
Set colItems = GetObject("LDAP://ou=My Servers," & objRootDSE.Get("defaultNamingContext"))
colItems.Filter = Array("Computer")
For Each objItem In colItems
	strRemoteFile "\\" & objItem.CN & "\" & Replace(strFilePath, ":", "$")
	If objFSO.FileExists(strRemoteFile) = True Then
		objFSO.DeleteFile strRemoteFile, True
	End If
Next

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
coolsport00Author Commented:
Thanks Rob; I don't have VPN connectivity 2nite, so I'll have to test this out tomorrow. Thank you for the response.

Regards,
~coolsport00
0
 
RobSampsonCommented:
Sure, no problem.

Rob.
0
 
coolsport00Author Commented:
Ok Rob. I was able to run this tonite (along with the "kazillion" other things I had to do) :)  I had to slightly modify the script as follows (the full blown script):

Option Explicit

Dim objFSO, objRootDSE, ColItems
Dim objItem, strFilePath, strRemoteFile

 strFilePath = "C:\FileName.extension"

 Set objFSO = CreateObject("Scripting.FileSystemObject")
 Set objRootDSE = GetObject("LDAP://RootDSE")
 Set colItems = GetObject("LDAP://ou=Servers," & objRootDSE.Get("defaultNamingContext"))
 colItems.Filter = Array("Computer")

 For Each objItem In colItems
      
      strRemoteFile = "\\" & objItem.CN & "\" & Replace(strFilePath, ":", "$")
      
      If objFSO.FileExists(strRemoteFile) = True Then
            objFSO.DeleteFile strRemoteFile, True
      End If
      
 Next

 Set objFSO = Nothing
 Set objRootDSE = Nothing
 Set ColItems = Nothing
 Set objItem = Nothing
 Set strFilePath = Nothing
 Set strRemoteFile = Nothing

What I had to 'add' was...well, obviously 'calling' the variables but the "=" after strRemoteFile. I got an error without that there. So, this worked for my 'high-level' OU 'Servers'. I have some nested server OUs under the 'Servers' OU. How do I use this script for those OUs. I tried changing the OU name with the name of the nested OUs, but that didn't work. I assume it's because of the 'RootDSE' that was made?..not sure. Can you assist a bit further?

Thanks.

~coolsport00
0
 
RobSampsonCommented:
OK, sure, well to do that, we need to omit the filter by Computer objects, and test for the type during the For Each loop.

I haven't tested it, but give this a shot.  If you want to, add some output so you know whether it's getting all the machines or not.

Regards,

Rob.
Option Explicit

Dim objFSO, objRootDSE, ColItems
Dim objItem, strFilePath, strRemoteFile

strFilePath = "C:\FileName.extension"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objRootDSE = GetObject("LDAP://RootDSE")

EnumerateOU "LDAP://ou=Servers," & objRootDSE.Get("defaultNamingContext")

Set objFSO = Nothing
Set objRootDSE = Nothing
Set ColItems = Nothing
Set objItem = Nothing
Set strFilePath = Nothing
Set strRemoteFile = Nothing

Sub EnumerateOU(strOUPath)
	Set colItems = GetObject(strOUPath)
	
	For Each objItem In colItems
		If LCase(objItem.Class) = "computer" Then
			strRemoteFile = "\\" & objItem.CN & "\" & Replace(strFilePath, ":", "$")
			If objFSO.FileExists(strRemoteFile) = True Then
				objFSO.DeleteFile strRemoteFile, True
			End If
		ElseIf LCase(objItem.Class) = "organizationalunit" Then
			EnumerateOU objItem.adsPath
		End If
	Next
End Sub

Open in new window

0
 
coolsport00Author Commented:
Ok...well, it's erroring out on me. I have tried making changes & keep getting a "Name Redefined" error, which I believe has something to do with either variable naming or assigning? I modified the above code because..well..it's a bit 'off' :) Below is what I have...

What I added was "Set" in front of your 'EnumerateOU' variable, as well as the "=" sign. (not sure if those were needed?) Not sure if "organizationalunit" should be two separate words in the quotes or not?

This is the error I get:

Script:   C:\LocationOfScript
Line:     16
Char:    6
Error:    Name redefined
Code:   800A0411
Source: Microsoft VBScript compilation error

This is my AD OU structure just as an FYI:

ADU&Cs
-> MyDomain
    .
    .
    .
 -> Servers OU
  -> App Servers OU
  -> DB Servers OU
  -> File Servers OU
etc...

So you can see it's just 2 levels down. I have my root "Domain", then Servers under the 'root', then sub server OUs after 'Servers'.

Thanks for all your continued assistance Rob.

~coolsport00
Option Explicit

Dim objFSO, objRootDSE, ColItems
Dim objItem, strFilePath, strRemoteFile
Dim EnumerateOU, strOUPath

 strFilePath = "C:\Filename.Ext"

 Set objFSO = CreateObject("Scripting.FileSystemObject")
 Set objRootDSE = GetObject("LDAP://RootDSE")

 Set EnumerateOU = "LDAP://ou=Servers," & objRootDSE.Get("defaultNamingContext")

 Sub EnumerateOU(strOUPath)
	Set colItems = GetObject(strOUPath)
	
	For Each objItem In colItems
	  If LCase(objItem.Class) = "computer" Then
		strRemoteFile = "\\" & objItem.CN & "\" & Replace(strFilePath, ":", "$")
			If objFSO.FileExists(strRemoteFile) = True Then
				objFSO.DeleteFile strRemoteFile, True
			End If
	  ElseIf LCase(objItem.Class) = "organizationalunit" Then
			EnumerateOU objItem.adsPath
	  End If
	Next 

 Set objFSO = Nothing
 Set objRootDSE = Nothing
 Set ColItems = Nothing
 Set objItem = Nothing
 Set strFilePath = Nothing
 Set strRemoteFile = Nothing
 Set EnumerateOU = Nothing
 Set strOUPath = Nothing

Open in new window

0
 
RobSampsonCommented:
I'm not sure what the original error with the code I posted was, but I've fixed it up so it should work for you as is.

EnumerateOU is a Sub Procedure, so it does need declaring, just "calling", hence this line:
EnumerateOU "LDAP://OU=Computers,OU=MainSite," & objRootDSE.Get("defaultNamingContext")

That calls the procedure, passing the OU string to it to enumerate the objects.

The LDAP string above must be defined by OU in reverse order, so the above actually refers to
mydomain.com\MainSite\Computers

If you have just
mydomain.com\Servers OU

then use
EnumerateOU "LDAP://OU=Servers OU," & objRootDSE.Get("defaultNamingContext")

Regards,

Rob.
Option Explicit

Dim objFSO, objRootDSE, colItems
Dim objItem, strFilePath, strRemoteFile

strFilePath = "C:\Filename.Ext"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objRootDSE = GetObject("LDAP://RootDSE")

EnumerateOU "LDAP://OU=Computers,OU=MainSite," & objRootDSE.Get("defaultNamingContext")

Set objFSO = Nothing
Set objRootDSE = Nothing
Set ColItems = Nothing
Set objItem = Nothing
Set strFilePath = Nothing
Set strRemoteFile = Nothing

MsgBox "Script complete."

Sub EnumerateOU(strOUPath)
	Set colItems = GetObject(strOUPath)
	
	For Each objItem In colItems
	  If LCase(objItem.Class) = "computer" Then
		strRemoteFile = "\\" & objItem.CN & "\" & Replace(strFilePath, ":", "$")
		If objFSO.FileExists(strRemoteFile) = True Then
			objFSO.DeleteFile strRemoteFile, True
		End If
	  ElseIf LCase(objItem.Class) = "organizationalunit" Then
		EnumerateOU objItem.adsPath
	  End If
	Next 
 End Sub

Open in new window

0
 
coolsport00Author Commented:
Ok...so I'll just copy the exact code you have and test it out, modifying it slightly for my OU structure. So, since I have a Servers OU and "sub-Server OUs", I would put EnumerateOU like this?

EnumerateOU "LDAP://OU=App Servers, OU=Servers,OU=MyDomain," & objRootDSE.Get("defaultNamingContext")

I didn't try your code as you had it above because...I thought it was backwards a bit (told you I'm not a scripter :)  ).

Thanks with your patience with me :)

~coolsport00
0
 
RobSampsonCommented:
Not quite.  All you need to do is specify your "top most" OU, and it will iterate through each of the Sub OUs from there.

So, if you have
domain.com
   --- Servers
      --- App Servers
      --- Prod Servers

Then just specify "LDAP://OU=Servers," & objRootDSE.Get("defaultNamingContext")

Then it would start at Servers, and go through App Servers and Prod Servers automatically as well.  The defaultnamingcontext takes care of the domain.com part for you, so you don't need to enter it.

Regards,

Rob.
0
 
coolsport00Author Commented:
Oh, ok...thanks; will give it a go and let you know (umm...didn't mean for that to rhyme) :P
0
 
RobSampsonCommented:
LOL! You're a poet and you didn't know it!
0
 
coolsport00Author Commented:
Ha...now that's *real* bad! :P

Getting ready to test this out; you know 1 thing I forgot to add?...the 'End Sub' parameter. That may be why it errored (or 1 of many)... :)

I'll let ya know...
0
 
RobSampsonCommented:
It more likely errored out because you treated the Sub declaration incorrectly.  Since the procedure isn't returning an object, the Set keyword was incorrect.

Rob.
0
 
coolsport00Author Commented:
I'm at a loss...really. Like I said...I can 'kinda' figure it out, but when it comes to knowing where to begin as far as calling this or that, I'm clueless. Being a Sys Admin, I know I should know more, but alas...I do not. It is on my never-ending list of things to work on (powershell, powerCLI, etc., etc., etc.) :)

So, the script ran and it seems to have worked. I admin-shared to several servers and no longer see the file. Thanks for your due diligence and patience Rob. Have a good nite...

~coolsport00
0
 
coolsport00Author Commented:
Great work and continual follow-up. A "true expert"! :)
0
 
RobSampsonCommented:
No problem. Thanks for the grade.

It certainly is a great custom tool to have in a sys admin's arsenal, but it takes time to learn.  I learnt by looking at other examples of what I wanted to do, and having a couple of scripting books handy.

Unfortunately at the moment I don't know much Powershell, and VBScript is becoming outdated, although still widely supported, but I'll have to move on soon....

Regards,

Rob.
0
 
coolsport00Author Commented:
I agree. It may be 'outdated' but you're right, it's still widely used. We use it for our login scripting. Thankfully my operations (IS) side has a good 'coder' and assists me quite often. I try on my own first though, mostly by searching. Getting a book is something I certainly need to do, although maybe if I can get Powershell/PowerCLI (for my vSphere environment) down, I won't need it?...I don't know...one of those things that are just plain good to have.

Thanks again!
0
 
RobSampsonCommented:
vSphere certainly supports PowerCLI much more readily than VBScript....so if you want to use it for the VMWare stuff, definately go with Powershell.  But if you're still keen on doing things for your file systems and workstations, then VBScript can still get the job done well.

Rob.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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