如果我想将导出文件格式更改为excel,有什么办法吗?
谢谢。
Powershell脚本
Import-Module ActiveDirectory
$refDate = (Get-Date).AddDays(1).Date
New-PSDrive -Name UNCPath -PSProvider FileSystem -Root "\\Test-PC\Test\"
$logTime = Get-Date -Format "yyyyMMdd"
$logFile = 'UNCPath:\Log\'+$logTime+".log"
start-transcript $logFile
$users = Get-AdUser -Filter {AccountExpirationDate -eq $refDate} –Properties EmailAddress, AccountExpirationDate -ErrorAction SilentlyContinue
if ($users) {
$fileOut = 'UNCPath:\'+ 'ExpiryUserList' + ".csv"
$users | Select-Object -Property SamAccountName, Name, EmailAddress, AccountExpirationDate |
Export-CSV -Path $fileOut -NoTypeInformation
Write-Host ""
Write-Host "Result: Record have found!"
Write-Host ""
Stop-Transcript
}
else {
Write-Host ""
Write-Host "Result: No expired accounts found!"
Write-Host ""
Stop-Transcript
}
答案 0 :(得分:0)
众所周知,Excel会很自然地读取csv,但是要想得到这个,是需要将CSv转换为XL *。
所以重构一下代码...
Import-Module ActiveDirectory
$refDate = (Get-Date).AddDays(1).Date
# Note, you are nto using this anywhere in your code, so why specify this at all
# New-PSDrive -Name UNCPath -PSProvider FileSystem -Root "\\Test-PC\Test\"
$logTime = Get-Date -Format "yyyyMMdd"
$logFile = "UNCPath:\Log\$($logTime).log"
start-transcript $logFile
$users = Get-AdUser -Filter {AccountExpirationDate -eq $refDate} –Properties EmailAddress, AccountExpirationDate -ErrorAction SilentlyContinue
if ($users)
{
$fileOut = 'UNCPath:\ExpiryUserList.csv'
$users | Select-Object -Property SamAccountName, Name, EmailAddress, AccountExpirationDate |
Export-Csv -UseCulture -Path $fileOut -NoTypeInformation -Encoding UTF8
"`nResult: Record have found!`n"
# Convert to Excel format by a load into Excel
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $true
$excel.Workbooks.Open("$fileOut").SaveAs("$($fileOut.BaseName).xlsx",51)
$excel.Quit()
explorer.exe "/Select,$fileOut"
}
else {Write-Warning -Message "`nResult: No expired accounts found!`n"}
Stop-Transcript
但是,我同意AdminOfThings,通过MS powershellgallery.com可以找到一些更谨慎的模块。
Find-Module -Name '*Excel*' | Format-Table -AutoSize
<#
# Results
Version Name Repository Description
------- ---- ---------- -----------
7.1.0 ImportExcel PSGallery PowerShell module to import/export Excel spreadsheets, without Excel....
0.1.5 PSWriteExcel PSGallery Little project to create Excel files without Microsoft Excel being installed.
1.0.2 PSExcel PSGallery Work with Excel without installing Excel
19.0.7354.0 ExcelCmdlets PSGallery CData Cmdlets for Excel
19.0.7354.0 ExcelServicesCmdlets PSGallery CData Cmdlets for Excel Services
0.1.6 BitTitan.Runbooks.Excel PSGallery PowerShell module for Excel-related functions and resources used in BitTitan Runbooks
19.0.7354.0 ExcelOnlineCmdlets PSGallery CData Cmdlets for Excel Online
0.6.9 ExcelPSLib PSGallery Allow simple creation and manipulation of XLSX file
0.1.6 BitTitan.Runbooks.Excel.Beta PSGallery PowerShell module for Excel-related functions and resources used in BitTitan Runbooks
0.0.4 Excelimo PSGallery Simple project generating Excel Workbooks/Worksheets
0.0.1 ProductivityTools.PSExcel2SQL PSGallery Module takes all excel files in given directory and push the content to database.
#>