Solved

Won't Exit Loop : VBScript

Posted on 2011-09-02
8
818 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

910 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

19 Experts available now in Live!

Get 1:1 Help Now