【问题标题】:Powershell - Import seperate array values to columnsPowershell - 将单独的数组值导入列
【发布时间】:2021-02-28 17:06:27
【问题描述】:

我是 Powershell 的新手,我正在尝试从 Teams 中提取数据(使用 Graph API)我已成功将所需数据导出到 SQL 表中(使用 Invoke-Sqlcmd 函数)

提取通话中涉及的用户时,我得到了 CallID,但参与者值放在单个字段中

CallID                               Participants
CallGUID-xxxx-xxxx-xxxx-xxxxxxxxxx   John Smith Mary Brown Billy Dee Williams

我需要像下面这样格式化它

CallID                               Participants
CallGUID-xxxx-xxxx-xxxx-xxxxxxxxxx   John Smith
CallGUID-xxxx-xxxx-xxxx-xxxxxxxxxx   Mary Brown
CallGUID-xxxx-xxxx-xxxx-xxxxxxxxxx   Billy Dee Williams

我使用的代码如下。

    $clientId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
    $clientSecret = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
    $tenantName = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
    $tenantName = "xxxxxxxx.onmicrosoft.com"
    $resource = "https://graph.microsoft.com/"
    
    $tokenBody = @{  
        Grant_Type    = "client_credentials"  
        Scope         = "https://graph.microsoft.com/.default"  
        Client_Id     = $clientId  
        Client_Secret = $clientSecret  
    }
    
    $URL = "https://graph.microsoft.com/v1.0/communications/callRecords/CallGUID-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
    
    $tokenResponse = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$TenantName/oauth2/v2.0/token" -Method POST -Body $tokenBody
    $response = Invoke-RestMethod -Headers @{Authorization = "Bearer $($tokenResponse.access_token)"} -Uri $URL
    
    $CallDetailsID = $response.id
    $CallDetailsStart = $response.startDateTime
    $CallDetailsEnd = $response.endDateTime
    $CallDetailsParticipants = $response.Participants.user.displayname
    
    Invoke-Sqlcmd -Query "INSERT INTO [Alpha].[dbo].[Table] (CallID, Participants) SELECT '$CallGUID',  $CallDetailsParticipants)" -ServerInstance "Server\Instance"

有人能帮忙指出我做错了什么吗?

【问题讨论】:

    标签: sql sql-server powershell import


    【解决方案1】:

    看起来您需要遍历每个参与者以单独插入它们。

    试试

    $qry = "INSERT INTO [Alpha].[dbo].[Table] (CallID, Participants) SELECT '$CallGUID', '{0}'"
    foreach ($participant in $CallDetailsParticipants) {
        Invoke-Sqlcmd -Query ($qry -f $participant) -ServerInstance "Server\Instance"
    }  
    

    (测试后由OP编辑)

    【讨论】:

    • 嗨 Theo,我在第 13 行 Invoke-Sqlcmd 上遇到错误:使用密钥“故障转移伙伴”需要存在密钥“初始目录”。在 line:56 char:13 + Invoke-Sqlcmd -Query $qry -f $participant -ServerInstance ... + ~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidResult: (Server\Instance:PSObject) [Invoke-Sqlcmd], ArgumentException + FullyQualifiedErrorId : ExecutionFailed,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand
    • ALSO 这个错误在 line:56 char:42 + Invoke-Sqlcmd -Query $qry -f $participant -ServerInstance ... + ~~~~~~~~~~~~ + CategoryInfo :无效数据:(:) [Invoke-Sqlcmd],ParameterBindingValidationException + FullyQualifiedErrorId:ParameterArgumentValidationError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand Invoke-Sqlcmd:无法验证参数“FailoverPartner”上的参数。参数为 null 或空。提供一个不为 null 或空的参数,然后重试该命令。
    • @Rob 抱歉,我在发布此内容时出现了复制粘贴错误。现在应该修复了。
    • 在你的帮助下我已经搞定了。我遇到了另一个错误,我通过删除额外的右括号并在 {0} 周围加上引号来修复它 我的 $qry 行看起来像这样 $qry = "INSERT INTO [Alpha].[dbo].[Table] (CallID, Participants ) SELECT '$CallDetailsID', '{0}'" foreach ($participant in $CallDetailsParticipants) { Invoke-Sqlcmd -Query ($qry -f $participant) -ServerInstance "Server\Instance" } 希望你不要介意,但我更新了您的帖子以供其他人/我将来参考非常感谢您的帮助!
    • @Rob 干得好!我一点也不介意。如前所述,我自己无法对此进行测试,所以我很高兴你这样做了。
    猜你喜欢
    • 2023-03-09
    • 1970-01-01
    • 2015-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-17
    • 2020-11-03
    • 1970-01-01
    相关资源
    最近更新 更多