WMI Query Script as a Job

Posted by Kenneth on Stack Overflow See other posts from Stack Overflow or by Kenneth
Published on 2011-03-15T15:41:38Z Indexed on 2011/03/15 16:10 UTC
Read the original article Hit count: 350

Filed under:

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

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!

© Stack Overflow or respective owner

Related posts about powershell