Problem with -split functionality in Powershell

huangapple go评论121阅读模式
英文:

Problem with -split functionality in Powershell

问题

你的文本

然后在另一个 DC 上,尝试使用来自 CSV 文件的数据,我尝试循环遍历每个代理

$proxyToAdd = $userInfo.proxyAddresses -split "-"
foreach($proxy in $proxyToAdd){
    Write-Host $item
    Set-ADUser $foundUser.sAMAccountName -add @{proxyAddresses = $proxy}
}

当运行时,返回以下错误。

smtp:jdoe@local.grp
SMTP:jdoe@local.com

Set-ADUser : add
在 D:\Scripts\Migration Eneria\Traitement_Migration.ps1 的位置:48
+             Set-ADUser $foundUser.sAMAccountName -add @{proxyAddresse ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation : (proteauclementt:ADUser) [Set-ADUser], ADInvalidOperationException
    + FullyQualifiedErrorId : ActiveDirectoryServer:0,Microsoft.ActiveDirectory.Management.Commands.SetADUser

我不太明白这个错误是什么,我还尝试了以下内容

$userInfo.proxyAddresses -split "-" | foreach{
    Write-Host $_
    Set-ADUser $foundUser.sAMAccountName -add @{proxyAddresses = $_}
}

那返回一个无效类型的错误。

英文:

Hello I am having some trouble while trying to loop over a colomn in a export csv table.

$userproxy = $null
    foreach($proxy in $foundUser.proxyAddresses) {
       
        if($proxy.substring(0,4) -like "smtp" -or $proxy.substring(0,4) -like "SMTP") {
            $userproxy += $proxy + "-" 
        }        
    }  

**this is the code used to export the user proxyAddresses which is then added to table. An exmaple of how the table is affected, this table is used to migrate a user from one domain to another. **

"Name","givenName","sn","sAMAccountName","mail","mailNickname","mS-DS-ConsistencyGuid","targetAddress","proxyAddresses"
"John Doe","John","Doe","B004796","jdoe@local.com","jdoe","7c8f6e36-7a21-254a-ae7b-85adb256a777","SMTP:jdoe@local.com","smtp:jdoe@local.grp-SMTP:jdoe@local.com-"

your text

Then on the other DC, while trying to use the data from the csv file I tried to loop over each of the proxies

$proxyToAdd = $userInfo.proxyAddresses -split "-"
         foreach($proxy in $proxyToAdd){
            Write-Host $item
            Set-ADUser $foundUser.sAMAccountName -add @{proxyAddresses = $proxy}
         }

When launched it returns the following error.

smtp:jdoe@local.grp
SMTP:jdoe@local.com

Set-ADUser : add
Au caractère D:\Scripts\Migration Eneria\Traitement_Migration.ps1:48 : 13
+             Set-ADUser $foundUser.sAMAccountName -add @{proxyAddresse ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation : (proteauclementt:ADUser) [Set-ADUser], ADInvalidOperationException
    + FullyQualifiedErrorId : ActiveDirectoryServer:0,Microsoft.ActiveDirectory.Management.Commands.SetADUser
 

I dont really understand what the error is, I also tried the following

$userInfo.proxyAddresses -split "-" | foreach{
            Write-Host $_
            Set-ADUser $foundUser.sAMAccountName -add @{proxyAddresses = $_}
         }

That returned an error not valide type

答案1

得分: 2

让我们尝试将您的-split操作孤立起来,看看这里实际发生了什么:

PS ~> $proxyAddresses = "smtp:jdoe@local.grp-SMTP:jdoe@local.com-"
PS ~> $proxyAddresses -split '-' | ForEach-Object { Write-Host "Got substring '$_'" }
Got substring 'smtp:jdoe@local.grp'
Got substring 'SMTP:jdoe@local.com'
Got substring ''

啊哈!末尾的-意味着-split操作返回一个数组,其中最后一项只是一个_空字符串_ - 这可能是Set-ADUser抱怨的原因!

您可以过滤掉空字符串:

$proxyToAdd = $userInfo.proxyAddresses -split "-" | Where-Object { $_ }

或者您可以使用the String.Split() method ,因为它可以被配置为舍弃空的结果:

$proxyToAdd = $userInfo.proxyAddresses.Split("-", [StringSplitOptions]::RemoveEmptyEntries)
英文:

Let's try and apply your -split operation in isolation and see what actually happens here:

PS ~> $proxyAddresses = "smtp:jdoe@local.grp-SMTP:jdoe@local.com-"
PS ~> $proxyAddresses -split '-' |ForEach-Object { Write-Host "Got substring '$_'" }
Got substring 'smtp:jdoe@local.grp'
Got substring 'SMTP:jdoe@local.com'
Got substring ''

Aha! The trailing - means that the -split operation returns an array where the last item is just an empty string - this is probably what Set-ADUser is complaining about!

You can filter out the empty string:

$proxyToAdd = $userInfo.proxyAddresses -split "-" |Where-Object { $_ }

Or you can use the String.Split() method instead, as it can be made to discard empty results:

$proxyToAdd = $userInfo.proxyAddresses.Split("-", [StringSplitOptions]::RemoveEmptyEntries)

huangapple
  • 本文由 发表于 2023年3月31日 18:22:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75897430.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定