gpt4 book ai didi

Powershell 脚本组合

转载 作者:行者123 更新时间:2023-12-03 00:14:36 24 4
gpt4 key购买 nike

如何组合这些 PowerShell cmdlet 而不是单独运行它们?这些命令在我们的 Exchange 服务器上运行,用于内部部署和 Office 365。

# General
New-DistributionGroup -Name dis_its3 -DisplayName dis_its3 -Alias dis_its3 -PrimarySmtpAddress dis_its3@jackson.k12.ms.us

# Ownership
Set-DistributionGroup -Identity "dis_its3" –ManagedBy Robinson Mykal -BypassSecurityGroupManagerCheck

# Membership approval
Get-DistributionGroup | Set-DistributionGroup -MemberJoinRestriction:closed -MemberDepartRestriction:closed

# Delivery Management
Set-DistributionGroup "dis_its3" -RequireSenderAuthenticationEnabled $False

图中显示的是 PowerShell 脚本正在自动化的内容:

O365 screenshot

最佳答案

您可以创建如下所示的函数并将其放入 .psm1 中,然后将其作为模块导入或导入 .ps1 并将其点源到您的 session 中。然后你可以像我在下面显示的那样调用这个函数。请注意,您可能需要添加一些错误处理。

 function Set-CustomDistributionGroup
{
[CmdletBinding()]
param
(
[Parameter(
Position = 0,
Mandatory = $true
)]
[ValidateNotNullorEmpty()]
[Alias('DistributionGroupName')]
[String] $Name,

[Parameter(
Position = 1,
Mandatory = $true
)]
[ValidateNotNullorEmpty()]
[Alias('DistributionGroupDisplayName')]
[String] $DisplayName,

[Parameter(
Position = 2,
Mandatory = $true
)]
[ValidateNotNullorEmpty()]
[Alias('DistributionGroupAlias')]
[String] $Alias,

[Parameter(
Position = 3,
Mandatory = $true
)]
[ValidateNotNullorEmpty()]
[Alias('DistributionGroupPrimarySmtpAddress')]
[String] $PrimarySmtpAddress,

[Parameter(
Position = 4,
Mandatory = $true
)]
[ValidateNotNullorEmpty()]
[Alias('DistributionGroupManagedBy')]
[String] $ManagedBy
)
process
{
# General
New-DistributionGroup -Name $Name -DisplayName $DisplayName -Alias $Alias -PrimarySmtpAddress $PrimarySmtpAddress

# Ownership
Set-DistributionGroup -Identity $Name –ManagedBy $ManagedBy -BypassSecurityGroupManagerCheck

# Membership approval
Get-DistributionGroup | Set-DistributionGroup -MemberJoinRestriction:closed -MemberDepartRestriction:closed

# Delivery Management
Set-DistributionGroup -Identity $Name -RequireSenderAuthenticationEnabled $False
}
}

然后像这样调用它:
 Set-CustomDistributionGroup -Name 'dis_its3' -DisplayName 'dis_its3' -Alias 'dis_its3' -PrimarySmtpAddress 'jackson.k12.ms.us' -ManagedBy 'Robinson Mykal'

但是我会小心这个电话,因为你没有指定要得到什么。许多 powershell cmdlet 只会返回所有内容,最终效果是您更新所有这些。
 # Membership approval
Get-DistributionGroup | Set-DistributionGroup -MemberJoinRestriction:closed -MemberDepartRestriction:closed

关于Powershell 脚本组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38510065/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com