PowerShell: Looping Through a List of Servers and Copying Files

I’m new to Windows PowerShell and have been wanting to learn it for some time now.  I finally had a situation come up the other day that I thought might be a good excuse to sit down and write my first PowerShell script.

The scenario:

I have three different servers running the same application.  There is application data on each server that needs to be backed up and stored in a folder named in a date format but it is not necessarily the date the script is run. 

The folder structure is exactly the same on each server.  I needed a script that could make a new folder to store the backed up data. 

I prompt the user to name the new folder defined on 3 different servers and copy files from another folder to this new folder, essentially creating a backup of data from a root folder to the new folder.

PowerShell Script Example: Looping through a list of servers

#  Script name:    Backup_Data.ps1
#  Created on:     2010-05-14
#  Author:         Rick Cable
#  Purpose:        Backup Files

#new folder name in date format mm-dd-yy
$NewFolderName = "" #Stores the name of the folder 
#Create array of Servers
$Servers = @("Server1","Server2","Server3")
$ServerName = @("Server 1 Description","Server 2 Description","Server 3 Description")

#Prompt user to enter the new folder name
$NewFolderName = read-Host "Enter the name of the new folder in date format mm-dd-yy"

#Function to process the files for each server in the array list.
Function Backup {

#Loop throug each of the servers
for ($i = 0; $i -le $Servers.length -1; $i++) 
{
write-Host $Servers[$i]
$BasePath = "\\" + $Servers[$i] + "\Share\"
$DonePath = $BasePath + "\Backup\" + $NewFolderName + "\"

if (test-Path $DonePath)  'if folder already exists
{        
write-Host "Copying files for" $ServerName[$i]
$FilePath = $BasePath + "*.dfm"
Copy-Item $FilePath $DonePath
write-Host "Copying log files"
$FilePath = $BasePath + "*.log"
Copy-Item $FilePath $DonePath
write-Host "Copying txt files"
$FilePath = $BasePath + "*.txt"
Copy-Item $FilePath $DonePath

write-Host "Files Copied"
} 
else
{
[IO.Directory]::CreateDirectory($DonePath)

write-Host "Copying files for" $ServerName[$i]
$FilePath = $BasePath + "*.dfm"
Copy-Item $FilePath $DonePath
write-Host "Copying log files"
$FilePath = $BasePath + "*.log"
Copy-Item $FilePath $DonePath
write-Host "Copying txt files"
$FilePath = $BasePath + "*.txt"
Copy-Item $FilePath $DonePath

write-Host "Folder Created and Files Copied"
}

}
}

#Run the function   
Backup


Exporting IIS IP Restrictions to a File using a Powershell Script

If you ever find yourself needing to export a list of site or IP restrictions from a Windows based web server running IIS then this is how you do it using Powershell. 

I believe that there is a vbscript that can do this as well but I chose Powershell for this challenge and it worked so why not.
At this point I am assuming you know what Powershell is, have it installed and know how to execute scripts. With that being said.

Create a Powershell script and name the file “export-ip-restrictions.ps1” and paste in the text below.
Execute it and you should have a list of IP restrictions in the same folder as the script you ran.

Powershell Code Example

[string]$SitesVar = Read-Host "Enter Sitenames with no spaces in between, Seperated by ';' i.e site1;site2;site3" 
$exportfolder = Read-Host "Enter Export Folder i.e. C:\folder1" 
 
sl c:\windows\system32\inetsrv\ 
 
$a = 0 
$Sites = $SitesVar.Split(";") 
$count = $Sites.count 
 
do { 
foreach ($site in $Sites){ 
[xml]$out = .\appcmd.exe list config $site -section:system.webServer/security/ipSecurity  
 
$out."system.webserver"."security"."ipsecurity" | %{ 
$_.innerxml.split("<*>") | out-file $exportfolder\$site.txt 
 
} 
$a++ 
} 
} 
while ($a -ne $count) 

Connect to SQL Database and Output data to CSV File from Table using Powershell

This is sample Powershell code for connecting to SQL Database and outputting table data to a CSV file.

Code below was tested by me.

Reference: 
https://stackoverflow.com/questions/25682703/connect-to-sql-server-database-from-powershell

Powershell Code to Connect to SQL Server & Output to CSV File

$SQLServer = "aaaa.database.windows.net"
$SQLDBName = "Database"
$uid ="john"
$pwd = "pwd123"
$SqlQuery = "SELECT * from table;"
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; Integrated Security = True; User ID = $uid; Password = $pwd;"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)

$DataSet.Tables[0] | out-file "C:\Scripts\xxxx.csv"