我想将脚本结果导出到新的CSV文件中。不幸的是Export-Csv
仅创建一个空文件。我认为Write-Host
存在问题,无法保存到文件中?
也许有一个选项可以只将控制台的输出保存到txt / csv文件中。
$Date = $((Get-Date).ToString('yyyy-MM-dd'))
$Evaluation = {
$Users = Get-ADUser -Filter 'enabled -eq $true' -SearchBase $OU -Properties mailNickname
Write-Host "Name;CAL Exchange"
foreach ($User in $Users) {
if ($User.mailNickName -eq $null) {
$User.CALEX = "No"
} else {
$User.CALEX = "Yes"
}
Write-Host "$($User.Name);$($User.CALEX)"
}
Write-Host ""
Write-Host "Count: $($Users.Count) Users"
}
$Evaluation_Department = {
$OU = "OU=ofDepartment"
& $Evaluation | Export-Csv -Path "C:\Support\$($Date)-Department.csv"
}
& $Evaluation_Department
我除了要放在文件中之外
Name;CAL Exchange $($User.Name);$($User.CALEX) [...] $($User.Name);$($User.CALEX) Count: $($Users.Count) Users
& $Evaluation_Department
仅用于测试目的。
答案 0 :(得分:1)
所需文件可以通过脚本的简化版本生成:
$Date = $((Get-Date).ToString('yyyy-MM-dd'))
$OU = "OU=ofDepartment"
$Users = Get-ADUser -Filter 'enabled -eq $true' -SearchBase $OU -Properties mailNickname
$Users | Select-Object Name,@{name="CAL Exchange";e={if($User.mailNickName) {return "Yes"} else {return "No"}}} | Export-CSV -Path "C:\temp\$($Date)-Department.csv" -NoTypeInformation
当然它最后没有Count: X Users
,但是我故意删除了它-就我个人而言,我不会将它放在.csv
文件中。您可以使用Add-Content
轻松添加。
注意:在将内容手动添加到CSV文件时,请小心数据格式-否则例如Excel可能无法读取它。通常,您必须记住关于"
答案 1 :(得分:0)
我认为这就是您要寻找的
$date = $((Get-Date).ToString('yyyy-MM-dd'))
$OU = "OU=_Users,CN=..."
$Users = Get-ADUser -Filter 'enabled -eq $true' -SearchBase = $OU -Properties mailNickname
$Users | Add-Member -MemberType NoteProperty -Name CALEX -Value $null -Force
foreach ($User in $Users)
{
if ($User.mailNickName -eq $null)
{
$User.CALEX = "No"
}
else
{
$User.CALEX = "Yes"
}
}
$Users | Select-Object SamAccountName, CALEX | Export-CSV -Path "C:\Support\$($Date)-Department.csv" -NoTypeInformation -Force