【问题标题】:Compare objects and export the difference比较对象并导出差异
【发布时间】:2018-12-06 16:08:54
【问题描述】:

我想制作一个比较 O365 租户设置的脚本。阅读它们很好,现在我想做一些不同的对象。 一个相关的问题是here,但没有答案。 Powershell Compare-Object and getting the Differences into a File

我已经从两个租户创建了一个 json 文件,如下所示:

$srcTenant | Select-Object -Property * | ConvertTo-Json | Out-File "$targetfolder\$targetfile"

现在,我想要一个仅包含使用以下脚本收集的属性的文件:

到目前为止:

$properties = ($srcTenant | Get-Member -MemberType Property | Select-Object -ExpandProperty Name)
$selectedproperties = @{}
$i = 0
foreach ($property in $properties) {
  if (Compare-Object $srcTenant $trgTenant -Property "$property") {
  $selectedproperties.Add($i, "$property")
  $i++
  }
}

$selectedproperties 变量包含 9 个属性,我只想以与其他两个相同的格式导出这 9 个。

名称值
---- -----
8 StorageQuotaAllocated
7 存储配额
6 ResourceQuotaAllocated
5 资源配额
4 所有者匿名通知
3 OneDriveStorageQuota
2 默认链接权限
1 条件访问策略
0 AllowDownloadingNonWebViewableFiles

所以,我正在寻找类似的东西:

$srcTenant | Select-Object -Property (that 9 property above) | ConvertTo-Json | Out-File "$targetfolder\$targetfile

也欢迎其他实现相同结果的选项:)

【问题讨论】:

    标签: powershell select-object


    【解决方案1】:

    Select-Object -Property 可以采用属性名称数组。

    first example here

    【讨论】:

    • 如何将属性作为数组传递?我对这方面的 PS 不是很熟悉。
    • @vilmarci - 类似select-object -Property $selectedproperties.values 的东西适用于这种情况。 This page 是对 powershell 数组的一个很好的介绍。
    • 谢谢,$selectedproperties.values 不起作用,但解决方案是使用数组而不是哈希表。我切换到那个,现在它可以工作了:)
    猜你喜欢
    • 2011-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-19
    • 1970-01-01
    • 1970-01-01
    • 2019-02-01
    相关资源
    最近更新 更多