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
Related
I have a simple script to resolve hostname into IP address and then create from it key-value pair to use in jenkins:
$ips = Resolve-DnsName -type a HOSTNAME | FT IPAddress -HideTableHeaders
echo $ips
>> 192.168.3.1
But when I want to use this output in key-value pair, I receve following:
$keyvalue = $(echo 'ipaddr:'"$ips")
PS C:\Users\vegas.s> echo $keyvalue
ipaddr:
Microsoft.PowerShell.Commands.Internal.Format.FormatStartData Microsoft.PowerShell.Commands.Internal.Format.GroupStartData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.GroupEndData Microsoft.PowerShell.Commands.Internal.Format.FormatEndData
I want to receive following:
ipaddr:192.168.3.1
What am I doing wrong?
try this:
#join the string and ip separated by colon
$keyvalue = 'ipaddr',$ips -join ':'
PS C:\Users\vegas.s> echo $keyvalue
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
I'm new to PS and I am currently trying to make a PS script which can rearrange paragraphs with specific information into a form which can be easily taken to csv file.
The initial information looks like that:
IP Address: 192.168.1.1
Host Name: Test
Domain: contoso.com
IP Address: 192.168.1.2
Host Name: Test2
Domain: contoso.com
And i want to rearrange this info to look like that:
IP Address; Host Name; Domain;
192.168.1.1; Test; contoso.com
192.168.1.2; Test2; contoso.com
Would it be possible to do it and could you give me some examples? Thanks in advance!
Here is a solution :
Sample input file :
IP Address: 192.168.1.1
Host Name: Test
Domain: contoso.com
IP Address: 192.168.1.2
Host Name: Test2
Domain: contoso.com
Script
Clear-Host
#loads the input file and removes the blank lines
$raw = Get-Content "G:\input\rawinfo.txt" | Where-Object { $_ -ne "" }
#declare results array
$results = #()
#foreach line in the input file
$raw | % {
#split the line on ": "
$data = $_ -Split ": "
#switch on first cell of data
switch ($data[0]) {
#store ip address
"IP Address" { $ipaddress= $data[1] }
#store host name
"Host Name" { $hostname = $data[1] }
#store domain
"Domain" {
$domain = $data[1]
#since this is the last field for each item
#build object with all fields
$item = [PSCustomObject]#{
"IP Address" = $ipaddress;
"Host Name" = $hostname;
"Domain" = $domain;
}
#add object to results array
$results += $item
}
}
}
#output results array
$results
Example output :
IP Address Host Name Domain
---------- --------- ------
192.168.1.1 Test contoso.com
192.168.1.2 Test2 contoso.com
You can then pipe it to Export-Csv :
$results | Export-Csv "host_info.csv" -Delimiter ";" -NoTypeInformation
I am going to assume that the information is on a text file
First you need to import the file using Import-CSV , process the data and export it as NewCSV.csv
Here is a code example :
Import-Csv C:\test.txt -Delimiter ":" -Header "Name", "Value" |export-csv c:\test.csv -NoTypeInformation
This should create a file that looks like this :
"Name","Value"
"IP Address","192.168.1.1"
"Host Name","Test"
"Domain","contoso.com"
"IP Address","192.168.1.2"
"Host Name","Test2"
"Domain","contoso.com"
you can change the delimiter using -Delimiter ";"
Import-Csv C:\test.txt -Delimiter ":" -Header "Name", "Value" |export-csv c:\test.csv -NoTypeInformation -Delimiter ";"
I am trying to get a list of IP addresses from several servers defined in server.txt.
Each server has 2 IP addresses and 2 FQDN.
Example:
servername (Production lan):server1 IPaddress:147.111.111.16
servername (backup lan):server1-bck IPaddress:10.0.4.12
Here is the code I'm using:
$servers = Get-Content server.txt
$server2 = "$servers-bck"
$zai = ""
foreach ($server in $servers)
{
$zai = $zai + $server + "`t" +
([System.Net.Dns]::GetHostAddresses($server) | foreach {echo $_.IPAddressToString}) +
"`t" +
([System.Net.Dns]::GetHostAddresses($server2) | foreach {echo $_.IPAddressToString}) +
"`n"
}
$zai > IP-address.csv
Unfortunately only the IP for Production lan is correct. The IP for backup lan only shows the IP of the last server in server.txt. I assume the problem is in: "foreach { echo $._IPAddressToString". I don't know how to fix it.
Any idea or advice will be helpful.
You seem to assume that
$server2="$servers-bck"
would append -bck to the name of each element in the array $servers. That is not the case. Instead, the array is expanded by joining its elements using the output field separator, so that -bck ends up after the last array element:
PS C:\> $a = 'a', 'b', 'c'
PS C:\> $a
a
b
c
PS C:\> "$a-bck"
a b c-bck
To get the backup server for each server from your list you need to append -bck inside the loop:
...([System.Net.Dns]::GetHostAddresses("$server-bck"))...
As a side-note, you shouldn't build CSVs manually. Let PowerShell do that for you:
$servers | % {
New-Object -Type PSObject -Property #{
'Name' = $_
'Production' = ([Net.Dns]::GetHostAddresses($_) | select -Expand IPAddressToString) -join ' '
'Backup' = ([Net.Dns]::GetHostAddresses("$_-bck") | select -Expand IPAddressToString) -join ' '
}
} | Export-Csv 'IP-address.csv' -NoType -Delimiter "`t"
I am trying to read the host file and trying to ping the each host name and after that capturing the IP address in the response and trying to match with the IP address mentioned in the host file.
I have three scenarios:-
1) Its pinging the host and getting the reply back with the correct IP
Result :-Resolved and Replied
2) It's Not pinging at all and not resolving the IP
Result :-Not Resolved and Not Replied
3) It's Pinging but not resolving the IP correctly mentioned to the IP in the host file
Result :-Not Resolved and Replied
I am trying to achieve that scenario with the below script but not fully achieved as different expression need to be used.
Can someone help me to finish it
$lines = Get-Content myfile.txt | Where {$_ -notmatch "((^#)|(^\s+$))"}
# get all test pairs
$tests=$lines |
ForEach-Object{
$fields = $_ -split '\s+'
echo " Fields are $fields"
for ($i = 1; $i -lt $fields.Length; $i++){
New-Object PsObject -Property #{IP=$fields[0];Host=$fields[$i]}
}
}
$tests |
ForEach-Object{
$props=#{
IPAddress=$_.ip
Hostname=$_.Host
Resolve =' '
Reply = ' '
}
$PingResult = ping -n 1 -w 10 $_.host
#echo "$PingResult"
foreach ($line in $PingResult)
{
if ($line.StartsWith("Pinging") -eq $true)
{
$_.ip= $line -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'
echo "IP is $IP"
if ($matches[0])
{
$props.Resolve ='Y'
$props.Reply='Y'
}
else
{
$props.Resolve ='Y'
$props.Reply='N'
}
}
}New-Object PsCustomObject -Property $props
}|
Format-Table -AutoSize | Out-String | Out-File D:\CEP\testlogging.txt
Note:- Cannot use Test-Connection because it throws exception when server wont reply back or doesnot exist and it takes more time to ping.
Thanks.
Suppose it's too late to be of much help but this should resolve the issues you are reporting with Test-Connection.
Test-Connection -ComputerName $IP -Count 1 -ErrorAction SilentlyContinue
That will limit the ping count to 1 and error will be ignored. Then you can work with the object it produces instead of having to parse lines.