Tuesday, June 29, 2010

Filter all lines of a text file with multiple strings list

To display only lines containing a string in a text file, easiest way is to use findstr commandline. For example, if we want to display lines containing "ok" (with /i switch for no case sensitivity) in a file file.txt we just to run at command line something like

type file.txt | findstr /i ok


With findstr you can also display lines NOT containing a particular string. For example you want all lines except line with string "not good" with this command

type file.txt | findstr /i /v /c:"not good"


Today my problem is that I need to report all administrators users of all computers except some users in a list.(yes, there is not SCCM or another tool which permit to do it here). I could do it like this:

type file.txt findstr /i /v /c:"administrator1" findstr /i /v /c:"administrator2" findstr /i /v /c:"administrator3"


But that's not really simple to maintain if you need to do some changes. Moreover, why do not do something which could be used for others problems of the same type?
So to do better than excel :) I created this batch file filter.cmd

@echo off
REM
REM Line Filter
REM By F.RICHARD
REM
REM created 2010 July
REM modified 2010 september for %1
REM

SET CURDIR=%~d0%~p0
cd /d %CURDIR%

IF {%1}=={} GOTO :ERRFILEINPUT
SET INPUTFILE=%1
echo Input file:%INPUTFILE%
If NOT EXIST %INPUTFILE% GOTO :ERRNEINPFILE
IF {%2}=={} GOTO :ERRFILEFILTER
SET FILTERFILE=%2
echo Filter file:%FILTERFILE%
If NOT EXIST %FILTERFILE% GOTO :ERRNEFILFILE
IF {%3}=={} GOTO :ERRFILERESULT
SET RESULTFILE=%3
echo Result file:%RESULTFILE%

setlocal EnableDelayedExpansion
set /a nbstr=0

Echo Begin filter
type %INPUTFILE% > tempfile!nbstr!.txt
for /F "tokens=*" %%i IN (%FILTERFILE%) Do (
set tmpfileread=tempfile!nbstr!.txt
set /a nbstr=!nbstr!+1
set tmpfileresult=tempfile!nbstr!.txt
type !tmpfileread! | findstr /v /i /c:%%i > !tmpfileresult!
del /f /q !tmpfileread!
)
move /Y !tmpfileresult! %RESULTFILE%
Echo End filter
goto :EOF

:ERRFILEINPUT
Echo.
Echo ERROR: you need an input filename
goto :Syntax

:ERRFILEFILTER
Echo.
Echo ERROR: you need a filter file
goto :Syntax

:ERRFILERESULT
Echo.
Echo ERROR: you need a result filename
goto :Syntax

:ERRNEINPFILE
Echo.
Echo ERROR: input file - %INPUTFILE% - does not exist in %CURDIR% directory
goto :EOF

:ERRNEFILFILE
Echo.
Echo ERROR: filter file - %FILTERFILE% - does not exist in %CURDIR% directory
goto :EOF

:Syntax
Echo.
Echo Syntax:
Echo %~nx0 inputfile filterfile resultfile
Echo.
goto :EOF


use it with something like

stringfilter.cmd input.txt filter.txt result.txt


With a input file like this input.txt

aaaaaaa
do not want to see it
bbbbbbb
not this one
ccccccc


This batch use a filter file like this filter.txt

"not this"
bbbbbbb
"not want"


and result file was like this result.txt

aaaaaaa
ccccccc


Et voilà

You can download this script
here

Saturday, June 26, 2010

Get all OU's computers in a AD

If you need to have all computers of a specific OU, just run




@dsquery computer "OU=YourOU,DC=Domain,DC=Net" -limit 0 -o rdn > Computers_YourOu_domain_net.txt


Result will be savec in a file Computers_YourOu_domain_net.txt that will be like this:

"computer1"
"computer2"
"computer3"


Now you can use it to run a script on each OU's computers :-)

Friday, June 18, 2010

Append/Consolidate a lot of files to 1 file

Again a need I have regurlarly. You have a lot of files you want to append in only one text. For example a lot of .csv files that you want to consolidate in 1 .csv file

Here it's a powershell script that consolidate all file in a folder to a file "_allFilesInOne.txt". Just put this file GetAllFilesInOne_ps1.txt in a folder and run it (renamed it .ps1 before)


#
# Get All Files In One File
#
# F.Richard
# 2010 June
#
$appendfile = "_allFilesInOne.txt"
$files = Get-ChildItem * -exclude $appendfile,$MyInvocation.MyCommand.Name -include *
$Content = ""
$Content | Out-File $appendfile
ForEach ($filename in $files) {
Write-Host $filename
$Content = Get-Content $filename
$Content | Out-File -append $appendfile
}


You can improve this script easily.
For example, you can exclude .log files by changing
-exclude $appendfile,$MyInvocation.MyCommand.Name
by
-exclude $appendfile,$MyInvocation.MyCommand.Name,*.log

You can can also change inclusion of all files to only .txt files by changing
-include *
by
-include *.txt

Get user SID and vice versa

For a Citrix problem, support team had to know correspondance between user or computer name and SID. If you don't know what is SID, it's something like S-1-5-21-1009609029-4169793705-2371228398 and correspond to an unique identifier of a user, computer or group (see definition in Technet Glossary at http://technet.microsoft.com/en-us/library/cc776189(WS.10).aspx#s)

Well In my toolbox I have 2 very old vbs scripts from Shane Boudreaux that permit to convert SID to username and vice versa

Convert Username to SID
http://www.thescriptlibrary.com/Default.asp?Action=Display&Level=Category3&ScriptLanguage=VBScript&Category1=Security&Category2=Other&Title=Convert%20Username%20to%20SID (Alternate Download)

Convert SID to Username
http://www.thescriptlibrary.com/Default.asp?Action=Display&Level=Category3&ScriptLanguage=VBScript&Category1=Security&Category2=Other&Title=Convert%20SID%20to%20Username (Alternate Download)

But now Microsoft sysinternals have a tool PsGetSid (http://technet.microsoft.com/en-us/sysinternals/bb897417.aspx) (Alternate Download) that do the same

If you need to know how sysinternals do, you can check source code sid2user.cpp and user2sid.cpp from Evgenii B. Rudnyi http://evgenii.rudnyi.ru/soft/sid/ (Alternate Download user2sid.cpp and sid2user.cpp)

If you are a powershell geek, go directly to Powershell Guy blog http://thepowershellguy.com/blogs/posh/archive/2007/01/23/powershell-converting-accountname-to-sid-and-vice-versa.aspx (Alternate Download)

Thursday, June 17, 2010

Blog Syntax Highlighting

For my blog, I search something to code Syntax Highlighter (see http://en.wikipedia.org/wiki/Syntax_highlighting if you don't know what that means). Finally, I think there is 2 good choices:

- the simplest:
Use http://www.thecomplex.plus.com/highlighter.html or http://tohtml.com/ to translate your code in your language. Just HTML copy code to you blog .
If you prefer, you can also install a free software: "highlight" from http://www.andre-simon.de/
(Alternate Download)

- the most complete (because it permit to add "copy clipboard" and "view plain" ):
Use http://alexgorbatchev.com/wiki/SyntaxHighlighter (with integration example in http://alexgorbatchev.com/SyntaxHighlighter/integration.html)

Wednesday, June 16, 2010

return to my blog

I decided to come back to my blog...