簡體   English   中英

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測試共享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()

那應該有幫助。 如果您需要修剪尾隨空格,可以將這些調用鏈接在一起(盡管此電話沒有尾隨空格)。

'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