Showing posts with label ForEach-Object. Show all posts
Showing posts with label ForEach-Object. Show all posts

Tuesday, April 14, 2015

Generate the list of IP-Addresses using PowerShell Range operator.

 

main

PowerShell Range Operator.

I was busy in writing my book on "Automating Microsoft Azure with PowerShell" that's why was not able to write blogs in previous months. I am hoping that you guys missed me ;o) . In-case if you didn't missed me, that's fine too..

Yesterday our own  "A! Murky Ana" call me late night ( she called me after a long time, and not sure where she was been, anyway...) and asked me that if it s feasible to generate  a list of address using PowerShell.

I said , YES, it is feasible, it is just a single line of code to achieve that task. You need to use the "PowerShell Range Operator". PowerShell range operator is mentioned by double dots (..)

The syntax is simple, write the range in which you want to start, this should be integer and then type two dots and the end of the range integer.

For example, if you want to create a range of numbers from 1 to 10, then you just need to type 1..10 and it will generate the list of numbers started from 1 till 10.

1

Now, let's talk about generating IP-Addresses using PowerShell. To generate a range of PowerShell IP-Addresses, we need to use two commands, one the is the range operator, and second is the Foreach-Object , and off course the pipe too, to join both commands.

1..250 | ForEach-Object {"192.168.1.$_"}

 

RangeOperator

 

In above command, we are first generating a list of range of numbers from 1-to-250 , and the we are piping the output to the Foreach-Object cmdlet, and in for each script block we are looping through every number, and then Concatenation to 192.168.1.$_ , the $_ variable is replacing by the value of the integer.

 

Simple... ;o) . No? by using a  single line of code, we have generated a list of Ip-Addresses.

 

 

Check the video of PowerShell Range Operator

 

 

tumblr_njiml4gOlY1rku136o1_500

 

* All characters appearing in this work are fictitious Any resemblance to real persons, living or dead, is purely coincidental.

 

Regards.

Aman Dhally
If you like, you can follow me on Twitter and Facebook. You can also check my “You Tube channel for PowerShell video tutorials. You can download all of my scripts from “Microsoft TechNet Gallery”.

Wednesday, March 20, 2013

Powershell Script: Export EFS Certificates using Powershell.

Hi,
In my previous Article i post about how to create encrypted folder using powershell script. In encryption the most important thing is the backup of encryption certificate. We can export and save encryption certificate manually from control panel , using MMC and Certificate Snap-in or using cipher.exe.
But as we all love to use scripts , we can use a powershell scrip to export efs security certificate.

Download Link : http://gallery.technet.microsoft.com/scriptcenter/Export-EFS-Certificates-8ec8ba74

You can download the script and run it, this script will export the EFS Security Certificate to your “My Document” or “Document Folder”.
20-03-2013 00-04-05
I did not write this script, i have found it somewhere on the internet and lost is original source, and i also tweaked this script according to my need.

Download Link  :http://gallery.technet.microsoft.com/scriptcenter/Export-EFS-Certificates-8ec8ba74

Thanks
Aman Dhally
clip_image001 clip_image002 clip_image003 clip_image005clip_image007

Tuesday, September 18, 2012

Get Network Information of local and remote Computers using PowerShell.

Hi,

Last Sunday one of my friend come to visit me at my home, he is a SQL Administrator and we were talking about Powershell, and he told me that he is facing a problem, he said that in their environment they have approx. 500 Servers and they want to know the network configuration of all of them and specially they needed the SUBNET mask information, because their SUBNETMASK are based on geographic locations.

For him i wrote a simple script which can query multiple server using WMI.
You can download the script from this link :

Download Link  :http://gallery.technet.microsoft.com/scriptcenter/Get-Network-Information-of-6d07766f 

18-09-2012 12-52-23
You need to add computer names separated by commas in the front of the script.

Make sure that you run this script as ADMINISTRATOR if you want to run this script for remote computers.

Download Link  :http://gallery.technet.microsoft.com/scriptcenter/Get-Network-Information-of-6d07766f

Thanks
Aman Dhally
join aman on facebook Join aman on Linkedin follow aman on Twitter

Friday, July 6, 2012

Test Connectivity of Multiple Server and then do "Trace Route" on failed servers using powershell script.


Hi,

Yesterday one of our "New Delhi PowerShell User Group" member was trying to do the following.

Ping a list of Internet and External Server, and then do the "Trace Route" on the failed ones.  He was facing a problem using "foreach" cmdlets.

to help him i tried to write a script for him and he liked it .. So i think that i should share this script with everyone so that everyone else can be benefitted.

The Script is very easy, you can download the script form here:

Download Link : http://gallery.technet.microsoft.com/scriptcenter/Ping-Multiple-Servers-and-df50cd28

This script  doing  Test-Connection to the all Computers in $servers and if test failes then it does 
trace route to that Server. 

Screenshot of the Script.

06-07-2012 12-05-56

Download Link : http://gallery.technet.microsoft.com/scriptcenter/Ping-Multiple-Servers-and-df50cd28

Thanks for reading.


Thanks!

Aman Dhally

Buy-More-Twitter-Followers  4fb29548b6adc

00336yft

Wednesday, April 4, 2012

IP Address Release, Renew using PowerShell.


Hi,

This is a very common task which is nearly performed by every End-user and IT Admin and Engineers. Normally we use "IPCONFIG /Release" to release the IP-address and "IPconfig / Renew" to renew or getting a new IP Address.

In some powershell script you may need to do this ip config /release and renew task. We can use native "IPconfig" command to achieve this. But there is an another way to do the same. Using WMI.
Download the script from here :

In variable $ethernet we are querying class Win32_NetworkAdapterConfiguration and using where-Object cmdlet we are choosing those adapter which have IP Enabled and have DHCP Enabled selected in there IP Configuration

than on each adapter we are using wmi method ReleaseDHCPLease()  first and then RenewDHCPLease() .

Download the script from here: http://gallery.technet.microsoft.com/scriptcenter/Renew-IP-Adresses-Using-365f6bfa

################################
$ethernet = Get-WmiObject -Class Win32_NetworkAdapterConfiguration | Where { $_.IpEnabled -eq $true -and $_.DhcpEnabled -eq $true}  
foreach ($lan in $ethernet) {
       Write-Host "Flushing IP addresses" -ForegroundColor Yellow
       Sleep 2
       $lan.ReleaseDHCPLease() | out-Null
       Write-Host "Renewing IP Addresses" -ForegroundColor Green
       $lan.RenewDHCPLease() | out-Null
       Write-Host "The New Ip Address is "$lan.IPAddress" with Subnet "$lan.IPSubnet"" -ForegroundColor Yellow
       }
#############################################
Download the script from here: http://gallery.technet.microsoft.com/scriptcenter/Renew-IP-Adresses-Using-365f6bfa

30-03-2012 15-41-48

I hope that it may help someone.

Thanks

Aman Dhally

Monday, January 9, 2012

Finding Standard Parameter Names in PowerShell

 

PowerTip of the Day, from PowerShell.com:

 

In a previous tip, we suggested you to use standard parameter names for your own functions. To get a feeling for what the parameter names are that built-in cmdlets use, here is some code that creates a list of them:

PS> Get-Command -CommandType Cmdlet | Select-Object -ExpandProperty Parameters |  ForEach-Object { $_.Keys } | Group-Object -NoElement | Sort-Object Count, Name -Descending

(...)

   68 Force

   67 Name

   54 InputObject

   54 Credential

   52 Path

   44 PassThru

(...)

Retrieve Exchange Rates using PowerShell

 

PowerTip of the Day, from PowerShell.com:

If you need up-to-date exchange rates, try loading the rates via XML from the European Central Bank. This sample gets you the latest exchange rates for USD-EUR conversion. It also has a link to other currency data. Just exchange the data link. We included a sample for Danish currency as well.

#Exchangerate feeds : http://www.ecb.int/home/html/rss.en.html

$url = 'http://www.ecb.int/rss/fxref-usd.html'

#$url = 'http://www.ecb.int/rss/fxref-dkk.html'

$xml = New-Object xml

$xml.Load($url)

$xml.RDF.Item |

      ForEach-Object {

            $rv = 1 | Select-Object Date, Currency, Rate, Description

            $rv.Date = [DateTime]$_.Date

            $rv.Description = $_.description.'#text'

            $rv.Currency = $_.statistics.exchangeRate.targetCurrency

            $rv.rate = $_.statistics.exchangeRate.value.'#text'

            $rv

      }

And this is what the output looks like (provided you have Internet access and no proxy is required):

Date                                          Currency Rate       Description

----                                          -------- ----       -----------

01.12.2011 14:15:00 USD               1.3492 1 EUR buys 1.3492 US dollar (USD) - The
...

30.11.2011 14:15:00 USD               1.3418 1 EUR buys 1.3418 US dollar (USD) - The
...

29.11.2011 14:15:00 USD               1.3336 1 EUR buys 1.3336 US dollar (USD) - The
...

28.11.2011 14:15:00 USD               1.3348 1 EUR buys 1.3348 US dollar (USD) - The
...

25.11.2011 14:15:00 USD               1.3229 1 EUR buys 1.3229 US dollar (USD) - The
...

 

Thanks