如何删除无法删除的powershell中的空格

问题描述

使用 PowerShell 脚本来基本上检查当前可用的 365 许可证并选择一个分配给新用户(我知道其中一些有点卡顿,但正在进行中)。

它将Get-MsolAccountSku收集的数据输出数组,然后通过索引选择哪个许可证并将其加入到租户ID:

(Get-MsolDomain | Where-Object {$_.isInitial}).name

但无论我如何删除空格,即:使用 trim、使用 replace、使用 regex replaces 它总是在许可证名称周围保留一个空格,因此它显示为:

Redactedtenantname: FLOW_FREE

代替:

Redactedtenantname:FLOW_FREE

我也尝试过以某种方式使用 -f 对其进行格式化,但我不知道如何让它发挥良好的效果

我认为可能可行的另一个想法是暂时将其导出到 CSV 并从 CSV 导入,以查看从 CSV 获取凭据是否会清除任何奇怪的空格/格式。

只是想知道是否有人遇到过类似的事情/可能知道该怎么做。

我已经更改了这里的一些内容以使其适合,所以如果输出的格式有点奇怪(缺少空格),它可能只是我复制它的方式。

此外,如果它有助于我得到的最终 $sku 变量是 System.String

#Get Tenancy name (For license assignment)
$Tenant = (Get-MsolDomain | Where-Object {$_.isInitial}).name

#Get list of licenses
$test = Get-MsolAccountSku | select -ExpandProperty AccountSkuID

#Read host for selection of license (at this time only does 1 at a time will improve on) 
$selection = Read-Host "Pick an object on the list i.e 1 for the first item 2 for the second etc"

#the array starts at 0 so just subtracting from the selection to make it 1-1 i.e 1 equals one 2 equals 2 etc.
$select = $selection - 1

#this selects an object from the array based on the number selected and altered above
$license = $test | Select-Object -index $select

#Manipulating the data selected above to make it fit our needs below Splitting at the : and replacing uneccessary data 
$365license = @($license -split ':')
$3651 = $365license -replace '(^\s+|\s+$)','' -replace '\s+',''
$3652 = $361 -replace "redactedtenantsname",""
$tenant1 = $tenant -replace ".onmicrosft.com",""


#Joining tenancy name to formatted license name
$presku1 = "$tenant1",":","$3652"
-join $presku1

$sku           = "$presku1"
$upn = "redactedtestuserupn"

Set-msoluserlicense -userprincipalname "$upn" -addlicenses "$sku"

解决方法

在我看来,问题的核心是您有一个格式为 <account>:<skuId> 的字符串,并且您想用其他内容替换 <account> 部分。

# equivalent to
# $Tenant = (Get-MsolDomain | Where-Object {$_.isInitial}).name
$tenantDomain = "my_tenant_name.onmicrosoft.com";
$tenantName   = $tenantDomain -replace ".onmicrosoft.com","";
# e.g. "my_tenant_name"

# equivalent to
# $test = Get-MsolAccountSku | select -ExpandProperty AccountSkuID
$accountSkuIds = @(
    # the unique string ID of the account/SKU combination.
    # see https://docs.microsoft.com/en-us/powershell/module/msonline/get-msolaccountsku?view=azureadps-1.0#outputs
    "my_other_tenant:ENTERPRISEPACK"
    "my_other_tenant:AAD_BASIC"
    "my_other_tenant:FLOW_FREE"
    "my_other_tenant:POWER_BI_STANDARD"
);

# ask the user which item they want to use
$selectedIndex = 3;

# get the selected sku from the array,remembering that arrays are
# zero-based we we need to subtract 1 from the user's selection
$accountSkuId = $accountSkuIds[$selectedIndex - 1];
# e.g. "my_other_tenant:FLOW_FREE"

# split the string "<account>:<sku>" into the array @( "<account>","<sku>" )
# and then take the *second* item (i.e. [1])
$skuId = @( $accountSkuId -split ":" )[1];
# e.g. "FLOW_FREE"

# convert the @( "<new_account>","<sku>" ) into the string "<new_account>:<sku>"
$license = @( $tenantName,$skuId ) -join ":";
# e.g. "my_tenant_name:FLOW_FREE"

# call the api
$upn = "my_upn";
Set-MsolUserLicense -UserPrincipalName $upn -AddLicenses $license;