WMI 查询脚本作为作业 [英] WMI Query Script as a Job
问题描述
我有两个脚本.一个使用服务器列表作为参数调用另一个.第二个查询旨在执行 WMI 查询.当我手动运行它时,它完美地做到了这一点.当我尝试将其作为作业运行时,它会永远挂起,我必须将其删除.
I have two scripts. One calls the other with a list of servers as parameters. The second query is designed to execute a WMI query. When I run it manually, it does this perfectly. When I try to run it as a job it hangs forever and I have to remove it.
为了空间起见,这里是调用脚本的相关部分:
For the sake of space here is the relevant part of the calling script:
ProcessServers.ps1
Start-Job -FilePath .\GetServerDetailsLight.ps1 -ArgumentList $sqlsrv,$destdb,$server,$instance
GetServerDetailsLight.ps1
param($sqlsrv,$destdb,$server,$instance)
$password = get-content C:\SQLPS\auth.txt | convertto-securestring
$credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist "DOMAIN\MYUSER",$password
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO')
$box_id = 0;
if ($sqlsrv.length -eq 0) {
write-output "No data passed"
break
}
function getinfo {
param(
[string]$svr,
[string]$inst
)
"Entered GetInfo with: $svr,$inst"
$cs = get-wmiobject win32_operatingsystem -computername $svr -credential $credentials -authentication 6 -Verbose -Debug |
select Name, Model, Manufacturer, Description, DNSHostName, Domain, DomainRole, PartOfDomain,
NumberOfProcessors, SystemType, TotalPhysicalMemory, UserName, Workgroup
write-output "WMI Results: $cs"
}
getinfo $server $instance
write-output "Complete"
作为作业执行,它将永远显示为正在运行":
Executed as a job it will show as 'running' forever:
PS C:\sqlps> Start-Job -FilePath .\GetServerDetailsLight.ps1 -ArgumentList DBSERVER,LOGDB,SERVER01,SERVER01
Id Name State HasMoreData Location Command
-- ---- ----- ----------- -------- -------
21 Job21 Running True localhost param($sqlsrv,$destdb,...
GAC Version Location
--- ------- --------
True v2.0.50727 C:\WINDOWS\assembly\GAC_MSIL\Microsoft.SqlServer.Smo\10.0.0.0__89845dcd8080cc91\Microsoft.SqlServer.Smo.dll
getinfo MSDCHR01 MSDCHR01
Entered GetInfo with: SERVER01,SERVER01
我得到的最后一个输出是'Entered GetInfo with: SERVER01,SERVER01'.如果我像这样手动运行它: PS C:\sqlps>.\GetServerDetailsLight.ps1 DBSERVER LOGDB SERVER01 SERVER01
WMI 查询按预期执行.
The last output I ever get is the 'Entered GetInfo with: SERVER01,SERVER01'. If I run it manually like so: PS C:\sqlps> .\GetServerDetailsLight.ps1 DBSERVER LOGDB SERVER01 SERVER01
The WMI query executes just as expected.
我正在尝试确定为什么会这样,或者至少是一种从作业中捕获错误的有用方法.
I am trying to determine why this is, or at least a useful way to trap errors from within jobs.
谢谢!
推荐答案
This is one instance I could find. http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/a6c816dd-2c2c-47bc-a2d0-238fbb9d66a6
我还看到许多其他类似的讨论.
There are many other discussions I have seen around the same.
无论如何,要确保您的 WMI 存储库没有损坏,只需尝试重新编译它.将以下行放入批处理文件并运行:
Anyway, to make sure your WMI respository isn't corrupt, just try re-compiling it. Put the following lines in a batch file and run:
net stop winmgmt
c:
cd c:\windows\system32\wbem
rd /S /Q repository
regsvr32 /s %systemroot%\system32\scecli.dll
regsvr32 /s %systemroot%\system32\userenv.dll
mofcomp cimwin32.mof
mofcomp cimwin32.mfl
mofcomp rsop.mof
mofcomp rsop.mfl
for /f %%s in ('dir /b /s *.dll') do regsvr32 /s %%s
for /f %%s in ('dir /b *.mof') do mofcomp %%s
for /f %%s in ('dir /b *.mfl') do mofcomp %%s
mofcomp exwmi.mof
mofcomp -n:root\cimv2\applications\exchange wbemcons.mof
mofcomp -n:root\cimv2\applications\exchange smtpcons.mof
mofcomp exmgmt.mof
这篇关于WMI 查询脚本作为作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!