Arrays 如何在PowerShell中将阵列添加到阵列或解压缩动态生成的阵列?

Arrays 如何在PowerShell中将阵列添加到阵列或解压缩动态生成的阵列?,arrays,sql-server,powershell,outlook,Arrays,Sql Server,Powershell,Outlook,我的函数会为收件箱中的每封电子邮件生成一个数组。我想将它们添加到列表中进行解包,或者动态解包以在SQL查询中使用值作为字符串 我已经编写了我的函数,并验证了它是否像我预期的那样返回数组,并对|的ForEach对象、ForEach等进行了实验 由于在PowerShell中,功能是: Function Get-OutlookInBox { Add-type -assembly “Microsoft.Office.Interop.Outlook” | out-null $olFolde

我的函数会为收件箱中的每封电子邮件生成一个数组。我想将它们添加到列表中进行解包,或者动态解包以在SQL查询中使用值作为字符串

我已经编写了我的函数,并验证了它是否像我预期的那样返回数组,并对|的ForEach对象、ForEach等进行了实验

由于在PowerShell中,功能是:

Function Get-OutlookInBox
{
    Add-type -assembly “Microsoft.Office.Interop.Outlook” | out-null
    $olFolders = “Microsoft.Office.Interop.Outlook.olDefaultFolders” -as [type]
    $outlook = new-object -comobject outlook.application
    $namespace = $outlook.GetNameSpace(“MAPI”)
    $folder = $namespace.getDefaultFolder($olFolders::olFolderInBox)
    #Is an array now I just need to break down this array into constituent parts so i can DB these hoes.
    return $message = $folder.items | Select-Object -Property SenderName, Subject, SentOn, ReceivedTime, BCC.
}
此函数返回一组数组,这些数组表示电子邮件及其属性,我希望:

  • 立即解包,以便我可以将它们放入数据库
  • 打包到一个数组中并添加到数组/列表中,以传递给一个函数,该函数将解包并检查/插入到数据库中
  • 问题是我似乎不知道如何将我的函数吐出的数组放入数组中,以便在数组吐出时解包或解包,以便将它们插入表中

    如果我想用pythonical来做这个,它看起来会像:

    listOfEmailes = list()
    for x in GetOutlookEmails():
        listOfEmails.appened(x)
    
    for email in listOfEmails:
        sender =  emails[0]
        Sent = emails[1]
        received = emails[2]
        BCCs = emails[3]
    # Craft SQL string here with substitution
    sqlquery = blah
    # Execute crafted SQL query here.
    dosql(sqlquery)
    
    @{ @{array1}, @{array2}, @{array3}, etc...}
    
    我的想法是如果我要做这样的事情

    $EmailProperties = @{}
    $Inbox = Get-OutLookInbox
    
    ForEach ($message in $Inbox)
    {
         $EmailProperties.add($Message)
    }
    
    生成的数组看起来像:

    listOfEmailes = list()
    for x in GetOutlookEmails():
        listOfEmails.appened(x)
    
    for email in listOfEmails:
        sender =  emails[0]
        Sent = emails[1]
        received = emails[2]
        BCCs = emails[3]
    # Craft SQL string here with substitution
    sqlquery = blah
    # Execute crafted SQL query here.
    dosql(sqlquery)
    
    @{ @{array1}, @{array2}, @{array3}, etc...}
    

    <>我做错了什么?

    如果你要使用大型数组,考虑使用<代码> Stasy.Cuff.Gn泛滥.List。添加到普通数组会产生复制数组的副作用,这会导致不必要的内存使用。使用您的pseudocode作为道具(此处):

    如果您更喜欢
    PSCustomObject
    而不是哈希表,请将列表类型更改为
    PSCustomObject
    ,并通过使用
    更改第一行将
    hashtable
    强制转换为一。添加上面的
    调用:

    $EmailProperties.add([PSCustomObject]@{
    
    也就是说,根据你的问题,不清楚你想在这里做什么。您已经返回了一个数组,您实际上是在尝试将哈希表添加到数组中吗?如果我不在基地,请澄清你的问题

    编辑:假设您想要电子邮件对象的属性的
    哈希表,我对此做了一些修改


    < >编辑2:添加了如何将此工作作为<代码> pSuppObjult</代码>的数组。如果需要使用大型数组,请考虑使用<代码> Stasy.Cyto.Gual.List< <代码>。添加到普通数组会产生复制数组的副作用,这会导致不必要的内存使用。使用您的pseudocode作为道具(此处):

    如果您更喜欢
    PSCustomObject
    而不是哈希表,请将列表类型更改为
    PSCustomObject
    ,并通过使用
    更改第一行将
    hashtable
    强制转换为一。添加上面的
    调用:

    $EmailProperties.add([PSCustomObject]@{
    
    也就是说,根据你的问题,不清楚你想在这里做什么。您已经返回了一个数组,您实际上是在尝试将哈希表添加到数组中吗?如果我不在基地,请澄清你的问题

    编辑:假设您想要电子邮件对象的属性的
    哈希表,我对此做了一些修改


    编辑2:添加了如何根据需要将此操作作为包含电子邮件的选定属性的
    PSCustomObject
    数组。

    您的
    Get OutlookInBox
    函数输出自定义对象(
    [PSCustomObject]
    实例),这就是
    选择对象
    输出的内容

    您可以
    [pscustomobject]
    实例视为“属性包”
    类似于哈希表,但不同于哈希表;它们的行为与其他对象类似,您可以通过名称直接访问这些对象的属性

    通过将结果分配给
    $Inbox
    ,后者将包含一个数组
    [object[]]
    ,如果输出两个或多个对象,则包含此类
    [pscustomobject]
    实例,否则将包含一个
    [pscustomobject]
    实例。
    通过键入将接收变量约束为
    [array]
    ,可以确保始终获得数组:

    # Cast [array] ensures that $Inbox is always an array, even with just 1 output object.
    [array] $InboxMessages = Get-OutlookInBox
    
    然后可以迭代自定义对象并访问其属性:

    foreach ($message in $InboxMessages) { # loop over all messages
      $sender = $message.Sender    # access the .Sender property
      # ...
    }
    
    当然,您可以省去中间人,使用管道直接迭代您的消息:

    Get-OutlookInBox | ForEach-Object { 
      $sender = $_.Sender    # access the .Sender property; $_ is the input object at hand.
      # ...
    }
    

    您的
    Get OutlookInBox
    函数输出自定义对象(
    [pscustomobject]
    实例),其中包含电子邮件的选定属性,这就是
    select Object
    输出的内容

    您可以
    [pscustomobject]
    实例视为“属性包”
    类似于哈希表,但不同于哈希表;它们的行为与其他对象类似,您可以通过名称直接访问这些对象的属性

    通过将结果分配给
    $Inbox
    ,后者将包含一个数组
    [object[]]
    ,如果输出两个或多个对象,则包含此类
    [pscustomobject]
    实例,否则将包含一个
    [pscustomobject]
    实例。
    通过键入将接收变量约束为
    [array]
    ,可以确保始终获得数组:

    # Cast [array] ensures that $Inbox is always an array, even with just 1 output object.
    [array] $InboxMessages = Get-OutlookInBox
    
    然后可以迭代自定义对象并访问其属性:

    foreach ($message in $InboxMessages) { # loop over all messages
      $sender = $message.Sender    # access the .Sender property
      # ...
    }
    
    当然,您可以省去中间人,使用管道直接迭代您的消息:

    Get-OutlookInBox | ForEach-Object { 
      $sender = $_.Sender    # access the .Sender property; $_ is the input object at hand.
      # ...
    }
    

    您的代码看起来应该返回单个消息,而不是消息数组。
    $Inbox
    变量将包含一个MSG数组,因为它捕获了函数中的所有单个MSG。@mklement0您可以将
    [mcve]
    放入注释中,注释将自动扩展为。谢谢,@AnsgarWiechers,这很方便。我碰巧有一个自定义的
    mcve
    “hotstring”,它扩展到了我在评论中使用的内容,我认为显示首字母缩写“mcve”也没什么坏处,因为读者可能会反复遇到它。给其他读者:显示所有支持的可用于评论的所谓速记链接(
    […]
    )。@Lee_Dailey,就是这样。我一直在想,我的代码应该将电子邮件属性数组添加到数组中并返回它,但它没有