WMI查询脚本作为作业

时间:2011-03-15 15:41:39

标签: powershell

我有两个脚本。一个用服务器列表作为参数调用另一个。第二个查询旨在执行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查询执行正常。

我试图确定这是为什么,或者至少是一种从工作中捕获错误的有用方法。

谢谢!

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

帮自己一个忙,查看WinRM。如果关闭远程处理,您将体验到这一系列症状。

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

How do I debug a PowerShell background job?

答案 2 :(得分:0)

您的脚本使用写入凭据可以正常使用,但在凭据错误时会被阻止。