PowerCli substring extraction - powershell

I need to read the Ethernet adapter name from a PowerCli string so I can, through a script, and without user interaction, use it to change the ip address on a windows machine.
Here is how the powerCli string, from where I'm going to read the substring, will look:
C:\> $myIpInfoString
ScriptOutput
|
| Windows IP Configuration
|
|
| Ethernet adapter Local Area Connection 6:
|
| Connection-specific DNS Suffix . :
| Autoconfiguration IPv4 Address. . : 255.255.255.255
| Subnet Mask . . . . . . . . . . . : 255.255.255.255
| Default Gateway . . . . . . . . . :
|
how can I end up with another string variable that includes something like:
C:\>$myGoalString
Local Area Connection 6
The reason why this is important is because the number 6 changes from one machine to the other and can be, for example, "Local Area Connection 15".

Use Select-String:
$myGoalString = $myIpInfoStrong |
Select-String '^[\s\S]*?Ethernet adapter (.*?):' |
select -Expand Matches | select -Expand Groups |
select -Last 1 Value
With PowerShell v3 or newer you can simplify that a little, like this:
$myGoalString = $myIpInfoStrong |
Select-String '^[\s\S]*?Ethernet adapter (.*?):' |
% { $_.Matches.Groups[1].Value }

Related

Is there a way to query azure virtual networks for the next available subnet range

I have a virtual network (VNET) configured in azure with an address space of 10.0.0.0/8 that I'm using for a sort of (poor man's) IPAM functionality only.
I have several subnets configured in that NVET, 10.14.1.0/24, 10.14.2.0/24, 10.14.3.0/24, ...
What I want to do is query the NVET for the next available subnet "slot" (e.g. 10.14.12.0/24 is in use but 10.14.13.0/24 is not yet in use), and then store that "slot" as a variable and reserve it by creating the subnet with a given name.
$vnet = Get-AzVirtualNetwork -Name IPAM-vnet
$sublist = Get-AzVirtualNetworkSubnetConfig -VirtualNetwork $vnet | Where-Object {$_.AddressPrefix -gt "10.14*"} | Sort-Object -Property AddressPrefix -Descending
$lastsub = $sublist[0].AddressPrefix
PS C:\> write-host $lastsub
10.14.12.0/24
How can I increment the 3rd octet in this string (10.14.12.0/24) to (10.14.13.0/24) ?
There are several ways you can get the third octet.
$template = #'
10.14.{third:12}.0/24
'#
$thirdoctet = '10.14.12.0/24' | ConvertFrom-String -TemplateContent $template | select -Expand third
$thirdoctet = if('10.14.12.0/24' -match '(\d{2,3})(?=.\d*\/24)'){$matches.1}
$thirdoctet = '10.14.12.0/24' -split '\.' | select -Skip 2 -First 1
get-variable thirdoctet
Name Value
---- -----
thirdoctet 12
Now you can just add 1. However, since some of those are actually strings, you may end up with
$thirdoctet + 1
121
If you position the integer on the left hand side, it will coerce the right hand side to an integer as well, if it can.
$nextoctet = 1 + $thirdoctet
$nextoctet
13
Now you can just replace the octet
$newsubnet = $lastsub -replace $thirdoctet,$nextoctet
To increment and end up with the desired string in one go, I'd use the regex method
$newsubnet = $lastsub -replace '(\d{2,3})(?=.\d*\/24)',(1+$matches.1)
get-variable newsubnet
Name Value
---- -----
newsubnet 10.14.13.0/24

removing words from a line output powershell

I have a powershell command with below output, The command output shows the active NIC adapter and NIC adapter name differ in each machine. But what I am looking here is, in one server active NIC adapter is Local Area Connection and in another one it is Ethernet.This will differ in all VM's
PS C:\> netsh interface ipv4 show interface |where { $_ -match '\sconnected' -and $_ -notmatch 'loopback'}
12 5 1500 connected **Local Area Connection**
PS C:\>
PS C:\> netsh interface ipv4 show interface |where { $_ -match '\sconnected' -and $_ -notmatch 'loopback'}
12 5 1500 connected **Ethernet**
PS C:\>
I want only the adapter name to be captured (eg: Local Area Connection\Ethernet etc).
Can anyone help me modifying the command so that I will get the NIC adapter name without any white space as output?
The output should be like below
Local Area Connection
Ethernet
Ethernet 2
If you are running this on a Windows 8 or Server 2012 or newer operating system you can use the Get-NetAdapter cmdlet instead to get the result you want:
Get-NetAdapter | Where {$_.Status -eq 'Up'} | Select -ExpandProperty Name
On an older OS you could try this, which splits the text for each interface by the word "connected" and it's trailing whitespace using the regex special character \s and then return the second item of that split:
$Interfaces = netsh interface ipv4 show interface | where { $_ -match '\sconnected' -and $_ -notmatch 'loopback'}
$Interfaces | ForEach-Object {
($_ -Split 'connected\s+')[1]
}
Or here is another option that avoids using the -split operator and instead uses a regex lookbehind to match 'connected' followed by 5 spaces in the string before what we want to return:
$Interfaces = netsh interface ipv4 show interface | where { $_ -notmatch 'loopback'}
$Interfaces | ForEach-Object {
[regex]::matches($_, '(?<=connected\s{5})(.*)').value
}

Powershell v5- Export only the value

How to export the below with only the value.
Currently it exports below like this: PC IPv4 Address. . . . . . . . . . . : 192.168.30.50.
i would like the output to be: PC 192.168.30.50
script:
$ip = ipconfig | Select-String IPv4
$Host2 = Hostname
"$Host2 $ip" | Out-File "\\pc\c$\temp\Ipconfig.csv" -Append
Try this:
$hostname = hostname
ipconfig | where {$_ -match 'IPv4.+\s(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' } >> $null
$ip=$Matches[1]
$Final_output = "$Hostname : $ip "
$Final_output

get Filtered result in powershell

I want to have filtered result based on 'some string value' from output of a command.
For example, i need to get MAC address from output of ipconfig.
i tried
ipconfig /all | Select-String -Pattern "Physical Address"
I am getting all MAC Addresss like
Physical Address. . . . . . . . . : AA-AA-AA-AA-AA-AA
Physical Address. . . . . . . . . : AA-AA-AA-AA-AA-AA
I want to have only Mac address of wifi adapter.
SO output would be AA-AA-AA-AA-AA-AA.
I dont want entire line
Physical Address. . . . . . . . . : AA-AA-AA-AA-AA-AA
This works on my system:
$Wireless_Adapter_Regex =
#'
(?ms).+?Wireless LAN adapter .+?
Physical Address. . . . . . . . . : (\S+)
'#
(ipconfig /all | out-string) -match $Wireless_Adapter_Regex > $null
$matches[1]
C4-D9-87-41-37-F1
You can use the following command:
ipconfig /all | Select-String -Pattern "\w\w-\w\w-\w\w-\w\w-\w\w-\w\w" -AllMatches | % { $_.Matches } | % { $_.Value }
ipconfig /all | Select-String -Pattern "Physical Address" can be further extended to split output by ':' delimiter and display 2nd field. Example is given below.
PS C:\Users\jump> ipconfig /all | Select-String -SimpleMatch Physical
-AllMatches | ForEach-Object { $_.ToString().split(":")[1]}
B8-AC-6F-28-08-C5

How to select a specific column

I need to extract values of specific column. I need to parse output generated with cmd :
netstat -an |
Select-String "TCP\s+.+\:.+\s+(.+)\:(\d+)\s+(\w+)" |
ForEach-Object {
$key = $_.matches[0].Groups[1].value
$Status = $_.matches[0].Groups[3].value.
Above 2 strings when printed gives me Foreign IP and connection state. I need a column with port no of local IP to which a foreign IP is connected.
If you are running on Windows 8 or Windows Server 2012, you can use the following command examples in Powershell V3:
Pipelining with Select-Object
Get-NetTCPConnection | Select LocalAddress,LocalPort,RemoteAddress,RemotePort
Selecting each property individually
(Get-NetTCPConnection).LocalAddress
(Get-NetTCPConnection).LocalPort
(Get-NetTCPConnection).RemoteAddress
(Get-NetTCPConnection).RemoteAddress
Creating variables using each property
$LocalAddress = (Get-NetTCPConnection).LocalAddress
$LocalPort = (Get-NetTCPConnection).LocalPort
$Remote Address = (Get-NetTCPConnection).RemoteAddress
$RemotePort = (Get-NetTCPConnection).RemoteAddress
These should all come out as lists.
Hope this helps :)
I'm not sure what you mean by a column but I've tweaked your regex and this gets the local and foreign addresses and ports:
netstat -an |
Select-String "TCP\s+(.+)\:(.+)\s+(.+)\:(\d+)\s+(\w+)" |
ForEach-Object {
$LocalAddress = $_.matches[0].Groups[1].value
$LocalPort = $_.matches[0].Groups[2].value
$ForeignAddress = $_.matches[0].Groups[3].value
$ForeignPort = $_.matches[0].Groups[4].value
Write-Output "ForeignAddress: $ForeignAddress `t ForeignPort: $ForeignPort `t LocalAddress: $LocalAddress `t LocalPort: $LocalPort"
}