Powershell .ToUpper并删除空格

时间:2016-04-12 15:29:24

标签: string powershell syntax uppercase

经过大量的血,汗,泪,以及这些板上人们的惊人帮助,我终于让我的代码工作了,但我需要一点修剪和大写的帮助。我有点理解如何在已经创建之后改变它,但我宁愿在它被提交之前进行转换。

这是正确的语法:

#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

2 个答案:

答案 0 :(得分:0)

你在ToUpper电话上错过了parens

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

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

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

答案 1 :(得分:0)

这似乎对我有用:

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