Link to home
Start Free TrialLog in
Avatar of coolsport00
coolsport00Flag for United States of America

asked on

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
Avatar of cfEngineers
cfEngineers

strFilePath = "C:\FileName.extension"

change to

strFilePath = "\\yourservernameOrIP\C$\ChangeThisToYourFileNameWithExtension"
Avatar of coolsport00

ASKER

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
Avatar of RobSampson
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

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
Sure, no problem.

Rob.
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
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

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

ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oh, ok...thanks; will give it a go and let you know (umm...didn't mean for that to rhyme) :P
LOL! You're a poet and you didn't know it!
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...
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.
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
Great work and continual follow-up. A "true expert"! :)
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.
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!
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.