• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 753
  • Last Modified:

Remove select data from log file generated by batch script

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
samiam41
Asked:
samiam41
  • 4
  • 3
  • 2
  • +1
3 Solutions
 
TalShyarCommented:
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
 
Steve KnightIT ConsultancyCommented:
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
 
netballiCommented:
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
Problems using Powershell and Active Directory?

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

 
samiam41Author Commented:
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
 
Steve KnightIT ConsultancyCommented:
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
 
samiam41Author Commented:
@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
 
TalShyarCommented:
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
 
samiam41Author Commented:
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
 
samiam41Author Commented:
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
 
Steve KnightIT ConsultancyCommented:
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

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now