Search code examples
powershellpowershell-3.0

Converting Object to string in Powershell


Can anyone provide some help please. I have JSON script where my datapath and file names are stored. However, in Powershell, I am able to get the path in the string format but the filename is in array. I am expecting the output to be, for eample C:\Dep\reports\P1.sql C:\Dep\reports\P2.sql C:\Dep\reports\P3.sql

enter image description here

cls

$ReportConfiguration = Get-Content -path  "C:\Users\asingh\Documents\PowerShell Scripts\JSON\stackOverflow.json" | ConvertFrom-JSon

$ReportFolders = @($ReportConfiguration.ReportScripts | Get-Member -Type NoteProperty).Name
$reportPath = $ReportConfiguration.ReportScripts.ReportFiles.Location


foreach($folder in $ReportFolders)
{
    #Display folder name
    Write-Host  $folder -ForegroundColor Green
    
    #
    $subReportsFolders = @($ReportConfiguration.ReportScripts.$folder | Get-Member -Type NoteProperty).Name
    write-host "No. of subFolders = " $subReportsFolders.Count
    Write-Host "subfolder:  "  $subReportsFolders 
    #
    
    for ($ReportCount = 0 ; $ReportCount -le $QueryCount.Count ; $ReportCount++)
    {      
          $ReportPath + $folder + $ReportConfiguration.ReportScripts.$folder[$ReportCount]        
    }

    Write-Host(" `n" )
}

{
    "ReportScripts": 
    {
        "ReportFiles":
        {
            "Location": "C:\\Dep\\Test\\"
        },
        
        "Test1":
        {
            "Schema_File" : "S1.sql",
            "Proc_File": "P1.sql"
        },
                
        "Test2":
        {
            "Schema_File" : "S2.sql",
            "Proc_File": "P2.sql"
        }
    }
}

Solution

  • ##clear host 
    cls
    $ReportConfiguration = Get-Content -path  "C:\Users\asingh\Documents\PowerShell Scripts\JSON\stackOverflow.json" | ConvertFrom-JSon
    
    $ReportFolders = @($ReportConfiguration.ReportScripts | Get-Member -Type NoteProperty).Name
    $reportPath = $ReportConfiguration.ReportScripts.ReportFiles.Location
    
    
    foreach($folder in $ReportFolders)
    {
        #Display folder name
        Write-Host  $folder -ForegroundColor Green
        
        #
        $subReportsFolders = @($ReportConfiguration.ReportScripts.$folder | Get-Member -Type NoteProperty).Name
        write-host "No. of subFolders = " $subReportsFolders.Count
        Write-Host "subfolder:  "  $subReportsFolders 
        #
        
        for ($ReportCount = 0 ; $ReportCount -le $QueryCount.Count ; $ReportCount++)
        {      
              $ReportPath + $folder + $ReportConfiguration.ReportScripts.$folder[$ReportCount]        
        }
    
    # Here is the fix
    "C:\test\" + $ReportConfiguration.ReportScripts.$folder.Proc_File
    "C:\test\" + $ReportConfiguration.ReportScripts.$folder.Schema_File
    
    Write-Host(" `n" )
    }