Hi,
I am using below powershell script to disable Shared Memory Protocol 'SM' in Sql Server
Import-Module"sqlps"-DisableNamechecking
######################################################
# Function to Enable or Disable a SQL Server Network Protocol
#
#
###############################
function ChangeSQLProtocolStatus($protocol,$enable){
try
{
[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")
[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlWmiManagement")
}
catch{throw"Exception
occurred when we refer smo and SqlWmiManagement assembly, and its $_.Exception"}
try
{
Write-Host"Initialize
WMI object"
$wmi=new-object('Microsoft.SqlServer.Management.Smo.WMI.ManagedComputer')
Write-Host"Generation
URI"
$uri = "ManagedComputer[@Name='" + (get-item env:\computername).Value + "']/ServerInstance[@Name='MSSQLSERVER']/ServerProtocol"
Write-Host"Creating
protocol settings"
$prtocolSettings = $wmi.getsmoobject($uri + "[@Name='$protocol']")
Write-Host"Created
Protocol smo object"
$prtocolSettings.IsEnabled=$enable
$prtocolSettings.Alter()
try
{
[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")
[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlWmiManagement")
Get-Service-Name"MSSQLSERVER"|Restart-Service-Force#Restart
SQL Services
$logstring="Restarting
sql service"
$mc=New-Object-TypeNameMicrosoft.SqlServer.Management.Smo.Wmi.ManagedComputer
$svc=$mc.Services["MSSQLSERVER"]
#Stop this service
$svc.Stop()
$svc.Refresh()
while($svc.ServiceState-ne"Stopped")
{
$svc.Refresh()
$svc.ServiceState
}
#Start this service
$svc.Start()
$svc.Refresh()
while($svc.ServiceState-ne"Running")
{
$svc.Refresh()
$svc.ServiceState
}
}
catch
{
throw"Starting
Sql server service failed in server $_.Exception"
}
}
catch
{
$error[0]
}
}
Above script is running successfully in powershell version 4.0.But when I run that script in Powershell version 2.0,It is throwing below error
Exception calling "GetSmoObject" with "1" argument(s):"Attempt to retrieve data for object failed for ManagedComputer 'txdsepsn123'."
$ERROR[0] | FORMAT-LIST -FORCE
Detailed error given below
Exception : System.Management.Automation.MethodInvocationException: Exception calling "GetSmoObject" with "1" argument(s):
"Attempt to retrieve data for object failed for ManagedComputer 'txdsepsn123'." ---> Microsoft.SqlServer.Man
agement.Smo.FailedOperationException: Attempt to retrieve data for object failed for ManagedComputer
'txdsepsn123'. ---> Microsoft.SqlServer.Management.Smo.MissingObjectException: The ServerInstance 'MSSQLSERVER'
does n
ot exist on the server.
at Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer.GetSmoObjectRec(Urn urn)
at Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer.GetSmoObjectRec(Urn urn)
at Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer.GetSmoObject(Urn urn)
--- End of inner exception stack trace ---
at Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer.GetSmoObject(Urn urn)
at GetSmoObject(Object , Object[] )
at System.Management.Automation.MethodInformation.Invoke(Object target, Object[] arguments)
at System.Management.Automation.DotNetAdapter.AuxiliaryMethodInvoke(Object target, Object[] arguments,
Metho
dInformation methodInformation, Object[] originalArguments)
--- End of inner exception stack trace ---
at System.Management.Automation.StatementListNode.ExecuteStatement(ParseTreeNode statement, Array input,
Pip
e outputPipe, ArrayList& resultList, ExecutionContext context)
at System.Management.Automation.StatementListNode.Execute(Array input, Pipe outputPipe, ArrayList&
resultLis
t, ExecutionContext context)
at System.Management.Automation.TryStatementNode.Execute(Array input, Pipe outputPipe, ArrayList&
resultList
, ExecutionContext context)
TargetObject :
CategoryInfo : NotSpecified: (:) [], MethodInvocationException
FullyQualifiedErrorId : DotNetMethodException
ErrorDetails :
InvocationInfo : System.Management.Automation.InvocationInfo
PipelineIterationInfo : {}
PSMessageDetails :
How to resolve this issue?