这是一个powershell / AD / Exchange问题......
我正在针对一些用户运行一个脚本来检查他们的一些属性但是我无法将其输出到CSV。脚本运行良好,完全按照我的需要做,屏幕上的输出很好,我只是无法直接导出到csv。
输入是用户名的逗号分隔txt文件(例如“username1,username2,username3”)
我已经尝试过创建自定义ps对象,添加它们然后导出它们但它不起作用....
感激不尽的任何建议......
由于
乔治
$array = Get-Content $InputPath
#split the comma delimited string into an array
$arrayb = $array.Split(",");
foreach ($User in $arrayb)
{
#find group memebership
Write-Host "AD group membership for $User"
Get-QADMemberOf $User
#Get Mailbox Info
Write-Host "Mailbox info for $User"
Get-Mailbox $User | select ServerName, Database, EmailAddresses, PrimarySmtpAddress, WindowsEmailAddress
#get profile details
Write-Host "Home drive info for $User"
Get-QADUser $User| select HomeDirectory,HomeDrive
#add space between users
Write-Host ""
Write-Host "******************************************************"
}
Write-Host "End Script"
EDITED .... 我尝试过exproting的方法(只显示for循环/导出代码) 方法1
$AllData = @()
foreach ($User in $arrayb)
{
#set title for this user
#Write-host "Details for $User"
#find out their group memebership
Write-Host "AD group membership for $User"
$AdMemberOf = Get-QADMemberOf $User
Write-Host "ad completed"
Write-Host ""
Write-Host ""
#Get Mailbox Info
Write-Host "Mailbox info for $User"
$ExInfo = Get-Mailbox $User | select ServerName, Database, EmailAddresses, PrimarySmtpAddress, WindowsEmailAddress
Write-Host "ex completed"
Write-Host ""
Write-Host ""
#get profile details
Write-Host "Home drive info for $User"
$HomeInfo = Get-QADUser $User| select HomeDirectory,HomeDrive
Write-Host "home drive completed"
#add space between users
Write-Host ""
Write-Host "******************************************************"
$ReturnedObj = New-Object PSObject
$ReturnedObj | Add-Member NoteProperty -Name "AD Group Membership for $User" -Value $AdMemberOf
$ReturnedObj | Add-Member NoteProperty -Name "Exchange details for $User" -Value $ExInfo
$ReturnedObj | Add-Member NoteProperty -Name "Home drive info for $User" -Value $HomeInfo
Write-Host $ReturnedObj
$AllData += $ReturnedObj
}
Write-Host "starting csv export"
Write-Output $AllData |export-csv -Path $OutputPath -notype -force
Write-Host "End Script"
方法2
$ExportInfo = @()
foreach ($User in $arrayb)
{
$UserInfo = New-Object System.Object
#set title for this user
$ExportInfo += "Details for $User"
#Get Mailbox Info
$ExportInfo += Get-Mailbox $User
#find out their group memebership
$ExportInfo += Get-QADMemberOf $User
#get profile details
$ExportInfo += Get-QADUser $User| select HomeDirectory,HomeDrive
#add space between users
$ExportInfo += ""
$ExportInfo += ""
}
$ExportInfo | Export-Csv -Path $OutputPath ;
编辑:
感谢Stej的建议我现在有以下代码......仍然无法正常工作。我添加了一个检查以确认用户是否也存在于AD中。第一个问题是获取用户AD组成员资格 - 如果我设置了一个断点并查看特定用户AD成员资格的值,则teh varialbe中的值为错误“无法处理参数,因为参数”obj“的值为将参数“obj”的值更改为非空值。“不知道造成这种情况的原因是什么,所以我暂时忽略了它并注释掉了AD成员资格的这些行,专注于让其他人工作。
随着行注释掉,脚本运行正常,底部有一个断点我可以查看$ ExportInfo的值,它们看起来都很好,即它们都已正确保存到变量中。但是,它不会输出。正如你所看到的,我发出命令将$ ExportInfo写入屏幕,但是没有显示任何内容。当它尝试执行导出行(即“Export-Csv $ ExportInfo -Path $ OutputPath”)时,它出错: “无法将'System.Object []'转换为参数'Delimiter'所需的类型'System.Char'。不支持指定的方法。 在:行:80 char:10 + Export-Csv<<<< $ ExportInfo -Path $ OutputPath“
我将该行打包为“$ ExportInfo | Export-Csv -Path $ OutputPath”,它现在导出为CSV ...不知道为什么???但是有两个问题......如上所述,AD群组不起作用,而且电子邮件地址字段(应该返回{SMTP:j.smith @ domain.com,smtp:j.smith @ domain.com.au, smtp:smithj@exchange.domain.com.au和SIP:j.smith@domain.com.au})只是在CSV中显示为“Microsoft.Exchange.Data.ProxyAddressCollection”。再次,检查$ ExportInfo,地址就在那里....
由于
$ExportInfo = @()
foreach ($User in $arrayb)
{
$CheckUser = Get-QADUser -Name $User
if (!$CheckUser)
{
$CountUser++
Write-Warning "############################################"
Write-Warning "$user not found in AD"
Write-Warning "############################################"
}
else
{
$CountUser++
$UserInfo = New-Object System.Object
#find out their group memebership
Write-Host "AD group membership for $User"
#$Temp = Get-QADMemberOf $User
#$UserInfo | Add-Member NoteProperty -Name "AD Group Membership" -Value $Temp.Name
#set title for this user
#Write-host "Details for $User"
#Get Mailbox Info
Write-Host "Mailbox info for $User"
$Temp = Get-Mailbox $User #| select ServerName, Database, EmailAddresses, PrimarySmtpAddress, WindowsEmailAddress
$UserInfo | Add-Member NoteProperty -Name "ServerName" -Value $Temp.ServerName
$UserInfo | Add-Member NoteProperty -Name "Database" -Value $Temp.Database
$UserInfo | Add-Member NoteProperty -Name "Email Addresses" -Value $Temp.EmailAddresses
$UserInfo | Add-Member NoteProperty -Name "Primary SMTP" -Value $Temp.PrimarySmtpAddress
$UserInfo | Add-Member NoteProperty -Name "Windows Email Address" -Value $Temp.WindowsEmailAddress
#$ReturnedObj | Add-Member NoteProperty -Name
#get profile details
Write-Host "Home drive info for $User"
$Temp = Get-QADUser $User #| select HomeDirectory,HomeDrive
$UserInfo | Add-Member NoteProperty -Name "Home Directory Location" -Value $Temp.HomeDirectory
$UserInfo | Add-Member NoteProperty -Name "Home Drive Mapped To" -Value $Temp.HomeDrive
#add space between users
Write-Host ""
Write-Host "******************************************************"
$ExportInfo += $UserInfo
}#end else
}
Write-Host "blah"
Write-Host $ExportInfo
Export-Csv $ExportInfo -Path - $OutputPath
Write-Host "Number of Users processed: $CountUser"
答案 0 :(得分:1)
有错误信息吗? $error[0]
中有什么东西吗? 无效是什么意思?
编辑:
在第二种方法中,您可以创建$UserInfo
对象,但不要使用它。我想它应该被添加到$ExportInfo
。而不是将此字符串添加到$ExportInfo
,这就是您为csv文件提供如此奇怪内容的原因。
如何正确完成:
$UserInfo = New-Object System.Object
$ReturnedObj | Add-Member NoteProperty -Name "User Name" -Value $user.Name
$ReturnedObj | Add-Member NoteProperty -Name "User otherValue" -Value $user.othValue
#Get Mailbox Info
$mailBox = Get-Mailbox $User
$ReturnedObj | Add-Member NoteProperty -Name "User Mail box size" -Value $mailBox.Size
$ReturnedObj | Add-Member NoteProperty -Name "User Mail box -count of messages" -Value $user.countOfMessages
... # and so on
$ExportInfo += $ReturnedObj
请注意,要导出到csv 的对象必须具有string,integer,bools 等类型的属性。不包含$User
之类的对象或{Get-Mailbox $User
返回的对象。 1}}。 csv中的值必须是原始的。这就是原因。
这适用于您添加值为$AdMemberOf
的NoteProperty的第一个示例。这是无法导出到csv的对象本身。您必须为$AdMemberOf
的每个有趣属性创建一堆属性。
答案 1 :(得分:1)
原来我需要使用一些foreach循环来获取我需要的信息....所以:
foreach($user in $array)
{
$temp = Get-QADUser -Name $User
if (!$temp)
{
$log += "????????????????" + "`n"
$log += "$user not found in AD" + "`n"
$log += "????????????????" + "`n"
}
else
{
#find out their group memebership
$log += "AD group membership for $User" + "`n"
$temp = Get-QADMemberOf $User
foreach ($drive in $temp)
{
$temp2 = $drive
$temp2 = $temp2.Substring(6)
$log += "`t" + $drive + "`n"
}#end foreach for drive loop
$temp = Get-Mailbox $User
if ($temp.RecipientType -like "UserMailbox")
{
$log += "Mailbox info for $User" + "`n"
#$log += Get-Mailbox $User | select ServerName, Database, EmailAddresses, PrimarySmtpAddress, WindowsEmailAddress
$log += "Email Server: " + $temp.ServerName + "`n"
$log += "Email Database: " + $temp.Database + "`n"
$log += "Primary SMTP: " + $temp.PrimarySmtpAddress + "`n"
$log += "Windows Email Address: "+ $temp.WindowsEmailAddress + "`n"
#$log += "`n"
foreach ($e in $temp.EmailAddresses)
{
$log += "`t" + "Email Addresses: " + $e + "`n"
}
}
else
{
$log += "########" + "`n"
$log += "$User is not a MailboxUser, IE no Exchange Mailbox" + "`n"
$log += "########" + "`n"
}
$log += "Home drive info for $User" + "`n"
$temp = Get-QADUser $User| select HomeDirectory,HomeDrive
$log += "Home Directory: " + $temp.HomeDirectory + "`n"
$log += "Home Drive Letter: " + $temp.HomeDrive + "`n"
$tempvar = [string] $temp.HomeDirectory
if ($tempvar -eq "")
{
$noHomedirectory += $User + "`n"
$countNoHOmeDirectory ++
}
}#end of the main if/else to determine if the AD account exists
$OutputPath = "C:\SomeFolder\"+"User_Report_"+([datetime]::Now).tostring("yyyyMMddhhmmss")+".txt"
$log | Out-File -FilePath $OutputPath
然后我将所有特定日志(例如$ noHomeDirectory等)转储到电子邮件正文中,附上上面导出的完整日志并发送给自己。
感谢上面的每个人提出的建议,并对于延迟回复答案感到抱歉......