Export-CSV with the path as a variable

  • samirca007


    Points: 410

    Good morning all

    I'm looking to export the result of the execution of a stored procedure to a csv file

    except that I can't do an export-csv with a path created from a variable

    who can help me please

    $path='C:\TSQL\EDI\EDI_AOG\EDI SANTE\CDC Santé EQUITE\Resultat'
    get-childitem -path $path -include *.csv -recurse | remove-item
    Invoke-Sqlcmd -ServerInstance "$serverinstance" -database "Gestion_BO_Stats" -Query "exec SP_EQUITE_SANTE_CDC_Contrats" |export-csv -path $path + "\SP_EQUITE_SANTE_CDC_Contrats_$((Get-Date).ToString('MM-dd-yyyy_hh-mm-ss')).csv"
    Invoke-Sqlcmd -ServerInstance "$serverinstance" -database "Gestion_BO_Stats" -Query "exec SP_EQUITE_SANTE_CDC_Bénéficiaires" |export-csv -path $path + "\SP_EQUITE_SANTE_CDC_Bénéficiaires_$((Get-Date).ToString('MM-dd-yyyy_hh-mm-ss')).csv"
    Invoke-Sqlcmd -ServerInstance "$serverinstance" -database "Gestion_BO_Stats" -Query "exec SP_EQUITE_SANTE_CDC_Commisisons" |export-csv -path $path + "\SP_EQUITE_SANTE_CDC_Commisisons_$((Get-Date).ToString('MM-dd-yyyy_hh-mm-ss')).csv"
    Invoke-Sqlcmd -ServerInstance "$serverinstance" -database "Gestion_BO_Stats" -Query "exec SP_EQUITE_SANTE_CDC_Encaissements" |export-csv -path $path + "\SP_EQUITE_SANTE_CDC_Encaissements_$((Get-Date).ToString('MM-dd-yyyy_hh-mm-ss')).csv"


    You must be logged in to view attached files.
  • Jo Pattyn


    Points: 31376

    You can try

    Invoke-Sqlcmd -ServerInstance "$serverinstance" -database "Gestion_BO_Stats" -Query "exec SP_EQUITE_SANTE_CDC_Contrats" |export-csv -path "$path\SP_EQUITE_SANTE_CDC_Contrats_$((Get-Date).ToString('MM-dd-yyyy_hh-mm-ss')).csv"

    as powershell variables are expanded when used between double qoutes

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply