代码:
$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
现在我得到了,每次拨打$ 。无论什么,一切。例如,当我调用$ .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
答案 0 :(得分:3)
你没有说什么不起作用,所以这里有一些通用的建议。如果您可以发布任何错误消息或特定的无效消息,我可以更具体。
正如评论者所述,(Get-Date).ToString()
会返回类似7/10/2013 7:38:22 AM
的内容,这些内容可能在文件名中有效,也可能无效。您需要对其进行格式化,以便您知道它可以在文件名中使用,例如: $fileName = '{0:yyyy-MM-dd-HH-mm-ss}.txt' -f (Get-Date)
。
其次,PowerShell有一个Import-Csv函数,它将读取CSV文件并将其转换为对象。然后,您可以处理每个对象,该对象将具有CSV的每一列的属性,并在目标文件中返回所需的字符串。
这是一个更新的脚本:
$fileName = '{0:yyyy-MM-dd-HH-mm-ss}.txt' -f (Get-Date)
$outputFile = Join-Path 'C:\Users\<username>\Desktop' $filename
New-Item $outputFile -ItemType File
Import-Csv -Path 'C:\Users\<username>\Desktop\test.csv' |
ForEach-Object {
" <tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td><td>{7}</td><td>{8}</td><td>{9}</td><td>{10}</td><td>{11}</td><td>{12}</td><td>{13}</td></tr>" -f `
$_.Date,$_.Time,$_.'Call Type',$_.'Calling Number',$_.'Calling Extension',$_.'Calling Department',$_.'Called Number',$_.'Called Extension',$_.'Called Department',$_.'Call Connected',$_.Duration,$_.'Queuing Time',$_.'Account Code',$_.'Carrier Code'
} |
Add-Content $outputFile