我想從字串中獲取環境、專案名稱和位置,并將其存盤在 Powershell 中的變數中。該字串的格式為env-project-location. 例子uat-hrapp-westeurope
如何過濾字串并將輸出存盤在變數中?
$environment = "uat"
$project = "hrapp"
$location = "westeurope"
uj5u.com熱心網友回復:
你不需要正則運算式,假設字串總是有相同的命名約定,一個簡單的拆分就可以做到:
$environment, $project, $location = 'uat-hrapp-westeurope'.Split('-')
uj5u.com熱心網友回復:
Santiago Squarzon 的帖子讓我思考,所以我做了一點谷歌搜索,發現如果你想要 PSCustomObject 與自變數,也可以使用此方法。
Clear-Host
$Base = "uat-hrapp-westeurope"
$CFSArgs = @{PropertyNames = "Environment", "Project", "Location"
Delimiter = '-'}
$obj = $Base | ConvertFrom-String @CFSArgs
$Obj
輸出:
Environment Project Location
----------- ------- --------
uat hrapp westeurope
PS> $obj | gm
TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
Environment NoteProperty string Environment=uat
Location NoteProperty string Location=westeurope
Project NoteProperty string Project=hrapp
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/405507.html
標籤:
上一篇:下次運行腳本時中間名值仍然存在
