代码:
$date = (Join-path C:\Users\<username>\Desktop\ (Get-Date).toString())
New-Item -Path c:\Users\<username>\Desktop -Name "" + $((Get-Date).toString()) + ".txt" -ItemType file
$reader = [System.IO.File]::OpenText("C:\Users\<username>\Desktop\test.csv")
try {
for(;;) {
$line = $reader.ReadLine()
if ($line -eq $null) { break }
# process the line
Add-Content "C:\Users\<username>\Desktop\"($date.txt) -value $line
$line
}
}
finally {
$reader.Close()
}
我已经尝试了十几种不同的方法,但我不知道该怎么做。我正在尝试制作一个包含日期作为名称一部分的文件(用于文档,因为我会定期做几次),并处理要更改的文件的每一行并将其放入新文件中。有谁知道如何让它工作?
编辑:好的,所以我通读了它,它几乎可以工作。不幸的是,我认为我没有正确调用这些对象。
我的代码:
$fileName = '{0:yyyy-MM-dd HH-mm-ss}.html' -f (Get-Date)
New-Item $fileName -itemtype file
Add-content $fileName "<html>"
Add-content $fileName "<head>"
Add-content $fileName " <title>Call Logs</title>"
Add-content $fileName "<head>"
Add-content $fileName "<body>"
Add-content $fileName " <div id='header'>"
Add-content $fileName " </div>"
Add-content $fileName " <div id='table'>"
Add-content $fileName " </table>"
Add-content $fileName " <tr>"
Add-content $fileName " <th>Date</th>"
Add-content $fileName " <th>Time</th>"
Add-content $fileName " <th>Call Type</th>"
Add-content $fileName " <th>Calling Number</th>"
Add-content $fileName " <th>Calling Extension</th>"
Add-content $fileName " <th>Calling Department</th>"
Add-content $fileName " <th>Called Number</th>"
Add-content $fileName " <th>Called Extension</th>"
Add-content $fileName " <th>Called Department</th>"
Add-content $fileName " <th>Call Connected</th>"
Add-content $fileName " <th>Duration</th>"
Add-content $fileName " <th>Queuing Time</th>"
Add-content $fileName " <th>Account Code</th>"
Add-content $fileName " <th>Carrier Code</th>"
Add-content $fileName " </tr>"
import-csv -path 'C:\users\<username>\Desktop\test.csv' |
foreach-object{
$newRow = " <tr><td>$_.Date</td><td>$_.Time</td><td>$_.'Call Type'</td><td>$_.'Calling Number'</td><td>$_.'Calling Extension'</td><td>$_.'Calling Department'</td><td>$_.'Called Number'</td><td>$_.'Called Extension'</td><td>$_.'Called Department'</td><td>$_.'Call Connected'</td><td>$_.Duration</td><td>$_.'Queuing Time'</td><td>$_.'Account Code'</td><td>$_.'Carrier Code'</td></tr>"
return $newRow
} |
Add-content $fileName
现在我得到,每次调用 $ .Whatever,一切。例如,当我调用 $ .Date 时,我得到
@{Date=7/1/2012; Time=16:43:06; Call Type=Terminating; Calling Number=XXXXXXXXXX; Calling Extension=; Calling Department=; Called Number=YYYYYYYYYY; Called Extension=XXXX; Called Department=Sales Engineers; Call Connected=No; Duration=0:00:11; Queuing Time=; Account Code=; Carrier Code=}.Date