2011-03-15 49 views
1

我有两个脚本。一个用服务器列表作为参数调用另一个。第二个查询旨在执行WMI查询。当我手动运行它时,它完全可以做到这一点。当我尝试将它作为工作来运行时,它会永久挂起,我必须将其删除。WMI查询脚本作为作业

对于这里的空间着想是调用脚本的相关部分:

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" 

执行时,作为工作将永久显示为“跑步”:

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 

我得到的最后一个输出是'输入的GetInfo:SERVER01,SERVER01'。如果我像这样手动运行它:PS C:\sqlps> .\GetServerDetailsLight.ps1 DBSERVER LOGDB SERVER01 SERVER01 WMI查询按预期执行。

我想确定这是为什么,或至少是一种有用的方法来捕获作业内的错误。

谢谢!

+1

您使用的是Windows XP吗? WMI在后台作业中存在已知问题。我曾在几个地方看到过。此外,就像参考资料一样,后台作业不支持除NTLM以外的任何身份验证。 – ravikanth 2011-03-15 15:57:27

+0

我正在使用XP。我并不知道WMI的具体问题。我确实有一个问题,即所有的后台作业挂起,但这是通过删除SCOM解决的。这个已知问题记录在任何地方? – Kenneth 2011-03-15 16:00:08

+0

不知道这是否记录在任何地方。当我输入这个时,我正在试图找到它。 – ravikanth 2011-03-15 16:02:29

回答

2

这是我能找到的一个实例。 http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/a6c816dd-2c2c-47bc-a2d0-238fbb9d66a6

还有很多其他的讨论我都看过。

无论如何,要确保您的WMI存储库不会损坏,只需重新编译即可。将以下行放在批处理文件中并运行:

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 
+0

我给你答案,因为你回答了你的评论。我将这些脚本移动到服务器2008并且它们正确执行。 – Kenneth 2011-03-17 18:57:23

0

帮你一个忙,并检查WinRM。如果远程处理已关闭,您将遇到这一组确切的症状。

Can *.ps1 scripts run as background jobs themselves execute *.ps1 scripts?

How do I debug a PowerShell background job?

+0

这不是使用PowerShell远程处理,所以这不可能是问题。 – JasonMArcher 2011-03-16 20:12:51

+0

是的。这也是我的想法。但是,当您启动作业时,您的代码将调用WMI,该命令将通过PS Remoting发出。我有完美的代码运行,但是当我在WMI调用中代码失败。经过多次的搔抓之后,我学到了PS在WMI的封面下做了自己的Remoting。 – codepoke 2011-03-18 02:12:22

0

你的脚本工作得很好,我用写凭据,但受阻时的凭据是错误的。