powershell Invoke-PowerBIRestMethod-URL:“字符串值未转换为循环值

rnmwe5a2  于 2022-11-10  发布在  Shell
关注(0)|答案(1)|浏览(110)

我正在遍历CSV文件以设置变量。当我尝试使用我的更改变量时,当尝试在Invoke-PowerBIRestMethod-URL$url中使用时,我得到的只是变量名而不是它的值。有人能帮助我解决我的问题吗?我想以编程方式更改几个服务器名称和数据库值。

$csv = import-csv "C:\Temp\MyTest.csv"

Import-Module MicrosoftPowerBIMgmt
Import-Module MicrosoftPowerBIMgmt.Profile

Connect-PowerBIServiceAccount 

$d = $_.DatasetId
$w = $_.WorkspaceId
$url = "groups/$w/datasets/$d/Default.UpdateDatasources"
$server =  $_.Server

$csv | foreach-object {
     Invoke-PowerBIRestMethod -Url $url -Method Post -Body '{
  "updateDetails": [
    {
      "datasourceSelector": {
        "datasourceType": "Sql",
        "connectionDetails": {
          "server": "sql1",
          "database": "db"
        }
      },
      "connectionDetails": {
        "server": "sql01",
        "database": "Database"
      }
    }
  ]
}'
}

Disconnect-PowerBIServiceAccount

数据协议:WorkspaceID、DatasetID、服务器
这对我也不起作用:

Import-Module MicrosoftPowerBIMgmt
Import-Module MicrosoftPowerBIMgmt.Profile

$url='groups/9e55d5cf-26ec-4016-bfed-000000000/datasets/ffa73aa7-223c-4f73-b084-000000000/Default.UpdateDatasources'
Connect-PowerBIServiceAccount #-Credential $credential
  Invoke-PowerBIRestMethod -Url $($url) -Method Post -Body '{
  "updateDetails": [

请帮帮忙。

py49o6xq

py49o6xq1#

您需要在Foreach循环中设置变量,并且需要使用here-字符串将$SERVER变量插入到请求正文中。艾格

$csv = import-csv "C:\Temp\MyTest.csv"

Import-Module MicrosoftPowerBIMgmt
Import-Module MicrosoftPowerBIMgmt.Profile

Connect-PowerBIServiceAccount 

$csv | foreach-object {

    $d = $_.DatasetId
    $w = $_.WorkspaceId
    $url = "groups/$w/datasets/$d/UpdateParameters"
    $server =  $_.Server

    $body = @"
{
    "updateDetails": [
        {
            "datasourceSelector": {
                "datasourceType": "Sql",
                "connectionDetails": {
                    "server": "$server",
                    "database": "db"
                }
            },
            "connectionDetails": {
                "server": "$server",
                "database": "Database"
            }
        }
    ]
}
"@
    #$body
    #$url

    Invoke-PowerBIRestMethod -Url $url -Method Post -Body $body
}

Disconnect-PowerBIServiceAccount

相关问题