开发者

WMI Query Script as a Job

开发者 https://www.devze.com 2023-02-17 10:56 出处:网络
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

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 | 
    s开发者_如何学Pythonelect 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

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.

Thanks!


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.

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


Do yourself a favor and check WinRM. If remoting is turned off, you'll experience this exact set of symptoms.

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

How do I debug a PowerShell background job?


Your script worked fine for me with the write credentials, but blocked when the credentials were wrong.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号