0

我正在为 Azure PowerShell 3.1.0 编写一个 PowerShell 脚本:

  1. 从 JSON 获取内容
  2. 根据MigratedFlag ='Y'的条件循环内容并按升序排序OrderNo
  3. Location从使用执行 SQL 文件Invoke-Sqlcmd
  4. 如果我的值位置等于执行的脚本,则更新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-ObjectSelect-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'
}
4

1 回答 1

1

只需将它们插入管道之前Select-Object

$v_JSON = Get-Content '$(system.defaultworkingdirectory)\xxxxx\BuildOrder.json' -Raw | ConvertFrom-Json
$v_JSON | Sort-Object -Property OrderNo | Where-Object {$_.MigratedFlag -like 'Y'} | 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'
}

顺便说一句:你Select-Object -Property Location的用处不大,因为你必须在循环中再次迭代$V_JSON以获得其他属性。我推荐以下优化:

$v_JSON = Get-Content '$(system.defaultworkingdirectory)\xxxxx\BuildOrder.json' -Raw | ConvertFrom-Json
$v_JSON | Sort-Object -Property OrderNo | Where-Object {$_.MigratedFlag -like 'Y'} | ForEach {
    $Script =  $_.Location
    Write-Host "Executing Script: $Script"
    Invoke-Sqlcmd -ServerInstance "InstanceName" -Database $(database) -Username $(testauto_username)  -Password $(testauto_password) -InputFile $(system.defaultworkingdirectory)\$Script
    $_.MigratedFlag = 'N'
}
$v_JSON | ConvertTo-Json -depth 32| set-content '$(system.defaultworkingdirectory)\xxxxx\BuildOrder.json'
于 2020-07-19T11:41:47.720 回答