gpt4 book ai didi

powershell - 如何将本地组的描述设置为空白

转载 作者:行者123 更新时间:2023-12-04 14:51:21 25 4
gpt4 key购买 nike

考虑以下代码:

# create test group
New-LocalGroup -Name 'Group1' -Description 'xxx'

# update test group description to blank
Set-LocalGroup -Name 'Group1' -Description '' # fails
Set-LocalGroup -Name 'Group1' -Description $null # fails

相反,可以创建一个没有描述的组:

New-LocalGroup -Name 'Group2'

如何在不先删除组的情况下将本地组的组描述更新为空白?这发生在 PowerShell 5.1 上。

最佳答案

虽然有一些 AD 属性需要 Putex method ,这不计入 Description 属性。这意味着我在问题的初始评论中的假设是错误的,并且可以仅使用 Put method` 清除 Description 属性。 :

$Name = 'Group1'
New-LocalGroup -Name $Name -Description 'xxx'

$Group = [ADSI]"WinNT://./$Name,group"
$Group.Put('Description', '')
$Group.SetInfo()

Get-LocalGroup -Name $Name

Name Description
---- -----------
Group1

问题完全在于 cmdlet 参数定义。无需进入 C# 编程,您可以直接从代理命令中提取:

$Command = Get-Command Set-LocalGroup
$MetaData = [System.Management.Automation.CommandMetadata]$Command
$ProxyCommand = [System.Management.Automation.ProxyCommand]::Create($MetaData)
$ProxyCommand

[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='Medium', HelpUri='https://go.microsoft.com/fwlink/?LinkId=717979')]
param(
[Parameter(Mandatory=$true)]
[ValidateNotNull()] # <-- Here is the issue
[ValidateLength(0, 48)]
[string]
${Description},
...

换句话说,使用代理命令快速解决这个问题:

function SetLocalGroup {
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='Medium', HelpUri='https://go.microsoft.com/fwlink/?LinkId=717979')]
param(
[Parameter(Mandatory=$true)]
[AllowEmptyString()] # <-- Modified
[ValidateLength(0, 48)]
[string]
${Description},

[Parameter(ParameterSetName='InputObject', Mandatory=$true, Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[ValidateNotNull()]
[Microsoft.PowerShell.Commands.LocalGroup]
${InputObject},

[Parameter(ParameterSetName='Default', Mandatory=$true, Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[ValidateNotNull()]

${Name},

[Parameter(ParameterSetName='SecurityIdentifier', Mandatory=$true, Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[ValidateNotNull()]
[System.Security.Principal.SecurityIdentifier]
${SID})

end {
if ($Description) { Set-LocalGroup @PSBoundParameters }
elseif ($Name) {
$Group = [ADSI]"WinNT://./$Name,group"
$Group.Put('Description', '')
$Group.SetInfo()
}
}
}

SetLocalGroup -Name 'Group1' -Description ''

相关错误报告:#16049 AllowEmptyString()] for -Description in Set-LocalGroup/SetLocalUser

关于powershell - 如何将本地组的描述设置为空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69041644/

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