【问题标题】:Powershell .ToUpper and remove spacesPowershell .ToUpper 并删除空格
【发布时间】:2016-04-12 15:29:23
【问题描述】:

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

这是正确的语法吗:

#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

【问题讨论】:

    标签: string powershell syntax uppercase


    【解决方案1】:

    这似乎对我有用:

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

    【讨论】:

    • 太棒了,谢谢!这有很大帮助。我是倒着想它,并试图首先大写,这使它令人困惑......
    • 不客气!第一次来这里有一个帐户,很高兴我能提供帮助!我的代码实际上错过了一个括号,但我想你已经明白了。
    • 是的,我运行它并看到了错误。不过不用担心。 :D
    【解决方案2】:

    ToUpper 调用中缺少括号

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

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

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

    【讨论】:

    • 我认为他想删除字符串中的所有空格,而不仅仅是在末尾,在这种情况下他可以使用.Replace(),例如:'Name' = "FS-$($NAME.Trim().Replace(' ', '').ToUpper())-RW"
    • 如果我要替换所有空格,似乎不需要 .Trim 。我是不是理解不正确?
    猜你喜欢
    • 2012-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多