PowerShell script exchange
Post Reply
HannesK
Product Manager
Posts: 14836
Liked: 3083 times
Joined: Sep 01, 2014 11:46 am
Full Name: Hannes Kasparick
Location: Austria
Contact:

infrastructure deployment (Managed Server + Proxy)

Post by HannesK » 3 people like this post

Hello,
I created some scripts to deploy Veeam infrastructure components.

I created three scripts (maybe I create more if I have time) that should be used as as described:
1) 1_check_host_smb_connection.ps1 - this is a prerequisite script that checks whether the hosts are available
2) 2_add_windows_host_to_managed_servers.ps1 - add Windows hosts do VBR
3) 3_add_proxy_role.ps1 - deploys proxy role

Input is provided via a csv file like this:

Code: Select all

Win2012R2-0001,location1
Win2012R2-0002,location2
Win2008R2-0001,location1
SRV-phy1,location3

I use the description field do allow easy sorting by location as shown in the screenshot

Image


Feel free to comment or change to your needs

Best regards,
Hannes
HannesK
Product Manager
Posts: 14836
Liked: 3083 times
Joined: Sep 01, 2014 11:46 am
Full Name: Hannes Kasparick
Location: Austria
Contact:

1_check_host_smb_connection.ps1

Post by HannesK » 1 person likes this post

Code: Select all

<#

.SYNOPSIS
This script tries to connect to admin$ share to a list of hosts provided by a
csv file.

.DESCRIPTION
Connection to admin$ share is required to install Veeam components. This script
checks whether it is possible to login to admin$.

Adjust $filepath_input and $filepath_output according to your needs.

The CSV file should be in the following format:

    DNS-Hostname,Location

Location is optional

The script was tested on Windows Server 2012R2 with Powershell 4.0 


.NOTES
Date: 08.03.2016

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
THE SOFTWARE.

#>

Set-StrictMode -Version latest


#
# define input files
#
$filepath_input = "C:\scripts\check_host_reachable.csv"
$filepath_error ="C:\scripts\check_host_reachable_errors.txt"


#
# define remote credentials
#
$admin_user = Read-Host 'Pleae provide remote admin user(domain\user):'
$admin_password = Read-Host 'Please provide emote admin password (will not be saved):' -AsSecureString
$credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $admin_user, $admin_password


#
# try to connect to admin$ share on all computers in list
#
$computers = Import-Csv $filepath_input -Header hostname, location

foreach ($computer in $computers) {
    try {
        $hostname = $computer.hostname
        $location = $computer.location

        Write-Host -NoNewline Connecting to host $hostname in $location : -ForegroundColor Yellow
          
        $data = New-PSDrive -Name $hostname -PSProvider FileSystem -Root \\$hostname\admin$ -Credential $credentials -ErrorAction Stop
        Remove-PSDrive $hostname
                
        Write-Host " connection to admin$ share successful" -ForegroundColor Green
    } # try
    catch {
        Write-Host " connection failed" -ForegroundColor Red
        $_ | Out-File -Append  $filepath_error
    } #catch
} #foreach

Write-Host "Details available in $filepath_error"

HannesK
Product Manager
Posts: 14836
Liked: 3083 times
Joined: Sep 01, 2014 11:46 am
Full Name: Hannes Kasparick
Location: Austria
Contact:

2_add_windows_host_to_managed_servers.ps1

Post by HannesK » 1 person likes this post

Code: Select all

<#

.SYNOPSIS
This script adds Windows hosts to Veeam Backup and Replication managed servers
provided by a csv file.

.DESCRIPTION
The credentials must be saved in Veeam credentials manager before running this
script

Adjust 
     - $filepath_input 
     - $filepath_error 
     - $admin_user
     - description field
according to your needs.

The CSV file must be in the following format:

    DNS-Hostname,Location

The Location is used in the description that you can easily sort by location

The script was tested on Windows Server 2012R2 with Powershell 4.0 

.NOTES
Date: 08.03.2016

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
THE SOFTWARE.

#>



Set-StrictMode -Version latest
Add-PSSnapin VeeamPSSnapin


#
# define input files
#
$filepath_input = "C:\scripts\check_host_reachable.csv"
$filepath_error ="C:\scripts\add_windows_hosts_to_managed_servers_error.txt"


#
# password ist used from Veeam Credentials Manager
#
$admin_user = "veeamlab\administrator"
$credentials = Get-VBRCredentials -Name $admin_user


#
# connect  to computers in csv and add them as managed windows servers in VBR
#

$computers = Import-Csv $filepath_input -Header hostname, location
foreach ($computer in $computers) {
    try {
        $hostname = $computer.hostname
        $location = $computer.location
        $date = Get-Date -Format "yyyy-MM-dd hh:mm"
        
        Write-Host -NoNewline Trying to add $hostname in $location to manged servers : -ForegroundColor Yellow
        
        # adjust description to your needs
        $params = @{'Name'        = $hostname 
                    'Credentials' = $credentials
                    'Description' = "$location server added by $env:USERDNSDOMAIN\$env:USERNAME at $date" 
                    }
        $data = Add-VBRWinServer @params -ErrorAction Stop


        Write-Host " successfuly added" -ForegroundColor Green
        
    } #try
    catch {
        Write-Host " failed! see $filepath_error logfile for details" -ForegroundColor Red
        $_ | Out-File -Append  $filepath_error
    } #catch
} #foreach



Write-Host "Details available in $filepath_error"

HannesK
Product Manager
Posts: 14836
Liked: 3083 times
Joined: Sep 01, 2014 11:46 am
Full Name: Hannes Kasparick
Location: Austria
Contact:

3_add_proxy_role.ps1

Post by HannesK » 1 person likes this post

Code: Select all

<#
.SYNOPSIS
This script adds the proxy role to servers provided by a csv file

.DESCRIPTION
Make sure that the servers are already "Managed servers" in Veeam Backup
and Replication.

Adjust 
     - $filepath_input 
     - $filepath_error 
     - description field
according to your needs.


The CSV file should be in the following format:

    DNS-Hostname,Location

The Location is used in the description that you can easily sort by location

The script was tested on Windows Server 2012R2 with Powershell 4.0 


.NOTES
Date: 08.03.2016

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
THE SOFTWARE.

#>




Set-StrictMode -Version latest
Add-PSSnapin VeeamPSSnapin


#
# define input files
#
$filepath_input = "C:\scripts\list_proxy.csv"
$filepath_error ="C:\scripts\add_proxy_error.txt"
$computers = Import-Csv $filepath_input -Header hostname, location



#
# deploy proxy role to hosts in csv 
# checks how many cores are available and sets max tasks number to that value
#

foreach ($computer in $computers) {
    try {
        $hostname = $computer.hostname
        $location = $computer.location
        $date = Get-Date -Format "yyyy-MM-dd hh:mm"

                
        Write-Host -NoNewline  Getting number of CPU Cores of $hostname in $location : -ForegroundColor Yellow
        # use Get-WmiObject instead of Get-CimInstance for Server 2008R2 compatibility
        $cores = Get-WmiObject -ComputerName $hostname -ClassName Win32_Processor | measure NumberOfCores -sum -ErrorAction Stop
        Write-Host $cores.sum -ForegroundColor Green
        
        Write-Host -NoNewline Trying to add Proxy role to $hostname in $location : -ForegroundColor Yellow

        # adjust description to your needs
        $params = @{'Server'    = $hostname
                    'MaxTasks'  = $cores.sum
                    'Description' = "$location proxy added by $env:USERDNSDOMAIN\$env:USERNAME at $date" 
                    }      
        $data = Add-VBRViProxy @params -ErrorAction Stop
        

        Write-Host "Proxy role successfuly added" -ForegroundColor Green
        
    } #try
    catch {
        Write-Host " failed! see $filepath_error logfile for details" -ForegroundColor Red
        $_ | Out-File -Append  $filepath_error
    } #catch
} #foreach

Post Reply

Who is online

Users browsing this forum: No registered users and 16 guests