NCPA Doesn't Load SQLServer Powershell Module
Posted: Wed Oct 09, 2019 12:45 pm
Hello,
I wrote a PS1 script that uses the SQLServer module to query the count of records from a single table and report back based on the results. I am using NCPA installed on the target SQL Server to run the script. When I run it directly from the NCPA\Plugins directory, the script works as expected and gives me the count of records. However, when I set up the check in Nagios it throws an error that is not able to load the SQLServer module because no valid module was found in the module directory. It doesn't seem to make a difference if I import the module by name or using the full path to the file. I'm also getting that the server principal NT AUTHORITY\\SYSTEM doesn't have access to the database, but I'm not sure if this is because it's failing to load the module or if it's a separate unrelated issue. The full output of the check is below with the appropriate redaction.
Any assistance is greatly appreciated.
File returned contained:
{
"returncode": 3,
"stdout": "Import-Module : The specified module 'SQLServer' was not loaded because no \
valid module file was found in any module directory.\
At C:\\\\Program Files (x86)\\\\Nagios\\\\NCPA\\\\plugins\\\\myscript.ps1:19 \
char:1\
+ Import-Module SQLServer\
+ ~~~~~~~~~~~~~~~~~~~~~~~\
+ CategoryInfo : ResourceUnavailable: (SQLServer:String) [Import- \
Module], FileNotFoundException\
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Comm \
ands.ImportModuleCommand\
\
Invoke-SqlCmd : The server principal \\"NT AUTHORITY\\\\SYSTEM\\" is not able to \
access the database \\"mydatabase\\" under the current security context.\
At C:\\\\Program Files (x86)\\\\Nagios\\\\NCPA\\\\plugins\\\\myscript.ps1:24 \
char:18\
+ $indexRequests = Invoke-SqlCmd -ServerInstance $sqlServer -Query $sqlCommand\
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\
+ CategoryInfo : InvalidOperation: (:) [Invoke-Sqlcmd], SqlPowerS \
hellSqlExecutionException\
+ FullyQualifiedErrorId : SqlError,Microsoft.SqlServer.Management.PowerShe \
ll.GetScriptCommand\
\
UNKNOWN script state"
}
Import-Module : The specified module 'SQLServer' was not loaded because no
valid module file was found in any module directory.
At C:\\Program Files (x86)\\Nagios\\NCPA\\plugins\\myscript.ps1:19
char:1
+ Import-Module SQLServer
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (SQLServer:String) [Import-
Module], FileNotFoundException
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Comm
ands.ImportModuleCommand
Invoke-SqlCmd : The server principal "NT AUTHORITY\\SYSTEM" is not able to
access the database "mydatabase" under the current security context.
At C:\\Program Files (x86)\\Nagios\\NCPA\\plugins\\myscript.ps1:24
char:18
+ $indexRequests = Invoke-SqlCmd -ServerInstance $sqlServer -Query $sqlCommand
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Invoke-Sqlcmd], SqlPowerS
hellSqlExecutionException
+ FullyQualifiedErrorId : SqlError,Microsoft.SqlServer.Management.PowerShe
ll.GetScriptCommand
UNKNOWN script state
I wrote a PS1 script that uses the SQLServer module to query the count of records from a single table and report back based on the results. I am using NCPA installed on the target SQL Server to run the script. When I run it directly from the NCPA\Plugins directory, the script works as expected and gives me the count of records. However, when I set up the check in Nagios it throws an error that is not able to load the SQLServer module because no valid module was found in the module directory. It doesn't seem to make a difference if I import the module by name or using the full path to the file. I'm also getting that the server principal NT AUTHORITY\\SYSTEM doesn't have access to the database, but I'm not sure if this is because it's failing to load the module or if it's a separate unrelated issue. The full output of the check is below with the appropriate redaction.
Any assistance is greatly appreciated.
File returned contained:
{
"returncode": 3,
"stdout": "Import-Module : The specified module 'SQLServer' was not loaded because no \
valid module file was found in any module directory.\
At C:\\\\Program Files (x86)\\\\Nagios\\\\NCPA\\\\plugins\\\\myscript.ps1:19 \
char:1\
+ Import-Module SQLServer\
+ ~~~~~~~~~~~~~~~~~~~~~~~\
+ CategoryInfo : ResourceUnavailable: (SQLServer:String) [Import- \
Module], FileNotFoundException\
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Comm \
ands.ImportModuleCommand\
\
Invoke-SqlCmd : The server principal \\"NT AUTHORITY\\\\SYSTEM\\" is not able to \
access the database \\"mydatabase\\" under the current security context.\
At C:\\\\Program Files (x86)\\\\Nagios\\\\NCPA\\\\plugins\\\\myscript.ps1:24 \
char:18\
+ $indexRequests = Invoke-SqlCmd -ServerInstance $sqlServer -Query $sqlCommand\
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\
+ CategoryInfo : InvalidOperation: (:) [Invoke-Sqlcmd], SqlPowerS \
hellSqlExecutionException\
+ FullyQualifiedErrorId : SqlError,Microsoft.SqlServer.Management.PowerShe \
ll.GetScriptCommand\
\
UNKNOWN script state"
}
Import-Module : The specified module 'SQLServer' was not loaded because no
valid module file was found in any module directory.
At C:\\Program Files (x86)\\Nagios\\NCPA\\plugins\\myscript.ps1:19
char:1
+ Import-Module SQLServer
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (SQLServer:String) [Import-
Module], FileNotFoundException
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Comm
ands.ImportModuleCommand
Invoke-SqlCmd : The server principal "NT AUTHORITY\\SYSTEM" is not able to
access the database "mydatabase" under the current security context.
At C:\\Program Files (x86)\\Nagios\\NCPA\\plugins\\myscript.ps1:24
char:18
+ $indexRequests = Invoke-SqlCmd -ServerInstance $sqlServer -Query $sqlCommand
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Invoke-Sqlcmd], SqlPowerS
hellSqlExecutionException
+ FullyQualifiedErrorId : SqlError,Microsoft.SqlServer.Management.PowerShe
ll.GetScriptCommand
UNKNOWN script state