【发布时间】:2015-03-05 16:19:12
【问题描述】:
我正在编写一个 PowerShell 脚本,我想捕获目录中的文件列表并将其存储到一个变量中。捕获后,我想将其打印到 HTML 文件中。我已经捕获了这样的数据:
$listDownloads = Get-ChildItem -Force "C:\Users\Someuser\Downloads"
现在,我想将$listDownloads 转换为字符串并将其拆分。我该怎么做呢?
我试过这个:
$listSplit = $listDownloads.ToString().Split(" ")
但我得到了这个输出:
System.Object[]
更新:
$html = '<html>'
$body = '<body>'
$p = '<p>'
$pClose = '</p>'
$StatusDownloads = 'Directory of the Downloads Folder'
$pTags = $p.ToString() + $StatusDownloads + $pClose.ToString();
$listDownloads = (Get-ChildItem -Force "C:\Users\Someuser\Downloads").Name;
function createHTML($addtoFile){
$addtoFile | Add-Content 'status.html'
}
function printData($printData){
Write-Output $printData
}
$html | Set-Content 'status.html'
createHTML($html)
createHTML($body)
createHTML($pTags)
createHTML($listDownloads)
当我尝试打印 $listDownloads 时,它会将数据全部显示在同一行(即文件 1 文件 2)我希望每个文件或文件夹都显示在新行上。我该怎么做?如果我在 Powershell 中键入 Get-ChildItem -Force "C:\Users\Someuser\Downloads" ,它将逐行给出文件列表。我想在我的 HTML 页面上输出这种类型的输出。
【问题讨论】:
标签: string powershell split