Quantcast
Channel: SQL Server Database Engine forum
Viewing all articles
Browse latest Browse all 12963

Setting TCP port for Named Instance using powershell on Win2012 with SQLServer2008R2

$
0
0

Hi All,

I am trying to configure TCP port of named instance in win 2012 server (SQL Server 2008 r2 installed).

when i run below PS code got the error. can someone help to rectify the issue.

#$instanceName = "SQL2008R2"
$computerName = gc env:computername

$portNumber = "56545"

$instanceName = "SQL2008R2"
$SqlVersion = "SQL2008R2"


[system.reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlWmiManagement") | out-null

#Get the ManagedComputer instance and set the protocol properties
$wmi = new-object ("Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer") $computerName
$wmi.ServerInstances["$instanceName"].ServerProtocols["Tcp"].IPAddresses["IPAll"].IPAddressProperties["TcpPort"].value = "$portNumber"
$wmi.ServerInstances["$instanceName"].ServerProtocols["Tcp"].IPAddresses["IPAll"].IPAddressProperties["TcpDynamicPorts"].value = [System.String]::Empty

#We need to commit the changes by calling the Alter method
$wmi.ServerInstances["$instanceName"].ServerProtocols["Tcp"].Alter()

#Verify the results and write them to the log
$curPort = $wmi.ServerInstances["$instanceName"].ServerProtocols["Tcp"].IPAddresses["IPAll"].IPAddressProperties["TcpPort"].value
$curDynPort = $wmi.ServerInstances["$instanceName"].ServerProtocols["Tcp"].IPAddresses["IPAll"].IPAddressProperties["TcpDynamicPorts"].value

if ($portNumber -ne "" -and $portNumber -ne $null)
{
Write-Log -level "Info" -message "The SQL TCP port number is currently set to $curPort"
Write-Log -level "Info" -message "The SQL TCP Dynamic port number is currently set to $curDynPort"
}

Error: - i am getting.

Cannot index into a null array.
At line:1 char:1
+ $wmi.ServerInstances["$instanceName"].ServerProtocols["Tcp"].IPAddresses["IPAll" ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

This is coming when we are trying to set port for named instance: on below line:-

$wmi.ServerInstances["$instanceName"].ServerProtocols["Tcp"].IPAddresses["IPAll"].IPAddressProperties["TcpPort"].value = "$portNumber"

i checked and found there is no name instance is showing when try to display $wmi in powershell.

any help will be grateful . i tested the same code on win2008r2 box and it is running successfully.

 

Varun Jha




Viewing all articles
Browse latest Browse all 12963

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>