Solved

Remove select data from log file generated by batch script

Posted on 2013-06-11
10
746 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
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
size of file 1 22
Powershell - Save filename to variable 5 29
PowerShell and cisco ios 3 40
Script to Ping/Resovle names given IP 4 17
This article will help you understand what HashTables are and how to use them in PowerShell.
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).
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
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…

685 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