Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use Variables in JSON in PS script

i wanted to create script to send email with list of new features ( releaselist) using PS script with setting json to Declare recipiants list etc. But how can i use Variables in this json file?

PS script i´m using

#emailsendscript
$SettingsObject = Get-Content -Path C:\Temp\Settings.JSON | ConvertFrom-Json
$EmailFrom = $SettingsObject.'$EmailFrom'
$EmailTo = $SettingsObject.'$EmailTo'
$Subject = $SettingsObject.'$Subject'
$Body = $SettingsObject.'$Body'
$SMTPServer = “smtp.office365.com”
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true 
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($EmailFrom, “*****”);
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)

json file i´m using :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
    "$TaskName": "Release Verze 34",
    "$TaskTime": "16:52",
    "$ReleasedFeatures": "CDR7494 BC  storno polozek DM a doplneni kodu partnera transakce \n CDR7314 BC automaticke generovani nakupnich faktur ze smluv \n CDR7509 nelze tisknout prodejni faktury ktere nemaji vyplnene centrum odpovednosti chyba v priloze \n CDR7514 BC  MO  Vychozi hodnota prijmout u moznosti uctovani nakupnich objednavek \n CDR7215 BC  objednavky BC doplneni zustatku \n CDR7444 BC  Prodejni faktury generovane ze smluv  popise",
    "$EmailFrom": "***@*.*",
    "$EmailTo": "***@*.*",
    "$Subject": "Release do vyvojoveho prostredi",
    "$SubjectFinish": "Release do vyvojoveho prostredi - hotovo",
    "$Body": "Dobry den, \n Dnes od $TaskTime probehne release do Vyvojoveho prostredi. Nasazeny budou upravy : $ReleasedFeatures",
    "$BodyFinish": "Dobry den, \n Release verze **$AppVersionNo** probehl uspesne. \n Jakub Soucek",
    "$DeploymentInstance": "SSC175"
}

Email i recieve as result :

Dobry den, 
 Dnes od $TaskTime probehne release do Vyvojoveho prostredi. Nasazeny budou upravy : $ReleasedFeatures

Result i expect/want :

Dobry den, 
 Dnes od 16:52 probehne release do Vyvojoveho prostredi. Nasazeny budou upravy : 

CDR7494 BC  storno polozek DM a doplneni kodu partnera transakce 
CDR7314 BC automaticke generovani nakupnich faktur ze smluv
CDR7509 nelze tisknout prodejni faktury ktere nemaji vyplnene centrum odpovednosti chyba v priloze CDR7514 BC  MO  Vychozi hodnota prijmout u moznosti uctovani nakupnich objednavek
CDR7215 BC  objednavky BC doplneni zustatku 
CDR7444 BC  Prodejni faktury generovane ze smluv  popise`

>Solution :

You would have to replace the literal variables with the actual values, so:

...
$TaskTime=$SettingsObject.'$TaskTime'
$ReleasedFeatures=$SettingsObject.'$ReleasedFeatures'
$Body=$SettingsObject.'$Body'
$Body=$Body.replace('$TaskTime',$TaskTime)
$Body=$Body.replace('$ReleasedFeatures',$ReleasedFeatures)
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading