0

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 
  • 1
    This doesn't address your case, but it might get you started with copying AD data to CSV format. https://learn.microsoft.com/en-us/answers/questions/239938/export-ad-users-to-csv-file – Walter Mitty Jun 23 '23 at 17:10
  • 1
    Typically, you pipe toExport-Csv INSIDE the record-by-record generation loop. Not the way you did it. – Walter Mitty Jun 23 '23 at 17:12
  • 2
    @WalterMitty, for reasons of efficiency you do _not_ want to call `Export-Csv` inside a loop body, as it means making a separate call to it in each and every iteration. Instead, pipe all output to a single invocation at the end of the pipeline. Here that means: Don't use `$rows`, just output the `[pscustomobject]` instances directly, and follow the `ForEach-Object` call with `| Export-Csv -Path "C:\doc.csv" -NoTypeInformation`. As an aside: collecting outputs manually in an array (list) is rarely needed, and when it is, `+=` should be avoided - see https://stackoverflow.com/a/60708579/45375 – mklement0 Jun 23 '23 at 17:23
  • 1
    Your symptom is mysterious. You _may_ be able to bypass the problem if you use `$UserInfo.TerminalServicesProfilePath.Value.ToString()`, and so on. Which PowerShell version is this? – mklement0 Jun 23 '23 at 17:25
  • OOps. I goofed in my comment. What I should have said is that Export-Csv is typically called on the right side of a pipeline. The records are delivered one by one through the pipe, and assembled by Export-Csv. I don't know the efficiency implications however. – Walter Mitty Jun 23 '23 at 20:00
  • @mklement0 were you suggesting a format like the script which I will paste in a second comment below? If so, this is what the output CSV looks like, with no values at all captured for TSPRofilePath, TSHomeDir, TSHomeDrive, and I am not sure how best to format this differently: "User","TSProfilePath","TSHomeDir","TSHomeDrive" "Service Account 1",,, "User Account 1",,, – closedcasketfuneral Jun 26 '23 at 16:31
  • @mklement0 ```$OU = "OU= Accounts,DC=domain,DC=local" $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 $TSHomeDir = $UserInfo.TerminalServicesHomeDirectory.Value $TSHomeDrive = $UserInfo.TerminalServicesHomeDrive.Value``` – closedcasketfuneral Jun 26 '23 at 16:31
  • ```# Add a new CSV row [PSCustomObject]@{ User = $user.Name TSProfilePath = $TSProfilePath TSHomeDir = $TSHomeDir TSHomeDrive = $TSHomeDrive } } | Export-Csv -Path "C:\Results.csv" -NoTypeInformation``` – closedcasketfuneral Jun 26 '23 at 16:32
  • @closedcasketfuneral, yes, that's the fundamental structure I had in mind. Did you try `...Value.ToString()` instead of just `...Value`? – mklement0 Jun 26 '23 at 21:38

0 Answers0