powershell将创建xml以及父元素和子元素(如果它们不存在)

powershell将创建xml以及父元素和子元素(如果它们不存在),xml,powershell,powershell-2.0,powershell-3.0,powershell-4.0,Xml,Powershell,Powershell 2.0,Powershell 3.0,Powershell 4.0,我想知道是否有人能帮忙。 PowerShell从Exchange邮箱信息创建XML 据我所知,我们需要审核Exchange环境中每个邮箱的访问权限。 我已经成功地抓取了每个邮箱并添加到XML中,我的问题是剩下的! 对于每个邮箱,我需要执行以下操作 获取DisplayName、PrimaryStp、别名 检查邮箱是否已经在XML文件中,如果没有,请添加 检查OWA是否已启用 检查邮箱部分中的OWA是否已经在XML文件中,如果没有,请添加 获取月份的当前日期并将条目添加到OWA 检查IMAP是否已

我想知道是否有人能帮忙。 PowerShell从Exchange邮箱信息创建XML

据我所知,我们需要审核Exchange环境中每个邮箱的访问权限。 我已经成功地抓取了每个邮箱并添加到XML中,我的问题是剩下的! 对于每个邮箱,我需要执行以下操作

  • 获取DisplayName、PrimaryStp、别名
  • 检查邮箱是否已经在XML文件中,如果没有,请添加
  • 检查OWA是否已启用
  • 检查邮箱部分中的OWA是否已经在XML文件中,如果没有,请添加
  • 获取月份的当前日期并将条目添加到OWA
  • 检查IMAP是否已启用
  • 检查邮箱部分中的IMAP是否已经在XML文件中,如果没有,请添加
  • 获取月份的当前日期并将条目添加到IMAP
  • 检查POP是否已启用
  • 检查邮箱部分中的POP是否已经在XML文件中,如果没有,请添加
  • 获取月份的当前日期并将条目添加到IMAP
  • 检查是否已启用MAPI
  • 检查邮箱部分中的MAPI是否已在XML文件中,如果没有,请添加
  • 获取月份的当前日期并将条目添加到IMAP
  • 完成后,保存XML
我不需要Exchange方面的帮助,只需要按照上面的列表创建元素(如果它们不存在的话)
XML文件将在下个月的第一天被销毁。
之所以采用这种格式,是因为另一个部门提出了额外的要求,该部门将使用xml并绘制当月天数的图表。 希望有人能帮忙

谢谢

xml的副本应该如下所示

<root>
    <Mailbox DisplayName="Jerry The Mouse" PrimarySMTP="jerrythemouse@domain.com" Alias="jerrythemouse">
        <Access ServiceName="OWA">
            <Report Day="01" Date="01/10/2016" Enabled="true" />
            <Report Day="02" Date="02/10/2016" Enabled="true" />
            <Report Day="03" Date="03/10/2016" Enabled="false" />
        </Access>
        <Access ServiceName="IMAP">
            <Report Day="01" Date="01/10/2016" Enabled="false" />
            <Report Day="02" Date="02/10/2016" Enabled="false" />
            <Report Day="03" Date="03/10/2016" Enabled="false" />
        </Access>
        <Access ServiceName="POP">
            <Report Day="01" Date="01/10/2016" Enabled="true" />
            <Report Day="02" Date="02/10/2016" Enabled="true" />
            <Report Day="03" Date="03/10/2016" Enabled="true" />
        </Access>
        <Access ServiceName="MAPI">
            <Report Day="01" Date="01/10/2016" Enabled="true" />
            <Report Day="02" Date="02/10/2016" Enabled="true" />
            <Report Day="03" Date="03/10/2016" Enabled="true" />
        </Access>
    </Mailbox>
    <Mailbox DisplayName="Tom The Cat" PrimarySMTP="tomthecat@domain.com" Alias="tomthecat">
        <Access ServiceName="OWA">
            <Report Day="01" Date="01/10/2016" Enabled="true" />
            <Report Day="02" Date="02/10/2016" Enabled="true" />
            <Report Day="03" Date="03/10/2016" Enabled="false" />
        </Access>
        <Access ServiceName="IMAP">
            <Report Day="01" Date="01/10/2016" Enabled="false" />
            <Report Day="02" Date="02/10/2016" Enabled="false" />
            <Report Day="03" Date="03/10/2016" Enabled="false" />
        </Access>
        <Access ServiceName="POP">
            <Report Day="01" Date="01/10/2016" Enabled="true" />
            <Report Day="02" Date="02/10/2016" Enabled="true" />
            <Report Day="03" Date="03/10/2016" Enabled="true" />
        </Access>
        <Access ServiceName="MAPI">
            <Report Day="01" Date="01/10/2016" Enabled="true" />
            <Report Day="02" Date="02/10/2016" Enabled="true" />
            <Report Day="03" Date="03/10/2016" Enabled="true" />
        </Access>
    </Mailbox>
    ...
</root>

...

您可以使用XPath查询检查节点是否存在:

$xml = [xml](Get-Content $filename)

$xml.SelectNodes("//root/Mailbox[@DisplayName='Jerry The Mouse']").Count
1

$xml.SelectNodes("//root/Mailbox[@DisplayName='Jerry The Mouse']/Access[@ServiceName='OWA']").Count
1
也就是说,如果该值为非零,则该节点至少存在一次

添加新元素更费力:

# Adding a new mailbox
$newMbx = $xml.CreateElement('Mailbox')

$attDisplayName = $xml.CreateAttribute('DisplayName')
$attPrimarySmtp = $xml.CreateAttribute('PrimarySmtp')
# etc.

$attDisplayName.Value = 'Spike The Dog'
$attPrimarySmtp.Value = 'SpikeTheDog@domain.com'
# etc.

$newMbx.Attributes.Append($attDisplayName)
$newMbx.Attributes.Append($attPrimarySmtp)
# etc.

$newSvc = $xml.CreateElement('Access')
$attServiceName = $xml.CreateAttribute('ServiceName')
$attServiceName.Value = 'OWA'
$newSvc.Attributes.Append($attServiceName)

# Add this back into the XML document:
$xml.LastChild.Append($newMbx)
在邮箱/访问元素中添加新元素:

# Create a new $record XML element using the technique above

# Add this new element to an existing XML element:

$xml.SelectNodes("//root/Mailbox[@DisplayName='Spike The Dog']/Access[@ServiceName='OWA']").AppendChild($record)

谢谢你的回复。我要到星期一/星期二才能看到这个,因为我要去微软FutureDecoded!询问Steven Hawking是否可以使用XPATH查询XML数据!哈哈!我会的,但我不会在第一天去那里!