【问题标题】:How to remove impossible to remove white spaces in powershell如何在powershell中删除不可能删除的空格
【发布时间】:2021-09-11 05:50:30
【问题描述】:

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

它将Get-MsolAccountSku收集的数据输出到一个array,然后通过index选择哪个license,并加入到tenant ID from:

(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"

【问题讨论】:

  • 您的代码中有很多不必要的字符串操作,并且不清楚具体是在哪里显示额外的空间,以及它是否可能是输出格式化工件。请考虑提供minimal reproducible example
  • 今晚我可能会尝试减少它,但如果它有帮助,如果我不得不猜测它在下面的某个地方,我认为它是在最初抓取该属性时或从数组中选择它时(为压缩格式我不是普通的海报)。最近我可以告诉它不应该是它如何显示的问题。 $test = Get-MsolAccountSku | select -ExpandProperty AccountSkuID $selection = Read-Host "Pick an object on the list i.e 1 for the first item 2 for the second etc" one 2 equals 2 etc. $select = $selection - 1 $license = $test | Select-Object -index $select
  • Get-MsolAccountSku 输出 [Microsoft.Online.Administration.AccountSKU] 实例,所以我将从它们的 .AccountSkuID 属性值 stringify 开始(除非它们已经 字符串)。一般情况下,请不要在 cmets 中回复(除非通知其他人已对问题进行了更新),请直接更新您的问题。
  • 逐行和逐个变量测试,看看你的代码在哪里失败,一旦你得到那一行,给我们一个示例输入和你的预期输出。
  • 考虑启用严格模式 (Set-StrictMode -Version "Latest") - 这将有助于调试。例如,$3652 = $361 -replace "redactedtenantsname","" 引用了不存在的变量$361,而$presku1 = "$tenant1",":","$3652" -join $presku1 正在尝试与未初始化的变量$presku1 结合。还可以考虑将变量重命名为有意义的名称 - 例如$test 应该是 $accountSkuIds。在后面的代码中会更清楚实际发生了什么。

标签: powershell whitespace license-key


【解决方案1】:

在我看来,您的问题的核心是您有一个格式为 <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;

【讨论】:

    猜你喜欢
    • 2012-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-15
    • 2014-05-26
    相关资源
    最近更新 更多