Solved

Remove select data from log file generated by batch script

Posted on 2013-06-11
10
738 Views
Last Modified: 2013-06-11
Hey Experts!  I have a batch script that dumps out the names of the stale computer accounts into a log file.  Here is the code:    

@echo off
setlocal enabledelayedexpansion
set LogFile=C:\Tools\staleAdPc.log
if not exist "%LogFile%" goto SkipRename
for %%f in ("%LogFile%") do (
	for /f "tokens=1-3 delims=/ " %%a in ("%%~tf") do (set TimeStamp=%%c%%a%%b)
	set NewName=%%~nf.old.!TimeStamp!%%~xf
	echo Renaming "%LogFile%" to "!NewName!" ...
	ren "%LogFile%" "!NewName!"
	ECHO File date: %%~tf
)
:SkipRename

dsquery computer OU=Depts,OU=x,DC=x,DC=x,DC=x,DC=x -inactive 8 -limit 300 > c:\tools\staleADpc.log

Open in new window

This outputs something like this:
"CN=CSV-SHARED12,OU=Computers,ou=x,OU=x,OU=x,DC=x,DC=x,DC=x,DC=x"
"CN=CSV-SHARED1,OU=Computers,OU=x,OU=x,OU=x,DC=x,DC=x,DC=x,DC=x"

I need the output to be clear of everything except the computer name (ie CSV-SHARED12).  The log file is used by a powershell script and I need only the computer name listed.  What is the best way to go about clearing the log file out of the extra data?

Thanks!
0
Comment
Question by:samiam41
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 4

Expert Comment

by:TalShyar
Comment Utility
Add this line to the  end of your batch file:

for /f "tokens=2,3* delims=,=" %%i in (c:\tools\staleADpc.log) do @echo %%i >> c:\tools\pc-names.log

Now use the pc-names.log as input for your powershell script.
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
You could also do in one go without appending to the file each time using ( ) .... this is TalShyar's code ...

(for /f "tokens=2,3* delims=,=" %%i in (c:\tools\staleADpc.log) do @echo %%~i)> c:\tools\pc-names.log

I'm surprised if you can't do it all in Powershell if you are using that anyway.  You could also just add

-o rdn to the dsquery line and it will report just the computer name, i.e.

dsquery computer OU=Depts,OU=x,DC=x,DC=x,DC=x,DC=x -o rdn  -inactive 8 -limit 300 > c:\tools\staleADpc.log

Steve
0
 
LVL 11

Assisted Solution

by:netballi
netballi earned 50 total points
Comment Utility
You can use the following power shell command change the days from 90 to what ever you need it to be.

Import-Module ActiveDirectory
$date = [DateTime]::Today.AddDays(-90)
Get-ADComputer -Filter  ‘PasswordLastSet -le $date’ -SearchBase “OU=Depts,OU=x,DC=x,DC=x,DC=x,DC=x” -properties PasswordLastSet | select Name
0
 
LVL 9

Author Comment

by:samiam41
Comment Utility
Thanks everyone for your help and suggestions.

Here is the process I am running now.  The first thing I do is run the script posted in the OP which yields the StaleADPc.log.  The powershell script I run next moves the accounts listed in StaleADPc.log and outputs which computer accounts were successfully moved.  

Now, if there is a way to make this all done in one script, I'm all gamed for it but need you to provide the script as I'm still cutting my teeth on this.

With that said, @TalShyar and @dragon-it (nice to see you again!), what would the script I posted in the OP look like with the portion of code you suggested?

@netballi, here is the powershell script I run on the StaleADPc.log (so that you can plug in the code you suggested) as I'm not sure where I would put that.

Import-Module ActiveDirectory
Foreach ($User in GC C:\tools\staleADPC1.log) {
Try 
 {
  Get-ADUser $User | Move-ADObject -TargetPath 'OU=Inactive,OU=x,DC=x,DC=x,DC=x,DC=x' -ErrorAction Stop
  Write-Host "Moved user $User"
 }
 Catch
 {
  Write-Host "Moved user $User - Error - $($_.Exception.Message)"
 }
}

Open in new window

0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 250 total points
Comment Utility
For the batch way just replace your last line with:

dsquery computer OU=Depts,OU=x,DC=x,DC=x,DC=x,DC=x -o rdn  -inactive 8 -limit 300 > c:\tools\staleADpc.log

or add either of the for lines shown as the next line after your existing dsquery line.


the -o rdn just returns the computer name only into your log file.

But if you netballi can give you all in one ps will leave you with him.

Steve
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 9

Author Comment

by:samiam41
Comment Utility
@dragon-it, the output is "computername".  How can I get the " " out of the log file?  The powerscript isn't running with the format "computername".
0
 
LVL 4

Assisted Solution

by:TalShyar
TalShyar earned 200 total points
Comment Utility
I don't think there is a way to get rid of the double qoutes when using dsquery to pipe the output out without doing some form of clean up yourself

You can use my solution:
for /f "tokens=2,3* delims=,=" %%i in (c:\tools\staleADpc.log) do @echo %%i >> c:\tools\pc-names.log

or dragon-it's:
(for /f "tokens=2,3* delims=,=" %%i in (c:\tools\staleADpc.log) do @echo %%~i)> c:\tools\pc-names.log

In your original batch file, put the above for command at the very end. Then use c:\tools\pc-names.log as input for your powershell script.

Or you can go with @netballi's powershell solution. Your choice.
0
 
LVL 9

Author Comment

by:samiam41
Comment Utility
Just got it to work!!

@echo off
setlocal enabledelayedexpansion
set LogFile=C:\Tools\staleAdPc.log
if not exist "%LogFile%" goto SkipRename
for %%f in ("%LogFile%") do (
	for /f "tokens=1-3 delims=/ " %%a in ("%%~tf") do (set TimeStamp=%%c%%a%%b)
	set NewName=%%~nf.old.!TimeStamp!%%~xf
	echo Renaming "%LogFile%" to "!NewName!" ...
	ren "%LogFile%" "!NewName!"
	ECHO File date: %%~tf
)
:SkipRename

dsquery computer OU=Depts,OU=x,DC=x,DC=x,DC=x,DC=x -inactive 8 -limit 300 > c:\tools\staleADpc1.log
(for /f "tokens=2,3* delims=,=" %%i in (c:\tools\staleADpc1.log) do @echo %%~i)> c:\tools\pc-names.log

Open in new window

0
 
LVL 9

Author Closing Comment

by:samiam41
Comment Utility
Great job everyone!!  Thanks so much for your help with this and sticking around to make sure it got answered.  I hope to work with you all again soon!

Regards,
Aaron
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
No problem, no I don't know a way off hand of removing the "" either without using another for loop, which defeats the object.  What you can do though if you don't know is take the output of a command instead of needing a temporary file using ' ' around it in for loop, i.e.


(for /f "tokens=2,3* delims=,=" %%i in ('dsquery computer OU=Depts,OU=x,DC=x,DC=x,DC=x,DC=x -inactive 8 -limit 300') do @echo %%~i)> c:\tools\staleADpc1.log

Steve
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 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