locked
Cannot convert the "System.Object[]" value of type "Object[]" to type "string". RRS feed

  • Question

  • Good Morning,

    I have a Powershell object that I need to attached as the body of an email but to do this it needs to be converted to a string.  I'e tried out-string and convert-string but these didnt seem to work and I get the error 

     Cannot convert the "System.Object[]" value of type "Object[]" to type "string".

    My object is this:

    $pstobj = @() and I'm looping through and adding variables like this

    foreach($dest in $Pathrray){

    $pathfound = get-childitem -Path $dest -filter $type -recurse -ErrorAction silentlycontinue

    foreach ($pst in $pathfound){
    if ($pst){
    $pstpath = $pst.fullname 


    $pstobj += $hostname
    $pstobj += $Username
    $pstobj += $pstpath

    How do I convert $pstobj to a string??


    Alter De Ruine

    Saturday, February 10, 2018 9:50 AM

All replies

  • FOr arrays of all types:

    PS > [array]$a = 'joe','john','mary','nuper'
    PS > "$a"
    joejohn mary nuper
    PS >

    Or:

    PS > $a -join '><'
    joe><john><mary><nuper

    It is best to start here: Learn PowerShell  


    \_(ツ)_/



    Saturday, February 10, 2018 10:14 AM
  • You will also learn this if you do the tutorial.

    PS>[array]$a = 'joe','john','mary','nuper'
    PS>'First:{0} Second:{1}  Third:{2} Last:{3}' -f $a
    First:joe Second:john  Third:mary Last:nuper
    PS>
    Write-Host $a -green -Fore green


    \_(ツ)_/


    • Edited by jrv Saturday, February 10, 2018 10:19 AM
    Saturday, February 10, 2018 10:18 AM
  • Send him the xml version that he can import?

    [pscustomobject]@{name='Joe'} | Export-Clixml obj.xml
    get-content obj.xml                                                               
    
    <Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
      <Obj RefId="0">
        <TN RefId="0">
          <T>System.Management.Automation.PSCustomObject</T>
          <T>System.Object</T>
        </TN>
        <MS>
          <S N="name">Joe</S>
        </MS>
      </Obj>
    </Objs>
    


    Saturday, February 10, 2018 3:43 PM