Solved

Won't Exit Loop : VBScript

Posted on 2011-09-02
8
828 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

691 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