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
cawasakiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
LDF files are log files from Access or MSSQL - so what is that about? Your demand only makes sense for text files.
0
cawasakiAuthor Commented:
Hi Qlemo,

its a file generated by ldifde export and it contain user account.
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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.
0
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.

cawasakiAuthor Commented:
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
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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.
0
cawasakiAuthor Commented:
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.
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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.
0
cawasakiAuthor Commented:
i have test :

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

i have 0 result????
0
cawasakiAuthor Commented:
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

0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
"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.
0
cawasakiAuthor Commented:
rename the file from test.txt to test.ldf
test.txt
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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

0
footechCommented:
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.
 screenshot
0
cawasakiAuthor Commented:
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:

command result
0
cawasakiAuthor Commented:
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
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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

0
cawasakiAuthor Commented:
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
0
cawasakiAuthor Commented:
error on script execution:

error code
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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.
0
cawasakiAuthor Commented:
Its possible to scann all file plz without limit script to name value?
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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).
0
cawasakiAuthor Commented:
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
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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)!
0
cawasakiAuthor Commented:
ok i will test

thanks
0
cawasakiAuthor Commented:
hi,

i still have error:

ldif error
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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

0
cawasakiAuthor Commented:
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

0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Replace move-item by copy-item to create a copy of the file.
And there was another mistake, I've used $result instead of $reason, and hence the detection did not work:
$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 '[éèç]' | % {
     $reason += "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
  { Copy-Item $ldf $target }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cawasakiAuthor Commented:
hi,

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

thanks
0
cawasakiAuthor Commented:
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
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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.
0
cawasakiAuthor Commented:
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
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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.
0
cawasakiAuthor Commented:
thank you for you help :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.