Solved

Won't Exit Loop : VBScript

Posted on 2011-09-02
8
820 Views
Last Modified: 2012-05-12
I'm having a serious brain fart. I can't figure out why, in the code below, that when it loops to the last line of the data it won't leave the loop and stop the script. I'm assuming I'm missing something relatively small and overthinking it. Everything else works great.

' This script is designed to query the domain
' see what computer accounts have not checked
' in for more then 90 days and deletes them.
' Created By: mmitchell57
' Date: today
'<!---- Variable Start ----!>
Dim objDSQuery1, strDSQuery1
Dim strLimit
Dim strLen 
Dim strDSRM
'<!---- Variable Start ----!>

'<!---- Initialize start -----!>
Set WshShell = CreateObject("WScript.Shell") 
strLimit = 2

'<!----- Initialize end ----!>


'<!---- Body Start ----!>
WshShell.Exec("%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -Command DSQuery Computer 'OU=Agencies,OU=FORSCOM,OU=Installations,DC=nase,DC=ds,DC=army,DC=mil' -stalepwd 90 -limit " & strLimit & " >> '\\Server\Share\Folder\Folder\File.txt'") 
Set objDSQuery1 = WshShell.Exec("%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -Command DSQuery Computer 'OU=Grouops,OU=Someplace,OU=,DC=domain,DC=controller,DC=com' -stalepwd 90 -limit " & strLimit) 


Do Until objDSQuery1.StdOut.AtEndOfStream 
    strDSQuery1 = objDSQuery1.StdOut.ReadLine()
    strLen = Len(strDSQuery1)
    'wscript.echo strlen
    strLen = strLen - 2
    strDSQuery1 = mid(strDSQuery1, 2, strLen)
    strDSRM = "dsrm '" & strDSQuery1 & "' -noprompt -c -q"
    wscript.echo strDSRM
    'WshShell.Exec("%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -Command " & strDSRM) 
Loop

'<!---- Body End ----!>

Open in new window

0
Comment
Question by:mmitchell57
  • 4
  • 3
8 Comments
 
LVL 9

Expert Comment

by:jerrypd
ID: 36474190
looked real quick... but what if you change it to
do while not endofstream?
0
 

Author Comment

by:mmitchell57
ID: 36474221
JerryPD, based on your response I gave it a try. It didn't seem to help.  Also, i cleaned up the code a bit so it is easier to read.

' This script is designed to query the domain
' see what computer accounts have not checked
' in for more then 90 days and deletes them.

'<!---- Variable Start ----!>
Dim objDSQuery1, strDSQuery1
Dim strLimit
Dim strLen 
Dim strDSRM
Dim strPS
'<!---- Variable Start ----!>

'<!---- Initialize start -----!>
Set WshShell = CreateObject("WScript.Shell") 
strLimit = 2 
strFileLoc = "'deleted_comps.txt'"
strPS = "%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -Command "
strCMD = "DSQuery Computer 'OU=y,DC=Domain,DC=name,DC=coml' -stalepwd 90 -limit " & strLimit

'<!----- Initialize end ----!>


'<!---- Body Start ----!>
WshShell.Exec(strPS & strCMD  & " >> " & strFileLoc) 
Set objDSQuery1 = WshShell.Exec(strPS & strCMD) 

Do While Not objDSQuery1.StdOut.AtEndOfStream 
    strDSQuery1 = objDSQuery1.StdOut.ReadLine()
    strLen = Len(strDSQuery1)
    strLen = strLen - 2
    strDSQuery1 = mid(strDSQuery1, 2, strLen)
    strDSRM = "dsrm '" & strDSQuery1 & "' -noprompt -c -q"
    wscript.echo strDSRM
    'WshShell.Exec(strPS & strDSRM) 
Loop

'<!---- Body Start ----!>

Open in new window

0
 

Author Comment

by:mmitchell57
ID: 36474242
Also, i've run the script under a couple accounts with the same problem. It doesn't appear to be related to the need of elevated rights on the domain.
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 9

Expert Comment

by:jerrypd
ID: 36474271
are you actually seeing a null line or a bunch of null lines come thru from the wscript.echo?
if so i would take out the echo line, and just create a counter to see how many there actually are in the file?
Could it be that there is a bunch of null entries that you are not aware of and the code is actually not reaching EOF??
0
 

Author Comment

by:mmitchell57
ID: 36474348
I write the results of the original query out to a file to help with troubleshooting. There's one blank line after the final result of the query in the text file.

In the echo screen at the command prompt I see each line appear as suspected. Presently my query only returns 2 items for testing purposes. I see both of the items written to the screen as expected, then the cursor carriage returns and sits like it's waiting for something. I've waited for about 10 minutes max for it. I then have to do a ctrl+c to kill it.  I've changes the results count on the query and it doesn't change the issue.

Also, copied out the script into a new file. It did the same thing. I have a feeling there's something I'm really missing. Do you think I should write the results of the second query to a document via the write lines and so forth?
0
 
LVL 16

Expert Comment

by:Dale Harris
ID: 36474427
I know you said you wanted to fix your script, but have you tried writing this in Powershell?  I think it would be MUCH easier.  You also would benefit installing Quest Active Directory cmdlets.
http://www.quest.com/powershell/activeroles-server.aspx

#Start script
#Variables
$Days = 90

#Command to get all computers in AD that haven't been logged into the last ($days value) days
$Computers = get-qadcomputer -IncludedProperties LastLogonTimeStamp | where {((get-date)-$_.LastLogonTimeStamp).Days -gt $days }

#Computers now is a variable holding all of your computers that need to be removed
#Not recommended to actually delete them now.  Just disable them and see if anyone squawks.

$Computers | Disable-QADComputer

Write-Host "Script complete"

Two lines of code to do all the heavy lifting for you.  I love Powershell!


If you want a more robust script with a lot more functionality, you can also check out this:
http://trevorsullivan.net/2010/08/17/powershell-ad-sccm-workstation-cleanup-script-version-3-0/

HTH,

Dale Harris
0
 
LVL 9

Accepted Solution

by:
jerrypd earned 125 total points
ID: 36475161
just thought of something here.
Could it be that the exec command is not terminating cleanly?
what if you just process the file you created in line 24?
I bet thats the problem!
0
 

Author Comment

by:mmitchell57
ID: 36475325
Jerry, that's a good idea.  I may give that a run to see how it works. I do find it weird that the first command to make the file work flawlessly, but the next command that is similar gives me issues.
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
A brief introduction to what I consider to be the best editor for PowerShell.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question