简体   繁体   中英

Powershell Array

I'm trying to get multiple users first and last name to be converted into a format: first initial+ last name while using a GUI input box. My code is wrong and it keeps the first array letter and won't remove it to allow the other inputs to do the same task. I tried the Remove to help remove the fixed size array. Didn't work. I read that I must create another array to remove from the first array. This is where I'm stuck. How do I make the second array delete the first entry while keeping the same variables if possible - I want to enter X number of names into the input box and run other other task for each user.

right now the results are the following: jblank jeric jbob

Im trying to get

jblank,enewnew,bmo

help please. thank you

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 ##

Do you think at something like that?

$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

Result:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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