Link to home
Start Free TrialLog in
Avatar of cawasaki
cawasaki

asked on

Powershell script to check ldf file

hi,

i need to do this:
from a windows 2003 server, with powershell v2 installed, i need a script to do this:

get an ldf file and check content, if detect any special char ç, é è ô  ï ö...... or if the file is empty (0 ko)==> it send email with the ldf file attached and if possible on email body right wich special char it have detect or if the file is empty tell empty file detected on email body.


if not detect special char and the file is not empty==> copy the ldf file to \\server\folder1\folder2.

are this is possible?

thanks for help
Avatar of Qlemo
Qlemo
Flag of Germany image

LDF files are log files from Access or MSSQL - so what is that about? Your demand only makes sense for text files.
Avatar of cawasaki
cawasaki

ASKER

Hi Qlemo,

its a file generated by ldifde export and it contain user account.
So I assume you are only after the name entries. Just note character which is ok in the search string:
select-string "^name: [^A-Za-z0-9 \-]" .\test.ldf

Open in new window

BTW, there are better means to check for "invalid" usernames, like querying AD directly with PS.
hi,

i dont understand? it possible or not?

if the extension is the problem, the script can rename file to txt file?

thanks for help
My code line is doing all: It shows all lines starting with "name: ", followed by any characters not in the list provided. The regular expression [^ says "any character not contained in". So I've allowed latin letters, digits, space, dash (needs to get escaped with a backslash, because a dash has a special meaning here).
The file checked is test.ldf in the current folder, but of course you change that to your liking.
Ok, but what i need is to fund any character like é or è for exemple:

this is the contain of test.ldf:

fsdlfjsdlfjslé
sdjhlsdjlsdfè
sdhflhlsdflksdç

i need the script to detect é from first line, è from second and ç from third line.
A ldifde file isn't like that. Do you or do you not want to resrict on lines with "name:"? You can modify the search string to be any regular expression, so if you want those 3 specific characters (not differing betweeen the lines and what they mean):
select-string '[éèç]' .\test.ldf

Open in new window

that is, just enumerate them between square brackets.
i have test :

select-string '[éèç]' .\test.ldf

i have 0 result????
why not use the get-content command and found this char?


PS C:\temp> Get-Content test.ldf
fsdlfjsdlfjslé
sdjhlsdjlsdfè
sdhflhlsdflksdç

Open in new window

"Why not ..."? Because that is the same, but slower. Doesn't matter for a single, small file, but the more files and the bigger they get, the slower is "manually" scanning them.

When I test with your strings, I get them back. You are doing something different - is the file UTF8 or Unicode, maybe? Post an example file, please, so I can test with your "original" data.
rename the file from test.txt to test.ldf
test.txt
Ok, you convinced me. For some reasons I do not know, select-string has issues with those characters if found in a file. So let's fall back to what you suggested (sigh):
get-content test.ldf | select-string '[éèç]'

Open in new window

I thought this was interesting that Select-String wasn't finding those characters, so I did some investigating.  It appears that Select-String will find them without problem if the file is Unicode or UTF8 (maybe others), just not when the file is ASCII/ANSI.  I wonder if using the unicode switch for ldifde would produce a unicode-encoded file (can't test right now).  However, even if the file is ASCII, I got it to work if I added the encoding parameter set to "default" ("utf7" also worked).
select-string -pattern "[èéç]" -path .\test.ldf -Encoding default

Some interesting results along the way showing how PS interprets different encodings.
 User generated image
Hello,

ok the 2 code work, my test.ldf file containt:

fsdlfjsdlfjslé
sdjhlsdjlsdfè
sdhflhlsdflksdç
dsqdsdqsdqs
dqsdqsd
gxf
gh
ch
gf
hh
gf

Open in new window


and the result of script:

User generated image
so now, how i can use this line command with script to do this:

get an ldf file and check content, if detect any special char ç, é è ô  ï ö...... or if the file is empty (0 ko)==> it send email with the ldf file attached and if possible on email body right wich special char it have detect or if the file is empty tell empty file detected on email body.


if not detect special char and the file is not empty==> copy the ldf file to \\server\folder1\folder2.

thanks very much for help
You didn't refer to my point that the ldifde produces a file with much more info than the usernames. I don't expect french accents in the other AD properties, so it will not matter for those most probably, but if you extend your request.
Leaving that aside:
$ldf= 'C:\Temp\EE\test.ldf'
$target = '\\server\folder1\folder2'

$reason = $null
$attach = $true
ldifde -x -l name -f $ldf | out-null
if (!test-path $ldf)
  { $reason = 'no file'; $attach = $false }
elseif ((get-childitem $ldf).Length -eq 0)
  { $reason = 'no content' }
else
{
  get-content $file | select-string '[éèç]' | % {
     $result += "Invalid chars in  $_  =>  $($_ -replace '[^éèç]', '.')`n"
  }
}
if ($reason)
  {
    send-mailmessage -SmtpServer smtp.here.com -From me@here.com `
       -To you@there.com -Subject 'LDIFDE Error report' -Body $reason `
       -Attachments $(if ($attach) { $ldf } else { $null })
  }
else
  { move-item $ldf $target }

Open in new window

i understand what you say.

in the ldf file, the special char can be in this line:

displayName: tony Paté
givenName: paté

but i prefer check all file if possible.

what this line code do???:

ldifde -x -l name -f $ldf | out-null
error on script execution:

User generated image
Sorry, forgot to fix that; replace line 7:
if (!(test-path $ldf))

Open in new window

ldifde -x -l name -f $ldf | out-null

Open in new window

creates the LDF file without any binary values, and only attributes related to the name.
Its possible to scann all file plz without limit script to name value?
The script works on whatever is contained in the ldifde export file. Just tailor the command to create that file, e.g. by removing "-l name" (but I would not include binary data, "-x" excludes that).
hi Qlemo.

the problem is i need to not modify the ldf file, because i send it to other adam server and import it.

i need the script only do what i want without any change

thank you
Gosh. What is the issue? Just remove the ldifde command of my script (line 6), and use the existing file's name in $ldf (line 1)!
ok i will test

thanks
hi,

i still have error:

User generated image
Are you using this code, only changing the first two lines?
$ldf= 'C:\Temp\EE\test.ldf'
$target = '\\server\folder1\folder2'

$reason = $null
$attach = $true
# ldifde -x -l name -f $ldf | out-null
if (!(test-path $ldf))
  { $reason = 'no file'; $attach = $false }
elseif ((get-childitem $ldf).Length -eq 0)
  { $reason = 'no content' }
else
{
  get-content $ldf | select-string '[éèç]' | % {
     $result += "Invalid chars in  $_  =>  $($_ -replace '[^éèç]', '.')`n"
  }
}
if ($reason)
  {
    send-mailmessage -SmtpServer smtp.here.com -From me@here.com `
       -To you@there.com -Subject 'LDIFDE Error report' -Body $reason `
       -Attachments $(if ($attach) { $ldf } else { $null })
  }
else
  { move-item $ldf $target }

Open in new window

hi,

i have do 2 test.

1 test, no special char on ldf file, the script work and copy the file on remote server (the script not copy file, but it move it, i need to keep file on local destination, and copy it remotely)

2- i have modify the ldf, it containt a special char "é", script must not copy the file and send email, but it copy it, here my ldf contain:

on givenname and displayname, you can see "é" character:

dn: CN=stephanie.volitas,OU=paris,OU=user,DC=domain,DC=com
changetype: add
objectClass: top
objectClass: syncEngineAuxObject
objectClass: person
objectClass: organizationalPerson
objectClass: user
cn: stephanie.volitas
telephoneNumber: 02586565656
displayName: VOLITAS Stéphanie
sAMAccountName: stephanie.volitas
sn: VOLITAS
department: FINANCE
mail: stephanie.volitas@domain.com
givenName: Stéphanie
title: consultant

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hi,

its work but i need to test it one a few days more.

thanks
hi,

thanks for this script it work perfectly.

at the end i need just at the end when Copy-Item $ldf $target:

i need to rename  the test.ldf file to a new name for exemple test100.ldf (only rename file in destination folder not in source folder)

thanks you for help
You can provide a fully qualified file name in $target, instead of a path, and that should do the trick. But that uses the same name all the time.
ok perfect, for email destination, how i can use 2 or 3 different email adress

i can separate the adresse by ,  ?

like

-to email1,email2,email3  ??
thanks
Yes. If you look at  send-mailmessage -?  or  get-help send-mailmessage, you'll see a  [-To] <string[]>, which means "the -To parameter takes an array of strings". Arrays are built by enumerating values separated by comma.
That is the way you can help yourself if in doubt - ask get-help.
thank you for you help :)