PowerShell to remove text from a string

前端 未结 5 547
不思量自难忘°
不思量自难忘° 2020-12-06 09:59

What is the best way to remove all text in a string after a specific character? In my case "=" and after another character in my case a ,, but keep th

5条回答
  •  醉梦人生
    2020-12-06 10:29

    I referenced @benjamin-hubbard 's answer above to parse the output of dnscmd for A records, and generate a PHP "dictionary"/key-value pairs of IPs and Hostnames. I strung multiple -replace args together to replace text with nothing or tab to format the data for the PHP file.

    $DnsDataClean = $DnsData `
        -match "^[a-zA-Z0-9].+\sA\s.+" `
        -replace "172\.30\.","`$P." `
        -replace "\[.*\] " `
        -replace "\s[0-9]+\sA\s","`t"
    
    $DnsDataTable = ( $DnsDataClean | `
        ForEach-Object { 
            $HostName = ($_ -split "\t")[0] ; 
            $IpAddress = ($_ -split "\t")[1] ; 
            "`t`"$IpAddress`"`t=>`t'$HostName', `n" ;
        } | sort ) + "`t`"`$P.255.255`"`t=>`t'None'"
    
    "" | Out-File -Encoding ASCII -FilePath IpHostLookups.php
    
    Get-Content IpHostLookups.php
    

提交回复
热议问题