簡體   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