【问题标题】:Merge multiple configuration files using powershell使用 powershell 合并多个配置文件
【发布时间】:2022-10-07 21:21:51
【问题描述】:

我的 Powershell 脚本接收多个 Microsoft Office365 DSC 策略配置策略文件作为文件夹中的输入,如下所示:

文件1.ps1

Configuration EXOSharingPolicy {
    param (
        [parameter()]
        [System.Management.Automation.PSCredential]
        $GlobalAdmin
    )

    if ($null -eq $GlobalAdmin) {
        <# Credentials #>
        $GlobalAdmin = Get-Credential -Message "Credentials"

    }
    else {
        $Credential = $GlobalAdmin
    }

    $OrganizationName = $Credential.UserName.Split('@')[1]
    Import-DscResource -ModuleName 'Microsoft365DSC' -ModuleVersion '1.22.907.1'

    Node localhost
    {
        EXOSharingPolicy 8b39ae5a-f4ed-4bdb-932d-fbb9397f7fc6
        {
            Credential           = $Credential;
            Default              = $True;
            Domains              = @("Anonymous:CalendarSharingFreeBusyReviewer");
            Enabled              = $True;
            Ensure               = "Present";
            Name                 = "Default Sharing Policy";
        }
    }
}

文件2.ps1

Configuration AADTenantDetails {
    param (
        [parameter()]
        [System.Management.Automation.PSCredential]
        $GlobalAdmin
    )

    if ($null -eq $GlobalAdmin) {
        <# Credentials #>
        $GlobalAdmin = Get-Credential -Message "Credentials"

    }
    else {
        $Credential = $GlobalAdmin
    }

    $OrganizationName = $Credential.UserName.Split('@')[1]
    Import-DscResource -ModuleName 'Microsoft365DSC' -ModuleVersion '1.22.907.1'

    Node localhost
    {
        AADTenantDetails 5cfcabd5-9c82-4bed-9934-09e1cf20c71b
        {
            Credential                           = $Credential;
            IsSingleInstance                     = "Yes";
            MarketingNotificationEmails          = @();
            SecurityComplianceNotificationMails  = @();
            SecurityComplianceNotificationPhones = @();
            TechnicalNotificationMails           = @("admin@tech.net.au");
        }
    }
}

文件3.ps1

Configuration EXOEmailAddressPolicy {
    param (
        [parameter()]
        [System.Management.Automation.PSCredential]
        $GlobalAdmin
    )

    if ($null -eq $GlobalAdmin) {
        <# Credentials #>
        $GlobalAdmin = Get-Credential -Message "Credentials"

    }
    else {
        $Credential = $GlobalAdmin
    }

    $OrganizationName = $Credential.UserName.Split('@')[1]
    Import-DscResource -ModuleName 'Microsoft365DSC' -ModuleVersion '1.22.907.1'

    Node localhost
    {
        EXOEmailAddressPolicy a2188f3f-80d5-419c-b229-063fc2c18dbf
        {
            Credential                        = $Credential;
            EnabledEmailAddressTemplates      = @("SMTP:@$OrganizationName");
            EnabledPrimarySMTPAddressTemplate = "@$OrganizationName";
            Ensure                            = "Present";
            ManagedByFilter                   = "";
            Name                              = "Default Policy";
            Priority                          = "Lowest";
        }
    }
}

我有几个这样的配置文件。 powershell 中是否有一种方法可以组合/合并这些文件,所以我最终得到一个具有所有配置的文件,如下所示。

Configuration CombinedPolicy {
    param (
        [parameter()]
        [System.Management.Automation.PSCredential]
        $GlobalAdmin
    )

    if ($null -eq $GlobalAdmin) {
        <# Credentials #>
        $GlobalAdmin = Get-Credential -Message "Credentials"

    }
    else {
        $Credential = $GlobalAdmin
    }

    $OrganizationName = $Credential.UserName.Split('@')[1]
    Import-DscResource -ModuleName 'Microsoft365DSC' -ModuleVersion '1.22.907.1'

    Node localhost
    {
        EXOSharingPolicy 8b39ae5a-f4ed-4bdb-932d-fbb9397f7fc6
        {
            Credential           = $Credential;
            Default              = $True;
            Domains              = @("Anonymous:CalendarSharingFreeBusyReviewer");
            Enabled              = $True;
            Ensure               = "Present";
            Name                 = "Default Sharing Policy";
        }
        AADTenantDetails 5cfcabd5-9c82-4bed-9934-09e1cf20c71b
        {
            Credential                           = $Credential;
            IsSingleInstance                     = "Yes";
            MarketingNotificationEmails          = @();
            SecurityComplianceNotificationMails  = @();
            SecurityComplianceNotificationPhones = @();
            TechnicalNotificationMails           = @("jarrod@j-tech.net.au");
        }
        EXOEmailAddressPolicy a2188f3f-80d5-419c-b229-063fc2c18dbf
        {
            Credential                        = $Credential;
            EnabledEmailAddressTemplates      = @("SMTP:@$OrganizationName");
            EnabledPrimarySMTPAddressTemplate = "@$OrganizationName";
            Ensure                            = "Present";
            ManagedByFilter                   = "";
            Name                              = "Default Policy";
            Priority                          = "Lowest";
        }
    }
}

因此,在合并的配置文件中,我只需要合并每个单独的配置文件中的 Node localhost 下的部分,而不是合并整个文件内容。

我需要这个,以便我可以一次将所有 DSC 配置应用到 Office 365 租赁,而不是应用单独的配置。

希望这是有道理的。

【问题讨论】:

    标签: powershell office365 dsc


    【解决方案1】:

    以下脚本获取所需的组合文件。您只能将 DSC 文件放在“PathToDSCFiles”文件夹中。这也仅在给定的 DSC 文件都具有示例中给出的格式时才有效,因为它只是跳过给定脚本中的 21 行并从那里继续。如果您的文件中有其他格式,则必须找到一种方法来选择“Node Localhost”之后的所有内容。

    $path = "PathToDSCFiles"
    $PathToCombinedFile = "PathToCombinedFile"
    $list = (Get-ChildItem -Path $path).Name
    $first = $list | select -First 1
    
    $SecondWord = (Get-Content $path$first).Split(" ")[1]
    ((Get-Content $path$first)[0]) -creplace($SecondWord,"CombinedPolicy") > $PathToCombinedFile
    Get-Content $path$first | select -skip 1 | select -skipLast 2 >> $PathToCombinedFile
    foreach($file in ($list | select -skip 1)){
        if ($file -eq ($list | select -skip 1)[-1]){
            Get-Content -Path $path$file | select -Skip 21 >> $PathToCombinedFile
        }else{
            Get-Content -Path $path$file | select -Skip 21 | select -SkipLast 2 >> $PathToCombinedFile
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-06-19
      • 1970-01-01
      • 1970-01-01
      • 2015-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-19
      • 1970-01-01
      相关资源
      最近更新 更多