需要在PowerShell脚本中修剪URL

时间:2018-11-02 17:24:45

标签: powershell url

我有一个网址www.example.com:1234/,我需要将上面的内容缩小为2个变量:

  1. example.com
  2. 00234
    • 端口的第一位数字将替换为00

这可以在PowerShell中实现吗?

3 个答案:

答案 0 :(得分:1)

CommandLine flags: -XX:CMSInitiatingOccupancyFraction=75 -XX:+ExplicitGCInvokesConcurrent -XX:InitialHeapSize=2147483648 -XX:MaxHeapSize=2147483648 -XX:MaxTenuringThreshold=6 -XX:NewRatio=1 -XX:OldPLABSize=16 -XX:+PrintGC -XX:+PrintGCApplicationStoppedTime -XX:+PrintGCDateStamps -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintPromotionFailure -XX:ThreadStackSize=512 -XX:+UseCMSInitiatingOccupancyOnly -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseConcMarkSweepGC -XX:+UseParNewGC 
2018-11-03T01:26:37.136-0800: 0.245: Total time for which application threads were stopped: 0.0000852 seconds, Stopping threads took: 0.0000282 seconds
2018-11-03T01:26:37.136-0800: 0.245: Total time for which application threads were stopped: 0.0000434 seconds, Stopping threads took: 0.0000213 seconds
2018-11-03T01:26:37.136-0800: 0.245: Total time for which application threads were stopped: 0.0000500 seconds, Stopping threads took: 0.0000072 seconds
2018-11-03T01:26:38.138-0800: 1.247: Total time for which application threads were stopped: 0.0000811 seconds, Stopping threads took: 0.0000215 seconds
2018-11-03T01:26:41.441-0800: 4.550: Total time for which application threads were stopped: 0.0000860 seconds, Stopping threads took: 0.0000173 seconds
2018-11-03T01:26:42.419-0800: 5.527: [GC (System.gc()) 2018-11-03T01:26:42.419-0800: 5.527: [ParNew: 33556K->536K(943744K), 0.0049418 secs] 33556K->536K(1992320K), 0.0051371 secs] [Times: user=0.02 sys=0.00, real=0.01 secs] 
2018-11-03T01:26:42.424-0800: 5.533: Total time for which application threads were stopped: 0.0052935 seconds, Stopping threads took: 0.0000191 seconds
2018-11-03T01:26:42.424-0800: 5.533: [GC (CMS Initial Mark) [1 CMS-initial-mark: 0K(1048576K)] 536K(1992320K), 0.0003129 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
2018-11-03T01:26:42.424-0800: 5.533: Total time for which application threads were stopped: 0.0003991 seconds, Stopping threads took: 0.0000239 seconds
2018-11-03T01:26:42.424-0800: 5.533: [CMS-concurrent-mark-start]
2018-11-03T01:26:42.438-0800: 5.547: [CMS-concurrent-mark: 0.014/0.014 secs] [Times: user=0.01 sys=0.00, real=0.01 secs] 
2018-11-03T01:26:42.438-0800: 5.547: [CMS-concurrent-preclean-start]
2018-11-03T01:26:42.440-0800: 5.548: [CMS-concurrent-preclean: 0.002/0.002 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
2018-11-03T01:26:42.440-0800: 5.548: [GC (CMS Final Remark) [YG occupancy: 536 K (943744 K)]2018-11-03T01:26:42.440-0800: 5.548: [Rescan (parallel) , 0.0010829 secs]2018-11-03T01:26:42.441-0800: 5.550: [weak refs processing, 0.0000814 secs]2018-11-03T01:26:42.441-0800: 5.550: [class unloading, 0.0002646 secs]2018-11-03T01:26:42.441-0800: 5.550: [scrub symbol table, 0.0003364 secs]2018-11-03T01:26:42.442-0800: 5.550: [scrub string table, 0.0001618 secs][1 CMS-remark: 0K(1048576K)] 536K(1992320K), 0.0020665 secs] [Times: user=0.01 sys=0.00, real=0.00 secs] 
2018-11-03T01:26:42.442-0800: 5.551: Total time for which application threads were stopped: 0.0021350 seconds, Stopping threads took: 0.0000122 seconds
2018-11-03T01:26:42.442-0800: 5.551: [CMS-concurrent-sweep-start]
2018-11-03T01:26:42.442-0800: 5.551: [CMS-concurrent-sweep: 0.000/0.000 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
2018-11-03T01:26:42.442-0800: 5.551: [CMS-concurrent-reset-start]
Heap
 par new generation   total 943744K, used 8926K [0x0000000740000000, 0x0000000780000000, 0x0000000780000000)
  eden space 838912K,   1% used [0x0000000740000000, 0x00000007408315f0, 0x0000000773340000)
  from space 104832K,   0% used [0x00000007799a0000, 0x0000000779a263b0, 0x0000000780000000)
  to   space 104832K,   0% used [0x0000000773340000, 0x0000000773340000, 0x00000007799a0000)
 concurrent mark-sweep generation total 1048576K, used 0K [0x0000000780000000, 0x00000007c0000000, 0x00000007c0000000)
 Metaspace       used 3157K, capacity 4568K, committed 4864K, reserved 1056768K
  class space    used 338K, capacity 392K, committed 512K, reserved 1048576K

答案 1 :(得分:1)

这是一种实现方法... [咧嘴]

# fake reading in a list of URLs
#    in real life, use Get-Content
$UrlList = @'
www.example.com:1234/
www3.example.net:9876
www.other.example.org:5678/
'@ -split [environment]::NewLine

$Regex = '^www.*?\.(?<Domain>.+):(?<Port>\d{1,}).*$'

$Results = foreach ($UL_Item in $UrlList)
    {
    $Null = $UL_Item -match $Regex

    [PSCustomObject]@{
        URL = $UL_Item
        Domain = $Matches.Domain
        OriginalPort = $Matches.Port
        Port = '00{0}' -f (-join $Matches.Port.ToString().SubString(1))
        }
    }

$Results

输出...

URL                        Domain           OriginalPort Port 
---                        ------           ------------ ---- 
www.example.com:1234/     example.com     1234         00234
www3.example.net:9876      example.net      9876         00876
www.other.example.org:5678/ other.example.org 5678         00678    

注释掉或删除任何不需要的属性。 [咧嘴]


每个请求,一个简化的版本... [咧嘴]

$UserInput = 'www.example.com:1234/'

$Regex = '^www.*?\.(?<Domain>.+):(?<Port>\d{1,}).*$'

$Null = $UserInput -match $Regex

$Domain = $Matches.Domain
$Port = '00{0}' -f (-join $Matches.Port.SubString(1))

$Domain
$Port

输出...

example.com
00234

希望有帮助,

答案 2 :(得分:1)

要改善James C.'s answer

# Input URL string
$urlText = 'www.example.com:1234/'

# Prepend 'http://' and cast to [uri] (System.Uri), which
# parses the URL string into its constituent components.
$urlObj = [uri] "http://$urlText"

# Extract the information of interest
$domain = $urlObj.Host -replace '^www\.' # -> 'example.com'
$modifiedPort = '00' + $urlObj.Port.ToString().Substring(1) # -> '00234'