Quick hit – Server and Site Ips


Recently I needed to quickly get server IPS and Website addresses.  So I put together a couple line script to do this.  This post is about how that works:

Since I need to get these remotely I’m going to use a PowerShell Session . So I’ll first create my variable to hold the servers i need:

$servers = 'server1','server2','server3','server4'

Now that i have a variable with servers in it I can send this to new-pssesion and use the variable I setup above.

$session = new-pssession -computername $servers

My $sesion variable contains a session to each server that I want to Invoke-Command on.  So now I need to come up with a way to get the IPS for every site I have setup in IIS and all the ips for the server I’m calling. If I do this locally I can use this script

import-module webadministration ;(Get-Website) | Select-Object name`
 ,@{Name='bindings' expression= `
{($_.bindings.collection.bindingInformation -replace ':\d+','').trim(':') }}}

I’m taking the values from Get-website  and selecting the name bindings and alson the bindings.collection binding information and removing the Port from the binding number. Return results look like this:

name : MySite
bindings : 10.10.10.39

Now all I need is to find out how to get my ip address. I can do this by using the command Get-Netipaddress if I enclose the function I can get at the property:

PS PS:\iis> (get-netipaddress).IPAddress
::1
10.10.10.1
127.0.0.1

Now to put it together in a single script:

 $servers = 'server1','server2','server3','server4'
 $sess = new-pssession -ComputerName $servers -Credential $admCredentials
 invoke-command -Session $sess {import-module webadministration ;(Get-Website) | Select-Object name ,@{Name='bindings' 
 expression={($_.bindings.collection.bindingInformation -replace ':\d+','').trim(':') }}}
 invoke-command -session $sess { $env:computername;(get-netipaddress).IPAddress}

name : website1
bindings : 10.10.10.48
PSComputerName : server1
RunspaceId : 91f9523f-58df-49d5-a0b1-064101822aae

name : websiteapi
bindings : 10.10.10.49
PSComputerName : server1
RunspaceId : 91f9523f-58df-49d5-a0b1-064101822aae

name : website1
bindings : 10.10.10.50
PSComputerName : server2
RunspaceId : 3cd44533-d827-4a60-bdcc-6c91e77d96b9

name : websiteapi
bindings : 10.10.10.51
PSComputerName : server2
RunspaceId : a90c36ca-630e-42a8-abcd-069e8cec5360

server1
server2
::1
::1
::1
10.10.10.48
10.10.10.49
10.10.10.40
10.10.10.51

I hope this helps someone out. .

 

 

Until then keep Scripting

 

thom

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s