问题描述
以下是我在sql中禁用共享内存时使用的代码。它不起作用。任何人都可以看看它。
Param([string] $ serverName = $(Read-HostSQL实例(如server\instance))
)
#装载组件
[reflection.assembly] :: LoadWithPartialName(Microsoft.SqlServer.Smo)
[reflection.assembly] :: LoadWithPartialName(Microsoft.SqlServer.SqlWmiManagement)
函数EnableDisableServerProtocol
{
#溢出的机器,实例名称
$ array = $ serverName.Split(\)
#Write-Host$ serverName
if([String] :: IsNullOrEmpty($ serverName))
{
写错误服务器实例名称无效
返回
}
$ machineName = $ array [0]
if($ array.Length -eq 1)
{
$ instanceName =MSSQLSERVER
}
else
{
$ instanceName = $ array [ 1]
}
#Write-Host$ serverName
$ Services = get-wmiobject -class win32_service -computername $ machineName |其中{$ _。name -like'* SQL *'} | select-object *
$ sname = $ instanceName
#write-host $ sname
#获取托管计算机实例
$ mc = New-Object('Microsoft.SqlServer.Management.Smo .Wmi.ManagedComputer')
#禁用共享内存协议
$ urnSM = New-Object - TypeName'Microsoft.SqlServer.Management.Sdk.Sfc.Urn'-argumentlistManagedComputer [@Name ='$ machineName'] / ServerInstance [@Name ='$ sname'] / ServerProtocol [@ Name ='sm']
#获取协议对象
$ Sm = $ mc.GetSmoObject($ urnSM)
#enable对象上的协议
$ Sm.IsEnabled = $ false
#propagate回到实际服务
$ Sm.Alter()
}
EnableDisableServerProtocol
$ service = get-serviceMS SQLSERVER
restart-service $ service.name -force #Restart SQL Services
Below is the code I am nusing for disabling shared memory in sql. It is not working. can anyone have a look into it.
Param([string]$serverName = $(Read-Host "SQL Instance(like server\instance)")
)
# Load the assemblies
[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")
[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlWmiManagement")
function EnableDisableServerProtocol
{
# Spilt Machine , instance names
$array = $serverName.Split("\")
#Write-Host " " $serverName
if([String]::IsNullOrEmpty($serverName))
{
write-error "Server instance name is not valid"
return
}
$machineName = $array[0]
if($array.Length -eq 1)
{
$instanceName = "MSSQLSERVER"
}
else
{
$instanceName = $array[1]
}
#Write-Host " " $serverName
$Services=get-wmiobject -class win32_service -computername $machineName| where {$_.name -like '*SQL*'}| select-object *
$sname = $instanceName
#write-host $sname
#Get a managed computer instance
$mc = New-Object ('Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer')
# Disable Shared Memory Protocal
$urnSM = New-Object -TypeName 'Microsoft.SqlServer.Management.Sdk.Sfc.Urn' -argumentlist "ManagedComputer[@Name='$machineName']/ServerInstance[@Name='$sname']/ServerProtocol[@Name='sm']"
#Get the protocol object
$Sm = $mc.GetSmoObject($urnSM)
#enable the protocol on the object
$Sm.IsEnabled = $false
#propagate back to actual service
$Sm.Alter()
}
EnableDisableServerProtocol
$service = get-service "MSSQLSERVER"
restart-service $service.name -force #Restart SQL Services
推荐答案
这篇关于禁用共享内存不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!