how can I edit an HTML tag using PowerShell?
I have created PS script that queries the Event Logs on my Servers then writes the info to a plain old html file. The problem being is the html file is just that, plian. What I want to do is add jquery's DataTables to it so that I can sort and organize it.
Using the "ConvertTo-HTML -head" commandlet I can insert the code I need into the head of the document before the document is written. After the file is written I then need to open it and change to .
The problem I'm having is when the script gets to the replace tag part, I get this error:
Set-Content : The process cannot access the file C:\users\norm\Documents\eventlogs\2011\02\System\04\tc2.html' because it is being used by another process.
I suspect that it is because the pipe used to open the file still has it open. So how do I close the file? or is there a better way of doing it?
Thanks, Norm
# EventLog viewer
# Head for the HTML document. Adds the CSS and JQuery DataTables to the document.
$h = '<head> <meta http-equiv="pragma" content="no-cache" />'
$h = $h + '<title>Celeste EventLog Viewer</title>'
$h = $h + '<link type="text/css" rel="stylesheet" href="css/demo_table_jui.css" />'
$h = $h + '<link type="text/css" rel="stylesheet" href="themes/base/jquery.ui.all.css" />'
$h = $h + '<link type="text/css" rel="stylesheet" href="css/stdcst.css" />'
$h = $h + '<script language="JavaScript" src="js/jquery.min.js"></script>'
$h = $h + '<script language="JavaScript" src="js/jquery.dataTables.js"></script>'
$h = $h + '</head>'
$h = $h + '<script type="text/javascript"> $(document).ready(function() {$("#t1").dataTable( {"bJQueryUI": true,"sPaginationType": "full_numbers", "bInfo" : true } ); });</script>'
$ServerNames = "s1","s2","s3","s4","s5","s6","s7","s8","s9"
$outdir = 'C:\users\norm\Documents\eventlogs\'
$year = get-date -uformat "%Y"
$month = get-date -uformat "%m"
$day = get-date -uformat "%d"
$count = $ServerNames.length
# Get the length of ServerNames, iterate through x until x is greater than ServerNames
for ($x=0; $x -lt $count; $x++)
{
# Before writing the file can begin the output directory needs to exsist. Test that it does, if not create it.
if (( Test-Path -path $outdir$year\$month\System\$day\) -ne $True)
{
New-Item $outdir$year\$month\System\$day\ -type directory
}
if (( Test-Path -path $outdir$year\$month\Application\$day\) -ne $True)
{
New-Item $outdir$year\$month\Application\$day\ -type directory
}
echo "Getting System log from " $ServerNames[$x]
#Query the System Event log and write it to an html fil开发者_如何学Goe
Get-EventLog -computername $ServerNames[$x] -LogName System | ConvertTo-HTML -head $h | Out-File -encoding utf8 $outdir$year\$month\System\$day\"$($ServerNames[$x]).html";
# Query the Aplication Event log and write it to an html file
echo "Getting Application log from " $ServerNames[$x]
Get-EventLog -computername $ServerNames[$x] -LogName Application | ConvertTo-HTML -head $h | Out-File -encoding utf8 $outdir$year\$month\Application\$day\"$($ServerNames[$x]).html";
}
for ($x=0; $x -lt $count; $x++)
{
#Open the files and replace the table tag
Get-Content $outdir$year\$month\System\$day\"$($ServerNames[$x]).html" | ForEach-Object { $_ -replace '<table>', '<table id="t1">'} | Set-Content $outdir$year\$month\"$($ServerNames[$x]).html";
}
#give me a message to let me know the script has completed.
echo "Done!"
I would use Handle to investigate what process accesses the file. Simiraly you can use ProcessExplorer (run, hit CTRL+F, type the file name, Enter).
Edit:
After looking at your code, I see the problem.
Line Get-Content | process something | Set-Content
is your problem. Change it to this:
$a = Get-Content ... | process ...
$a | Set-Content
Cmdlet Get-Content
opens file, reads one line and sends the line to pipe. Then it is processed and that first line is piped to Set-Content
. Set-Content
would like to write, but Get-Content
hasn't finished yet, because it read only first line.
Doesn't seem like it should be necessary to write it out to a file and then read it back in just to replace that tag:
Get-EventLog -computername $ServerNames[$x] -LogName System |
ConvertTo-HTML -head $h |
foreach-object { $_ -replace '<table>', '<table id="t1">'} |
Out-File -encoding utf8 $outdir$year\$month\System\$day\"$($ServerNames[$x]).html"
While your at it:
$h = @"
<title>Celeste EventLog Viewer</title>
<link type="text/css" rel="stylesheet" href="css/demo_table_jui.css" />
<link type="text/css" rel="stylesheet" "href="themes/base/jquery.ui.all.css" />
<link type="text/css" rel="stylesheet" href="css/stdcst.css" />
<script language="JavaScript" src="js/jquery.min.js"></script>
<script language="JavaScript" src="js/jquery.dataTables.js"></script>
</head>
<script type="text/javascript"> $(document).ready(function() {$("#t1").dataTable( {"bJQueryUI": true,"sPaginationType": "full_numbers", "bInfo" : true } ); });</script>
"@
精彩评论