Solved

Remove select data from log file generated by batch script

Posted on 2013-06-11
10
748 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
[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
  • 2
  • +1
10 Comments
 
LVL 4

Expert Comment

by:TalShyar
ID: 39238409
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
ID: 39238439
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
ID: 39238477
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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 9

Author Comment

by:samiam41
ID: 39238583
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
ID: 39238595
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
 
LVL 9

Author Comment

by:samiam41
ID: 39238779
@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
ID: 39238923
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
ID: 39238939
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
ID: 39238946
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
ID: 39239056
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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…

688 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