Powershell .ToUpper 并删除空格

Powershell .ToUpper and remove spaces

经过这些板上的人们的大量热血、汗水、泪水和惊人的帮助后,我终于让我的代码可以工作了,但是我需要一些关于修剪和大写的帮助。我有点了解如何在创建后更改某些内容,但我宁愿在提交之前对其进行转换。

这是正确的语法吗:

#Create Security Groups
    $GroupParams1= @{
        'Name' = "FS-$NAME-RW".ToUpper
        'SamAccountName' = "FS-$NAME-RW".ToUpper
        'GroupCategory' = "Security"
        'GroupScope' = "Global"
        'DisplayName' = "$NAME Read-Write Access"
        'Path' = "OU=$LOCATION,OU=FILE SHARE GROUPS,OU=Security Groups,DC=esg,DC=intl"
    'Description' = "Members of this group have read-write access to $Path."
}

如果这是正确的,我该如何删除可能因 $Name 输入而添加的空格?

在这种情况下,我希望能够创建一个名为 "Test Share 123," 的文件夹,但我希望由此创建的 AD 组被称为 "FS-TESTSHARE123-R" 和 "FS-TESTSHARE123-RW"共 "FS-Test Share 123-R."

此外,还有关于我的剧本的其他建议吗?

$Parent = read-host -prompt "Enter full parent path that will contain the new folder (ie. \eccofs01\Groups\ECCO IT\)"
$Name = read-host -prompt "Enter New Folder Name."
$Path = "$($parent)$($Name)"
$Location = read-host -prompt "Enter the AD Security Group Location (i.e. Global, Americas, Europe, Asia Pacific)"

Import-Module ActiveDirectory

#Create Security Groups
$GroupParams1= @{
    'Name' = "FS-$NAME-RW" 
    'SamAccountName' = "FS-$NAME-RW" 
    'GroupCategory' = "Security"
    'GroupScope' = "Global"
    'DisplayName' = "$NAME Read-Write Access"
    'Path' = "OU=$LOCATION,OU=FILE SHARE GROUPS,OU=Security Groups,DC=esg,DC=intl"
    'Description' = "Members of this group have read-write access to $Path."
}

New-ADGroup @GroupParams1

$GroupParams2= @{
    'Name' = "FS-$NAME-R" 
    'SamAccountName' = "FS-$NAME-R" 
    'GroupCategory' = "Security"
    'GroupScope' = "Global"
    'DisplayName' = "$NAME Read-Write Access"
    'Path' = "OU=$LOCATION,OU=FILE SHARE GROUPS,OU=Security Groups,DC=esg,DC=intl"
    'Description' = "Members of this group have read access to $Path"
}

New-ADGroup @GroupParams2

# Create New Folder
New-Item -Path $Path -ItemType Directory

#Create All ACEs (permission sets) and Set ACL on the new folder
function New-Ace {
  [CmdletBinding()]
  Param(
    [Parameter(Mandatory=$true, Position=0)]
    [Security.Principal.NTAccount]$Account,
    [Parameter(Mandatory=$false, Position=1)]
    [Security.AccessControl.FileSystemRights]$Permissions = 'ReadAndExecute',
    [Parameter(Mandatory=$false, Position=2)]
    [Security.AccessControl.InheritanceFlags]$InheritanceFlags = 'ContainerInherit,ObjectInherit',
    [Parameter(Mandatory=$false, Position=3)]
    [Security.AccessControl.PropagationFlags]$PropagationFlags = 'None',
    [Parameter(Mandatory=$false, Position=4)]
    [Security.AccessControl.AccessControlType]$Type = 'Allow'
  )

  New-Object Security.AccessControl.FileSystemAccessRule(
    $Account, $Permissions, $InheritanceFlags, $PropagationFlags, $Type
  )
}

$domain = 'ESG.INTL'

$administrators = ([wmi]"Win32_Sid.Sid='S-1-5-32-544'").AccountName

$acl = Get-Acl $path

$administrators, "$domain\Domain Admins" | ForEach-Object {
  $acl.AddAccessRule((New-Ace $_ 'FullControl'))
}
$acl.AddAccessRule((New-Ace "$domain\FS-$NAME-RW" 'Modify'))
$acl.AddAccessRule((New-Ace "$domain\FS-$NAME-R" 'ReadAndExecute'))

Set-Acl $path $acl

您在 ToUpper 调用中缺少括号

'Name' = "FS-$NAME-RW".ToUpper()

这应该有所帮助。如果你需要 trim 尾随空格,你可以将调用链接在一起(尽管这个没有尾随空格)......

'Name' = "FS-$NAME-RW".ToUpper().TrimEnd(' ')

这似乎对我有用:

'Name' = "FS-$($NAME.replace(' ',''))-RW".toupper()