Get public IP on a Windows Server via PowerShell

20160614150939Every so often I need a quick way of getting the public IP of a Windows server. There are several ways of doing this but my preferred method leverages PowerShell, specifically the Invoke-WebRequest CMDLET. It sends HTTP, HTTPS, FTP, and FILE requests to a web page or service and responds with elements that you have configured it to capture such as links, images etc.

To obtain the public IP via PowerShell run the simple one-liner below:

PS C:\> (Invoke-WebRequest -uri "http://ipecho.net/plain").Content

Running this command will successfully show your public IP:

20170529132213

There are numerous other services you can call to get the public IP, a few are below:

http://ipinfo.io/ip
http://icanhazip.com
http://ident.me
http://smart-ip.net/myip

Simply replace http://ipecho.net/plain in the PowerShell command above with one of the URLs above and you should get your public IP back in exactly the same way.

References:
Invoke-WebRequest

PowerShell script to copy file to multiple remote servers

20160614150939I often have to copy files and folders from one server to another for various reasons. Copying stuff manually is a bore but if you are only copying to a couple of servers it is quicker to do it manually but if you have to copy to several servers it would be quicker to use a script. We can leverage PowerShell to perform this task.

We will use the Copy-Item cmdlet in PowerShell. It will copy  file and folders to a remote server using the following syntax:

Copy-Item "source" -Destination "\\server\C$"

Please note that the user you are running the commands under must also exist and work on the servers you are copying the file(s) to. All the examples in this post were performed in a test domain environment using a Domain Admin account but as long as your user has sufficient rights the commands should work fine.

Let us use an example to demonstrate how Copy-Item works. I want to copy the EMC Networker SQL agent NWVSS.exe from server vs11vdc01 to server vs11app01. To do so, I would run:

Copy-Item "C:\Software\EMC\Networker\NWVSS.exe -Destination \\vs11app01\c$\temp

As you can see from the above, I have stated the full path of the NWVSS.exe file and the destination it is going to, in this case \\vs11app01\c$\temp. Running  results in a successful file copy:

20160614132832

To copy to another server you could use the same command and change the destination accordingly but that wouldn’t good enough for the Japinator. I prefer a slightly more sophisticated solution – we can use the Get-Content cmdlet or gc for short to get the contents of a text file called servers.txt that contains a list of the servers you want to copy to. This text file simply contains a list of servers, each on a new line per the below:

20160614152653

We can pass each line of the contents of servers.txt as variables to the Copy-Item cmdlet to perform the copies to those servers. The script below will achieve this:

# This file contains the list of servers you want to copy files/folders to
$computers = gc "C:\scripts\servers.txt"

# This is the file/folder(s) you want to copy to the servers in the $computer variable
$source = "C:\Software\EMC\Networker\NWVSS.exe"

# The destination location you want the file/folder(s) to be copied to
$destination = "C$\temp\"

#The command below pulls all the variables above and performs the file copy
foreach ($computer in $computers) {Copy-Item $source -Destination "\\$computer\$destination" -Recurse}

The file copies successfully to all servers in C:\scripts\servers.txt successfully, below we see the file on vs11app02:

20160614141733

To make it a little but more sophisticated we will add a check to confirm that the remote servers’ path exists using the Test-Path cmdlet before performing the copy. If the path does not exist it will state that it “is not reachable or does not exist”:

# This file contains the list of servers you want to copy files/folders to
$computers = gc "C:\scripts\servers.txt"

# This is the file/folder(s) you want to copy to the servers in the $computer variable
$source = "C:\Software\EMC\Networker\NWVSS.exe"

# The destination location you want the file/folder(s) to be copied to
$destination = "C$\temp\"

foreach ($computer in $computers) {
if ((Test-Path -Path \\$computer\$destination)) {
Copy-Item $source -Destination \\$computer\$destination -Recurse
} else {
"\\$computer\$destination is not reachable or does not exist"
}
}

I hope this is useful to you and saves you time and energy! So kick this script off, get a coffee and put your feet up.

References:
Copy-Item
Get-Content
Test-Path
PowerShell : Copy File(s) TO / FROM remote Servers