简体   繁体   English

Powershell .ToUpper和删除空格

[英]Powershell .ToUpper and remove spaces

After a lot of blood, sweat, tears, and amazing assistance from folks on these boards I finally have my code working, but I need a little help with trimming and capitalizing. 在这些板上的人们经过大量的流血,汗水,眼泪和惊人的帮助之后,我终于使我的代码工作了,但是在整理和大写方面需要一些帮助。 I sort of understand how to change something after it is already created, but I'd rather have it converted before it is committed. 我有点理解在创建某些内容后如何对其进行更改,但是我宁愿在提交之前对其进行转换。

Is this the correct syntax: 这是正确的语法:

#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."
}

If that is correct, how do I remove the spaces that MIGHT be added due to the $Name Input? 如果那是正确的,我该如何删除由于$ Name输入而可能添加的空格?

In this instance I want to be able to have a folder created called "Test Share 123," but I'm wanting the AD group created by this to be called "FS-TESTSHARE123-R" and "FS-TESTSHARE123-RW" instead of "FS-Test Share 123-R." 在这种情况下,我希望能够创建一个名为“ Test Share 123”的文件夹,但是我希望由此创建的AD组名为“ FS-TESTSHARE123-R”和“ FS-TESTSHARE123-RW” “ FS测试共享123-R”。

Also, any other suggestions on my script? 另外,我的脚本还有其他建议吗?

$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

You're missing the parens on the ToUpper call 您在ToUpper通话中缺少括号

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

That should help. 那应该有帮助。 If you need to trim trailing spaces, you can chain the calls together (though this one doesn't have trailing spaces).... 如果您需要修剪尾随空格,可以将这些调用链接在一起(尽管此电话没有尾随空格)。

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

这似乎对我有用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM