MSSQL Remote Installation using Powershell

I have 2 servers, with 2008 R2 Server installed.One is my AD and the other I want to install MSSQL Server remotely from AD server. So I am pasting the script for both machine. Error which I am getting is “UNHANDLED EXCEPTION : OUTOFMEMORYEXCEPTION”

AD SERVER SCRIPT(Motive to run the script in SERVER 2 ) :

$service_account_name=“Autoim\Administrator”
$service_account_password=“pass@word3”
$mssql_server_ip=“192.168.14.175”
$SecurePassWord = ConvertTo-SecureString -AsPlainText $service_account_password -Force
$Cred = New-Object -TypeName “System.Management.Automation.PSCredential” -ArgumentList $service_account_name,$SecurePassWord

write-host “Taking Session of MSSQL Machine”
$Session = New-PSSession -ComputerName $mssql_server_ip -Credential $Cred

Invoke-Command -Session $Session -ScriptBlock {Get-Content “\192.168.14.175\SQL\Install_SQL_Config_File.ps1”} -Authentication CredSSP -Credential $Cred

Remove-PSSession -session $Session


SERVER2 SCRIPT(Contains MSSQL Configuration and Install script)

##########################[ INITIALIZATION ]############################

[string]$instance=“MSSQLSERVER”
$ServiceAccountName=“Autoim\Administrator”
$ServiceAccountPassword=“pass@word3”
$saPassword=“pass@word3”
$sourceDir="C:\SQL"
$instDrive=“C”
$userDbDrive=“D”
$userLogDrive=“D”
$tempDbDrive=“E”
$tempLogDrive=“E”
$port=1433
$backup_directory=“G”
$collation=“Latin1_General_CI_AS”

#######[ MSSQL Feature ]####

$SQLEngine=“Yes”
$Replication=“Yes”
$Fulltext=“Yes”
$BIDS=“Yes”
$IS=“Yes”
$SSMS=“Yes”
$CONN=“Yes”
$BC=“Yes”
$ADV_SSMS=“Yes”
$SNAC_SDK=“Yes”
$AS=“Yes”
$RS=“Yes”

if($SQLEngine -eq “yes”) { $SQLEngine=“SQLENGINE” }else{ $SQLEngine=$null }
if($Replication -eq “yes”) { $Replication=“Replication” }else{ $Replication=$null }
if($Fulltext -eq “yes”) { $Fulltext=“FULLTEXT” }else{ $Fulltext=$null }
if($BIDS -eq “yes”) { $BIDS=“BIDS” }else{ $BIDS=$null }
if($IS -eq “yes”) { $IS=“IS” }else{ $IS=$null }
if($SSMS -eq “yes”) { $SSMS=“SSMS” }else{ $SSMS=$null }
if($CONN -eq “yes”) { $CONN=“CONN” }else{ $CONN=$null }
if($BC -eq “yes”) { $BC=“BC” }else{ $BC=$null }
if($ADV_SSMS -eq “yes”) { $ADV_SSMS ="ADV_SSMS " }else{ $ADV_SSMS=$null }
if($SNAC_SDK -eq “yes”) { $SNAC_SDK=“SNAC_SDK” }else{ $SNAC_SDK=$null }
if($AS -eq “yes”) { $AS=“AS” }else{ $AS=$null }
if($RS -eq “yes”) { $RS=“RS” }else{ $RS=$null }

$A=“FEATURES=$SQLEngine,$Replication,$Fulltext,$BIDS,$IS,$SSMS”

if($a.Contains(“,”)) { $A=$a.Replace(“,”,“,”)}

#FEATURES=SQLENGINE,REPLICATION,FULLTEXT,BIDS,CONN,BC,SSMS,ADV_SSMS,SNAC_SDK,AS,RS

################[ INSTALLING PREREQUISITE ]######################

function installPrereqs ()
{
Import-Module ServerManager
Add-WindowsFeature Application-Server,AS-NET-Framework,NET-Framework,NET-Framework-Core,WAS,WAS-Process-Model,WAS-NET-Environment,WAS-Config-APIs
}

##################[ CONFIGURATION FILE ]#############################

function prepareConfigFile ([String]$instance, $instDrive, $userDbDrive, $userLogDrive, $tempDbDrive, $tempLogDrive, $collation , $backup_directory ) {
$config = “[SQLSERVER2008]
ACTION=”“Install”"
$A
INSTANCENAME=“”$instance""
INSTANCEID=“”$instance""
INSTALLSHAREDDIR=““C:\Program Files\Microsoft SQL Server””
INSTALLSHAREDWOWDIR=““C:\Program Files (x86)\Microsoft SQL Server””
INSTANCEDIR=““C:\Program Files\Microsoft SQL Server””
INSTALLSQLDATADIR=“”“+$instDrive+”:\MSSQL$instance""
SQLUSERDBDIR=“”“+$userDbDrive+”:\MSSQL$instance\MSSQL10_50.“+$instance+”\MSSQL\Data""
SQLUSERDBLOGDIR=“”“+$userLogDrive+”:\MSSQL$instance\MSSQL10_50.“+$instance+”\MSSQL\Tlog""
SQLTEMPDBDIR=“”“+$tempDbDrive+”:\MSSQL$instance\MSSQL10_50.“+$instance+”\MSSQL\Data""
SQLTEMPDBLOGDIR=“”“+$tempLogDrive+”:\MSSQL$instance\MSSQL10_50.“+$instance+”\MSSQL\Tlog""
SQLBACKUPDIR=“”“+$backup_directory+”:\MSSQL$instance_name\MSSQL10_50.“+$instance_name+”\MSSQL\Backup""
FILESTREAMLEVEL=““0"”
TCPENABLED=”“1"”
NPENABLED=““1"”
SQLSVCACCOUNT=”“$ServiceAccountName”"
SQLSVCSTARTUPTYPE=““Automatic””
AGTSVCACCOUNT=“”$ServiceAccountName""
AGTSVCSTARTUPTYPE=““Automatic””
ISSVCACCOUNT=““NT AUTHORITY\NetworkService””
ISSVCSTARTUPTYPE=““Automatic””
BROWSERSVCSTARTUPTYPE=““Automatic””
SQLSYSADMINACCOUNTS=“”$ServiceAccountName""
SECURITYMODE=““SQL””
SQMREPORTING=““False””"

$config
}

$instance = $instance.ToUpper()
$environ = $instance.Substring($instance.length-1)
$hostName = get-content env:computername

#######################[ Creating Ini File ]##############################

$workDir = pwd

“Creating Ini File for Installation…”
$configFile = “C:\sql2008_”+$instance+“_install.ini”

prepareConfigFile $instance $instDrive $userDbDrive $userLogDrive $tempDbDrive $tempLogDrive $collation $backup_directory | Out-File $configFile

"Configuration File written to: "+$configFile

################[ Installing Prerequisites ]##############################

“Installing Prerequisites (.Net, etc) …”
installPrereqs

set-location $sourceDir

“Starting SQL 2008 Base Installation…”
$installCmd = “C:\SQL\setup.exe /qs /SQLSVCACCOUNT=”“$ServiceAccountName”" /SQLSVCPASSWORD=“”$ServiceAccountPassword"" /SQLSYSADMINACCOUNTS=“”$ServiceAccountName"" /AGTSVCACCOUNT=“”$ServiceAccountName"" /AGTSVCPASSWORD=“”$ServiceAccountPassword"" /SAPWD=“”$saPassword"" /ConfigurationFile=“”$configFile"“”

Invoke-Expression $installCmd
set-location $workDir

##################[ END-OF-SCRIPT ]###########################

Remote shells usually have a memory limit applied in the WinRM configuration - you can see it under the WinRM: drive, under the Server path on the remote machine. I’m guessing the SQL Server installer is exceeding the memory limit, and so you may need to adjust it.