I'm currently trying to get a list of Remote Desktop Services settings for a number of users in a particular OU, and export this list of users and their current RDS settings as a CSV. The problem is that these properties are of the type PropertyValueCollection
and this does not seem to be playing well with Export-CSV
. Someone recommended converting these to a string, but I'm still receiving a string conversion error after trying this out. Can anyone help me figure out how to get this to output properly to CSV?
$rows = @()
$Accounts = Get-ADUser -Filter * -SearchBase $OU -Properties *
$Accounts | ForEach-Object {
$user = $_
$UserInfo = [ADSI]"LDAP://$($user.distinguishedName)"
# Extract values and convert to strings
$TSProfilePath = $UserInfo.TerminalServicesProfilePath.Value | ForEach-Object { [string]$_ }
$TSHomeDir = $UserInfo.TerminalServicesHomeDirectory.Value | ForEach-Object { [string]$_ }
$TSHomeDrive = $UserInfo.TerminalServicesHomeDrive.Value | ForEach-Object { [string]$_ }
# Add a new CSV row
$rows += [PSCustomObject]@{
User = $user.Name
TSProfilePath = $TSProfilePath
TSHomeDir = $TSHomeDir
TSHomeDrive = $TSHomeDrive
}
}
$rows | Export-Csv -Path "C:\doc.csv" -NoTypeInformation
And this is the error which I am receiving from the above code, for each line with $TS
at the beginning:
+ ~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvalidCastFromAnyTypeToString