1

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!

Kenneth
  • 1,364
  • 1
  • 8
  • 11
  • 1
    Are you using Windows XP? There is a known issue with WMI in background jobs. I have seen that at several places. Also, just as an FYI, background jobs do not support any authentication other than NTLM. – ravikanth Mar 15 '11 at 15:57
  • I am using XP. I was not aware of a WMI specific problem. I did have an issue where *all* background jobs were hanging, but this was resolved by removing SCOM. Is this known issue documented anywhere? – Kenneth Mar 15 '11 at 16:00
  • Not sure if this is documented anywhere. I am trying to find that as I type this. – ravikanth Mar 15 '11 at 16:02

3 Answers3

2

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
ravikanth
  • 24,922
  • 4
  • 60
  • 60
  • I gave you the answer since you answered it with your comment. I move the scripts to server 2008 and they executed properly. – Kenneth Mar 17 '11 at 18:57
0

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?

Community
  • 1
  • 1
codepoke
  • 1,272
  • 1
  • 22
  • 40
  • This isn't using PowerShell remoting, so this could not be the issue. – JasonMArcher Mar 16 '11 at 20:12
  • Yeah. That's what I thought, too. When you Start-Job, though, and your code calls WMI, the command is issued via PS Remoting. I had perfectly good job code running, but when I put in the WMI call the code failed. After much head scratching, I learned PS does its own Remoting under the covers with WMI. – codepoke Mar 18 '11 at 02:12
0

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

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52