我创建了PS脚本,查询我的服务器上的事件日志,然后将信息写入一个普通的旧HTML文件。问题是html文件只是那个,plian。我想要做的是添加jquery的DataTables,以便我可以对它进行排序和组织。
使用“ConvertTo-HTML -head”命令行开关,我可以在编写文档之前将我需要的代码插入到文档的头部。写完文件后,我需要打开它并更改为。
我遇到的问题是当脚本到达替换标记部分时,我收到此错误:
Set-Content:进程无法访问文件C:\ users \ norm \ Documents \ eventlogs \ 2011 \ 02 \ System \ 04 \ tc2.html',因为它正由另一个进程使用。
我怀疑这是因为用于打开文件的管道仍然打开它。那么我该如何关闭文件呢?或者有更好的方法吗?
谢谢, 规范
# 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 file
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!"
答案 0 :(得分:2)
我会使用Handle来调查访问该文件的进程。 Simiraly你可以使用ProcessExplorer(运行,点击CTRL + F,输入文件名,输入)。
编辑:
查看代码后,我看到了问题所在。
行Get-Content | process something | Set-Content
是您的问题。将其更改为:
$a = Get-Content ... | process ...
$a | Set-Content
Cmdlet Get-Content
打开文件,读取一行并将该行发送到管道。然后处理它并将第一行传送到Set-Content
。 Set-Content
想写,但Get-Content
尚未完成,因为它只读取第一行。
答案 1 :(得分:1)
似乎没有必要将其写入文件,然后将其读回以替换该标记:
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"
答案 2 :(得分:1)
在你的时候:
$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>
"@