【问题标题】:Getting error in SQL Server executing get-wmiobject在 SQL Server 执行 get-wmiobject 中出现错误
【发布时间】:2016-06-30 18:54:05
【问题描述】:

我正在尝试在 SQL Server 中执行以下 PowerShell 查询

xp_cmdshell "get-WmiObject Win32_LogicalDisk -AV-RISCVMSQL114 | Format-Table -Property DeviceID,FreeSpace,Size"

执行此操作后,我收到错误:

'get-WmiObject' 未被识别为内部或外部命令, 可运行的程序或批处理文件。

我想在所有 SQL Server 中执行此命令。它在 SQL Server 2008 上执行,但在 SQL Server 2012 上,由于版本或未安装某些 PowerShell 组件而引发该错误。

能否请您看一下代码并告诉我

【问题讨论】:

  • From the documentation: Starting in Windows PowerShell 3.0, this cmdlet has been superseded by Get-CimInstance. 在您在较新服务器上使用的 PowerShell 版本中很可能不再可用。
  • 所以我必须使用 xp_cmdshell "get-ciminstance Win32_LogicalDisk -AV-RISCVMSQL114
  • Get-WmiObject 在 PowerShell v5 中仍然可用。

标签: sql sql-server powershell


【解决方案1】:

试试:

xp_cmdshell 'PowerShell.exe -noprofile -command "get-WmiObject Win32_LogicalDisk -AV-RISCVMSQL114 | Format-Table -Property DeviceID,FreeSpace,Size"'

如果这不起作用,请使用管理员权限登录到服务器打开 PowerShell。键入 Get-ExecutionPolicy 并检查执行策略是否设置为无限制。如果不是,则输入以下内容,然后输入“A”(全部是):

Set-ExecutionPolicy Unrestricted

如果这仍然不起作用,请尝试至少升级到 PowerShell V3。但是,如果这不可能,您可以尝试使用以下命令修复损坏的 PowerShell V2 WMI 存储库:

winmgmt /verifyrepository

如果发现存储库不一致,请运行 Winmgmt /salvagerepositoryWinmgmt /resetrepository 并在每个之后进行验证。如果仍然不一致,请尝试以下步骤:

  • 进入Task Manager>>服务>>打开服务>>Windows Management Instrumentation服务,将启动类型改为Manual
  • 停止 WMI 服务:

    net stop winmgmt

  • 将存储库文件夹 (C:\Windows\System32\wbem\Repository) 重命名为“Repository_OLD”。

  • 启动 WMI 服务:

    net start winmgmt

  • 将服务设置回自动启动。

失败重新注册所有dlls并使用以下.bat脚本重新编译Wbem目录中的.mofs,然后重新启动。

@echo off
sc config winmgmt start= disabled
net stop winmgmt /y
%systemdrive%
cd %windir%\system32\wbem
for /f %%s in (‘dir /b *.dll’) do regsvr32 /s %%s
wmiprvse /regserver 
winmgmt /regserver 
sc config winmgmt start= auto
net start winmgmt
for /f %%s in (‘dir /s /b *.mof *.mfl’) do mofcomp %%s

【讨论】:

  • 使用上述命令后 Get-WmiObject : Not found At line:1 char:14 + get-WmiObject
  • 运行xp_cmdshell 'PowerShell.exe -noprofile -command "Get-ExecutionPolicy"'xp_cmdshell 'PowerShell.exe -noprofile -command "$PSVersionTable.PSVersion.Major"'会得到什么?
  • 第一个输出我得到远程签名和其他 2
  • 无限制和3
  • 我必须在服务器上将 powershell 版本升级到 3 吗?
猜你喜欢
  • 2018-07-04
  • 1970-01-01
  • 1970-01-01
  • 2019-06-15
  • 1970-01-01
  • 2023-03-04
  • 2013-07-13
  • 2017-01-26
  • 2022-10-07
相关资源
最近更新 更多