# Retrieve the default language identifier of the current user. For most languages,
# you use the primary language identifier only to retrieve the text. In Windows XP and
# Windows Server 2003, you use the complete language identifier to retrieve Chinese
# text. In Windows Vista, you use the complete language identifier to retrieve Portuguese text.
function Get-GetLanguageId([String[]] $osVersion)
{
$LANG_PORTUGUESE = 0x16
$LANG_CHINESE = 0x04
$cul = Get-UICulture
$lcid = $cul.LCID
$lcidPrimary = $($lcid -band 0xff)
if (
($LANG_PORTUGUESE -eq $lcidPrimary -and $osVersion[0] -gt 5) -or #Windows Vista and later
($LANG_CHINESE -eq $lcidPrimary -and ($osVersion[0] -eq 5 -and $osVersion[1] -ge 1)) #XP and Windows Server 2003
)
{
return $lcid
}
return $lcidPrimary
}
# Build a hash array of offsets into the counter buffer. Use the index
# values from the performance data queries to access the offsets.
function Get-RegLocalizedPerfCounterNames($computerName = $env:COMPUTERNAME)
{
$path = ""
#get OS version
$osvi = Get-WmiObject -class Win32_OperatingSystem -computerName $computerName
if(($osvi -eq $null) -or ($osvi.Version -eq $null)) {
return $null
}
$osVersion = $osvi.Version.split(".")
#get language ID
$language = "{0:X3}" -f $(Get-GetLanguageId($osVersion))
#if OS windows 7, 8, 2008R2 and 2012
if( $osVersion[0] -ge 6 -and $osVersion[1] -ge 1 )
{
$path = "hklm:SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib\CurrentLanguage"
}
#if OS Windows XP, 2003, Vista, 2008
if( ($osVersion[0] -eq 6 -and $osVersion[1] -eq 0 ) -or ($osVersion[0] -eq 5 -and $osVersion[1] -ge 1 ) )
{
$path = "hklm:SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib\{0}" -f $language
}
#if no data in regestry when return null
$val = Get-ItemProperty -Path $path -Name "Counter" -ErrorAction SilentlyContinue
if (($val -eq $null) -or ($val.Length -eq 0))
{
return $null
}
#return registry values
$hashArray = @{}
for($i=0; $i -lt $val.Counter.Count; $i=$i+2)
{
[UInt32] $key = [UInt32]$val.Counter[$i]
if ($hashArray.ContainsKey($key) -ne $true)
{
$hashArray.Add($key, $val.Counter[$i+1])
}
}
return $hashArray
}
#Returns the performance object name or counter name corresponding to the specified index.
Function Get-PdhLookupPerfNameByIndex([UInt32] $ID, $computerName = $env:COMPUTERNAME)
{
$code = '[DllImport("pdh.dll", SetLastError=true, CharSet=CharSet.Unicode)] public static extern UInt32 PdhLookupPerfNameByIndex(string szMachineName, uint dwNameIndex, System.Text.StringBuilder szNameBuffer, ref uint pcchNameBufferSize);'
if ($rv -eq 0)
{
return $Buffer.ToString().Substring(0, $BufferSize-1)
}
else
{
Throw 'Get-PdhLookupPerfNameByIndex : Unable to retrieve localized name for performance counter ID ({0}) on computer "{1}".' -f $ID, $ComputerName
}
}
function Get-LocalizedPerfCounter($arrayCounters, [String] $format, $paramters)
{
$cul = Get-UICulture
#English language
if( $($cul.LCID -band 0xff) -ne 0x09)
{
#try to get locolized values
if ( $arrayCounters -ne $null)
{
#from regestry
for($i=0; $i -lt $paramters.Count; $i++)
{
[UInt32] $id = [UInt32]$paramters[$i][0]
if ($arrayCounters.ContainsKey($id) -eq $true)
{
$paramters[$i][1] = $arrayCounters[$id]
}
else
{
Throw 'Get-PerfCounter : Unable to retrieve localized name. (Index : {0} Name : {1})' -f $id,$paramters[$i][1]
}
}
}
else
{
#win32 api
for($i=0; $i -lt $paramters.Count; $i++)
{
[UInt32] $id = [UInt32]$paramters[$i][0]
$paramters[$i][1] = Get-PdhLookupPerfNameByIndex $id
}
}
}
#get counter
$counter = $format -f $paramters[0][1], $paramters[1][1]
return (Get-Counter ($counter))
}
#-------------------------------------------------------------------------------
#The function returns service or "Unknown" state
#Input:
# server - compute name
# service - system service name
# InstanceName - sql server instance name
#Output:
# service state or "Unknown" state
function GetServiceState($server, $service, $InstanceName)
{
try {
if ($service -eq "MSSQL") {
$service = "MSSQL`${0}" -f $InstanceName
}
$namespace = "root/cimv2"
$obje = Get-WmiObject -Namespace $namespace -ComputerName $server -Class "win32_service" -ErrorAction SilentlyContinue | where {$_.name -like $service }
if ($obje -ne $null) {
return $obje.State
}
}
catch {
}
return "Unknown"
}
function Main {
param($computerName, $SQL_WMI_NAMESPACE, $serviceName, $sqlInstanceName, $connectionString, $tcpPort, $instanceId)
#
# Prepare MOM API and property bag object
#
$api = New-Object -comObject "MOM.ScriptAPI"
$msg = [Environment]::NewLine
#Looks strange. I'm afraid to remove it. Need to investigate aftermath
if ($serviceName -eq "MSSQL") {
$serviceName = "MSSQL`${0}" -f $sqlInstanceName
}
$msg += "Computer Name = '$computerName' WMI = '$SQL_WMI_NAMESPACE' Service Name = '$serviceName' SQL Instance Name = '$sqlInstanceName'"
$msg += [Environment]::NewLine
#if service is not in running state when exit without any error
$state = GetServiceState $computerName $serviceName $sqlInstanceName
if(($state -ne "Running") -and ($state -ne "Unknown"))
{
if($DEBUG_MODE -eq 1) {
$api.LogScriptEvent($DEBUG_MODULE, $SCRIPT_EVENT_ID, $INFORMATION_EVENT_TYPE, $msg)
}
return
}
#check if connection string valid
$SqlConnection = GetValidatedConnection $computerName $sqlInstanceName 'master' $SQL_WMI_NAMESPACE $connectionString $tcpPort $DEBUG_SA $DEBUG_PWD
#if connection is null when exit the script without error
if($SqlConnection -eq $null )
{
$msg = "The server was not found or was not accessible."
if($DEBUG_MODE -eq 1) {
$api.LogScriptEvent($DEBUG_MODULE, $SCRIPT_EVENT_ID, $INFORMATION_EVENT_TYPE, $msg)
}
return
}