我正在编写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)中提供所有数据。我希望每个文件或文件夹都显示在新行上。我该怎么办?如果我要键入Get-ChildItem -Force&#34; C:\ Users \ Someuser \ Downloads&#34;进入Powershell,它将逐行提供文件列表。我想在HTML页面上输出那种类型的输出。
答案 0 :(得分:2)
我认为你想要拆分的唯一原因是得到一个文件名数组。
如果是这样的话,你可以这样做:
$list = (Get-ChildItem -Force "C:\Users\Someuser\Downloads").FullName
从您的更新中,您需要将以下内容传递给您的函数:
createHTML($listDownloads -join "`n")