繁体   English   中英

Powershell阵列

[英]Powershell Array

我试图让多个用户的名字和姓氏转换为一种格式:使用GUI输入框时,名字的首字母+姓氏。 我的代码是错误的,它保留了第一个数组字母,并且不会删除它以允许其他输入执行相同的任务。 我尝试使用“删除”来帮助删除固定大小的数组。 没用 我读到我必须创建另一个数组以从第一个数组中删除。 这就是我卡住的地方。 如何使第二个数组删除第一个条目,同时尽可能保留相同的变量-我想在输入框中输入X个名称,并为每个用户运行其他任务。

现在的结果如下:jblank jeric jbob

我试图得到

jblank,enewnew,BMO

请帮助。 谢谢

Add-Type –assemblyName Microsoft.VisualBasic
Add-Type –assemblyName System.Windows.Forms

$User = "joe blank, eric newnew, bob mo" #
[Microsoft.VisualBasic.Interaction]::InputBox("Enter Employee name or 
names.`nSeparate with a comma (,) or semi-colon (;).`n ** Do not add 
quotation marks **", "Search book")

If (-Not [System.String]::IsNullOrEmpty($User)) {
    [string[]]$Username = $User -split ",|;"
    ForEach ($User in $Username) { 
   #Write-host $User -ForegroundColor Yellow  

$Fname = $User.Split(" ")[0].trim()
$Lname = $User.Split(" ")[1].trim()



#initials
$In = $FName
$InRM = $In.Remove(1)
$unID = $InRM+$LName # intials done

$newID = @()
   foreach ($u in $User)
  {
    if ($User -ne 0)
    {
       Write-Host "Remove [0] here or something like that eq to $unID again"
    }
}

Write-Host $unID -foregroundcolor "green"
## Do more stuff here for each user ##

你觉得这样吗?

$User = "joe blank, eric newnew, bob mo"
#define arry for later
$SortUser = @()
#split users from textbox
$Users = @($User.Split(","))
#loop users
foreach($i in $Users){
    if($i.Substring(0,1) -eq " "){
    #remove space which make problems
        $i = $i.Remove(0,1)  
    }
    $Split = $i.Split(" ")
    $First = $Split[0]
    $Last = $Split[1]
    $FirstLetter = $First.Remove(1,$First.Length - 1)

    #Add all informations to object
    $obj = New-Object PSCustomObject
    $obj | Add-Member -type NoteProperty -name First -Value $First
    $obj | Add-Member -type NoteProperty -name Last -Value $Last
    $obj | Add-Member -type NoteProperty -name Username -Value "$FirstLetter.$Last"
    #fill all objects in one array
    $SortUser += $obj
}
$SortUser

结果:

First Last   Username
----- ----   --------
joe   blank  j.blank 
eric  newnew e.newnew
bob   mo     b.mo 

暂无
暂无

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

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