2011-03-15 76 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

你的腳本工作得很好,我用寫憑據,但受阻時的憑據是錯誤的。