我正在为 Azure PowerShell 3.1.0 编写一个 PowerShell 脚本:
- 从 JSON 获取内容
- 根据MigratedFlag ='Y'的条件循环内容并按升序排序
OrderNo
Location
从使用执行 SQL 文件Invoke-Sqlcmd
- 如果我的值位置等于执行的脚本,则更新MigratedFlag =N
我可以执行除第 2步之外的所有步骤,即按 OrderNo 排序并在 MigratedFlag 上添加条件,
下面是一段 JSON 内容:
[
{
"OrderNo": "1",
"Location": "ETS\\Stage_PS\\FS_PS_CUSTOMER.sql",
"MigratedFlag": "Y",
"Description": "Creation of STG_PS_FS.PS_CUSTOMER"
},
{
"OrderNo": "2",
"Location": "ETS\\Stage_PS\\FS_PS_CUST_ADDRESS.sql",
"MigratedFlag": "Y",
"Description": "Creation of STG_PS_FS.PS_CUST_ADDRESS"
},
{
"OrderNo": "3",
"Location": "ETS\\Stage_PS\\FS_PS_CUST_ADDR_SEQ.sql",
"MigratedFlag": "Y",
"Description": "Creation of STG_PS_FS.PS_CUST_ADDR_SEQ"
}
]
下面是我正在使用的 PS 脚本(无法使用Sort-Object
也不Where-Object
在Select-Object
):
$v_JSON = Get-Content '$(system.defaultworkingdirectory)\xxxxx\BuildOrder.json' -raw | ConvertFrom-Json
$v_JSON | Select-Object -Property Location | ForEach {
$Script = $_.Location
Write-Host "Executing Script"$Script
Invoke-Sqlcmd -ServerInstance "InstanceName" -Database $(database) -Username $(testauto_username) -Password $(testauto_password) -InputFile $(system.defaultworkingdirectory)\$Script
$v_JSON | % {if($_.Location -eq $Script){$_.MigratedFlag='N'}}
$v_JSON | ConvertTo-Json -depth 32| set-content '$(system.defaultworkingdirectory)\xxxxx\BuildOrder.json'
}