Won't Exit Loop : VBScript

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

mmitchell57Asked:
Who is Participating?
 
jerrypdConnect With a Mentor Commented:
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
 
jerrypdCommented:
looked real quick... but what if you change it to
do while not endofstream?
0
 
mmitchell57Author Commented:
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
Worried about phishing attacks?

90% of attacks start with a phish. It’s critical that IT admins and MSSPs have the right security in place to protect their end users from these phishing attacks. Check out our latest feature brief for tips and tricks to keep your employees off a hackers line!

 
mmitchell57Author Commented:
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
 
jerrypdCommented:
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
 
mmitchell57Author Commented:
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
 
Dale HarrisProfessional Services EngineerCommented:
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
 
mmitchell57Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.