ค้นหาเว็บไซต์

PowerShell แปลงเป็นสตริง | พารามิเตอร์ของ PowerShell แปลงเป็นสตริง


ภาพรวมของ PowerShell แปลงเป็นสตริง

หนึ่งในการจัดการสตริงที่ใช้กันมากที่สุดคือการสลับชื่อและนามสกุลของผู้ใช้ กล่าวคือ นามสกุลต้องมาก่อนตามด้วยชื่อ เพื่อให้บรรลุฟังก์ชันประเภทนี้ จึงมีการใช้ Convert-String cmdlet ใน PowerShell cmdlet สตริงการแปลงถูกนำมาใช้ครั้งแรกใน PowerShell เวอร์ชัน 5.0 นอกจากนี้ยังเป็นหนึ่งใน cmdlets ที่ไม่ได้รับการประเมินและไม่ได้รับการสำรวจมากที่สุดโดยผู้ใช้ การใช้ cmdlet แปลงสตริงคือสามารถจัดรูปแบบสตริงตามตัวอย่างที่กำหนดโดยผู้ใช้ cmdlet รับอินพุตตัวอย่างจากผู้ใช้ จากนั้นจัดรูปแบบเอาต์พุตในรูปแบบเดียวกันกับอินพุต

ไวยากรณ์

ด้านล่างนี้เป็นไวยากรณ์:

NAME
Convert-String
SYNTAX
Convert-String -InputObject <string> [-Example <List[psobject]>] [<CommonParameters>]
ALIASES
None

คำอธิบายไวยากรณ์ข้างต้น:

นี่หมายถึงรูปแบบตัวอย่างที่ต้องสร้างเอาต์พุต รูปแบบแหล่งที่มาจะถูกระบุทางด้านซ้ายของเครื่องหมายเท่ากับ (=) และด้านขวาหมายถึงรูปแบบเป้าหมาย อีกวิธีหนึ่ง สามารถใช้ประเภทตารางแฮชก่อนและหลังคุณสมบัติเพื่อจัดรูปแบบสตริงได้ จะต้องสังเกตอย่างระมัดระวังอย่าเว้นช่องว่างหลังเครื่องหมายเท่ากับ ประเภทของมันคือรายการทั่วไปของออบเจ็กต์ PowerShell นามแฝงของมันคือ E ค่าเริ่มต้นคือไม่มี ไม่ยอมรับอินพุตไปป์ไลน์และไม่อนุญาตให้ใช้อักขระตัวแทน

พารามิเตอร์ของการแปลงเป็นสตริง

ด้านล่างเป็นพารามิเตอร์ที่แตกต่างกันของการแปลงเป็นสตริง:

1. -อินพุตวัตถุ

นี่หมายถึงสตริงอินพุตที่จะจัดรูปแบบ ประเภทของมันคือสตริง ค่าเริ่มต้นคือไม่มี ยอมรับอินพุตไปป์ไลน์ แต่ไม่อนุญาตให้ใช้อักขระตัวแทน

ตัวอย่าง #1

รหัส:

Write-Host "Welcome to convert string tutorial"
$input=@("vignesh krishnakumar", "Nandhini venkatesan", "Vyapini Vignesh", "Rafa Nadal", "input output", "Krishnakumar sankaran", "Durgalakshmi Krishnakumar")
Write-Host "The input string is as follows" $input
Write-Host "Interchanging first and last names"
write-host "Interchnaged name is"
$input | Convert-String -Example "one two= two,one"

เอาต์พุต:

ตัวอย่าง #2

อินพุต:

Write-Host "Welcome to convert string tutorial"
$input=@("vignesh one krishnakumar", "Nandhini two venkatesan", "Vyapini three Vignesh", "Rafa four Nadal", "input five output", "Krishnakumar six sankaran", "Durgalakshmi seven Krishnakumar")
Write-Host "The input string is as follows" $input -ForegroundColor Yellow
Write-Host "Removing last name and appending hypen"
write-host "Interchnaged name is"
$output=$input | Convert-String -Example "one three two=three-one"
Write-Host $output -ForegroundColor Yellow
Write-Host "Example when strings doesnt match the format"
$input=@("vignesh krishnakumar", "Nandhini two venkatesan", "Vyapini Vignesh", "Rafa four Nadal", "input five output", "Krishnakumar sankaran", "Durgalakshmi seven Krishnakumar")
$output=$input | Convert-String -Example "one three two=three-one"
Write-Host $output -ForegroundColor Yellow

เอาต์พุต:

ตัวอย่าง #3

อินพุต:

Write-Host "Welcome to convert string tutorial"
Write-Host "Applying multiple patterns to the input"
$SamplePatterns = @(
@{before='"one","two"'; after='two: one'},
@{before='"world","1"'; after='1: world'},
@{before='"one-two","22"'; after='22: one-two'},
@{before='"hi hello","333"'; after='333: hi hello'}
)
$input = Get-Process  | Select-Object -Property ProcessName, CPU | ConvertTo-Csv -NoTypeInformation
$input | Convert-String -Example $SamplePatterns

เอาต์พุต:

2. นอกสตริง

cmdlet นี้ใช้เพื่อแปลงวัตถุ PowerShell เป็นสตริง

ไวยากรณ์:

NAME
Out-String
SYNTAX
Out-String [-Stream] [-Width <int>] [-InputObject <psobject>] [<CommonParameters>]
ALIASES
None

พารามิเตอร์ของ Out-String

ด้านล่างนี้คือพารามิเตอร์ต่างๆ ของ out-string:

1. -อินพุตวัตถุ

หมายถึงวัตถุที่จะแปลงเป็นสตริง อาจเป็นตัวแปรที่เก็บวัตถุ นิพจน์ หรือ cmdlet ที่จะส่งคืนวัตถุ ประเภทของมันคือ PS Object ค่าเริ่มต้นคือไม่มี ยอมรับอักขระไปป์ไลน์ แต่ไม่ยอมรับอักขระตัวแทน

2. -ไม่มีการขึ้นบรรทัดใหม่

มันหมายถึงการขึ้นบรรทัดใหม่ที่ไม่ควรเพิ่ม ถ้าออบเจ็กต์สตริงมีการขึ้นบรรทัดใหม่ เหล่านั้นจะไม่ถูกลบออก ประเภทของมันคือพารามิเตอร์สวิตช์ ค่าเริ่มต้นคือไม่มี ไม่ยอมรับอักขระไปป์ไลน์และไม่ยอมรับอักขระไวด์การ์ดด้วย

3. -สตรีม

มันหมายถึงแต่ละออบเจ็กต์จะมีการส่งสตริงแยกกันในเอาต์พุต ประเภทของมันคือพารามิเตอร์สวิตช์ ค่าเริ่มต้นเป็นเท็จ ไม่ยอมรับอักขระไปป์ไลน์และไม่ยอมรับอักขระไวด์การ์ดด้วย

4. -ความกว้าง

ระบุจำนวนอักขระที่ควรปรากฏในเอาต์พุต อักขระเพิ่มเติมจะถูกตัดทอน ค่าเริ่มต้นของอักขระคือ 80 ค่าเริ่มต้นคือไม่มี ไม่ยอมรับอักขระไปป์ไลน์และไม่ยอมรับอักขระไวด์การ์ดด้วย

ตัวอย่าง #1

อินพุต:

Write-Host "Welcome to out string example"
Write-Host "Reading a file and outing as string"
Write-Host "Reading the content...."
$input=get-content "C:\Vignesh\KB.txt"
Write-Host "Converting to string"
$input | Out-String

เอาต์พุต:

ตัวอย่าง #2

อินพุต:

Write-Host "Welcome to out string example"
Write-Host "find all the alias with get"
get-alias | out-string -stream | select-string "Get-"
Write-Host "find all the alias with set"
get-alias | out-string -stream | select-string "Set-"
Write-Host "find all the alias with write"
get-alias | out-string -stream | select-string "Write-"
Write-Host "find all the alias with Out"
get-alias | out-string -stream | select-string "Out-"

เอาต์พุต:

ตัวอย่าง #3

อินพุต:

Write-Host "welcome to conversion of int to string"
$int= 12
Write-Host "type before conversion is" $int.GetType()
Write-Host "Converting to int"
Write-Host $int
$new=$int.ToString()
Write-Host "Now the type is" $new.GetType() $new
Write-Host "another way of converting int to string"
$number=1654
Write-Host "The data type is" $number.GetType()
Write-Host "Converting to string"
$str=[String]$number
Write-Host "Converted data type is" $str.GetType()

เอาต์พุต:

ตัวอย่าง #4

อินพุต:

Write-Host "coversion of date time to string"
$date= Get-Date
Write-Host $date
Write-Host "The type is "$date.GetType()
Write-Host "First way of converting to string"
$first = '{0:MM/dd/yy}' -f $date
Write-Host "The type now is" $first.GetType()
Write-Host "Second way of converting to string"
$second = $date.ToShortDateString()
Write-Host "Now the type is" $second.GetType()
Write-Host "Third way of converting to string"
$Third = [String]$date
Write-Host "Now the type is" $Third.GetType()

เอาต์พุต:

บทสรุป

ดังนั้นบทความนี้จึงกล่าวถึงรายละเอียดเกี่ยวกับ Convert-String cmdlet นอกจากนี้ยังแสดงตัวอย่างต่างๆ ของวิธีใช้ cmdlet และพารามิเตอร์ที่เกี่ยวข้องด้วย บทความนี้ยังครอบคลุมรายละเอียดเกี่ยวกับ Out-String cmdlet พารามิเตอร์ที่เกี่ยวข้องพร้อมกับตัวอย่างที่เหมาะสม บทความนี้ยังกล่าวถึงวิธีการแปลงจำนวนเต็มหรือวัตถุวันที่-เวลาเป็นตัวแปรสตริงโดยใช้วิธีการที่เหมาะสมและด้วยความช่วยเหลือของการพิมพ์ เพื่อให้ครอบคลุมมากขึ้นและเรียนรู้ในรายละเอียด ขอแนะนำให้เขียนสคริปต์ตัวอย่างและฝึกฝน

บทความที่เกี่ยวข้อง