Solved

Remove select data from log file generated by batch script

Posted on 2013-06-11
10
744 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
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
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.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
A brief introduction to what I consider to be the best editor for PowerShell.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

831 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