Solved

Won't Exit Loop : VBScript

Posted on 2011-09-02
8
816 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
Synchronize a new Active Directory domain with an existing Office 365 tenant
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now