如何在 PowerShell 脚本中使用 JSON 中的变量。

huangapple go评论115阅读模式
英文:

How to use Variables in JSON in PS script

问题

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
英文:

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 :

{
	"$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`

答案1

得分: 1

...
$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)
英文:

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)

huangapple
  • 本文由 发表于 2023年7月20日 14:13:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76727120.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定