Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

How can I execute WMI method asynchronously?

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 12-18-2007   #1 (permalink)
ChanKaiShi
Guest


 

How can I execute WMI method asynchronously?

Hi,

I have sample script below which I use to archive files on remote server, I
need to wait till my remote process exits and then copy archived files over,
problem is that method call to ZipFile returns immediately instead of waiting
for process to actually terminate, is there an easy way to wait for it to
terminate before proceeding further?

$ComputerName = "SERVER01"
ZipFile $ComputerName
Write-Host "All Done"


function ZipFile ([string]$ComputerName)
{
$objWMI = New-Object System.Management.ManagementClass
"\\$ComputerName\root\cimv2:Win32_Process"
$FullPath = "c:\winnt\system32\7za.exe a -y -tgzip
d:\Logs\FTPLogs\MSFTPSVC1\a.gzip d:\Logs\FTPLogs\MSFTPSVC1\ex071127.log"
$cmdargs = $FullPath, $null, $null, 0
$objWmi.Create($FullPath) | Out-Null
}

My System SpecsSystem Spec
Old 12-18-2007   #2 (permalink)
Marco Shaw [MVP]
Guest


 

Re: How can I execute WMI method asynchronously?

ChanKaiShi wrote:
Quote:

> Hi,
>
> I have sample script below which I use to archive files on remote server, I
> need to wait till my remote process exits and then copy archived files over,
> problem is that method call to ZipFile returns immediately instead of waiting
> for process to actually terminate, is there an easy way to wait for it to
> terminate before proceeding further?
>
> $ComputerName = "SERVER01"
> ZipFile $ComputerName
> Write-Host "All Done"
>
>
> function ZipFile ([string]$ComputerName)
> {
> $objWMI = New-Object System.Management.ManagementClass
> "\\$ComputerName\root\cimv2:Win32_Process"
> $FullPath = "c:\winnt\system32\7za.exe a -y -tgzip
> d:\Logs\FTPLogs\MSFTPSVC1\a.gzip d:\Logs\FTPLogs\MSFTPSVC1\ex071127.log"
> $cmdargs = $FullPath, $null, $null, 0
> $objWmi.Create($FullPath) | Out-Null
> }
How about in your ZipFile function, just before it ends, that you create
a loop checking whether the 7za.exe process is still running? Once
gone, then exiting the function...

Marco

--
Microsoft MVP - Windows PowerShell
http://www.microsoft.com/mvp

PowerGadgets MVP
http://www.powergadgets.com/mvp

Blog:
http://marcoshaw.blogspot.com
My System SpecsSystem Spec
Old 12-18-2007   #3 (permalink)
ChanKaiShi
Guest


 

Re: How can I execute WMI method asynchronously?

I can not figure out how to do what you are offering since I have no idea how
to find ProcessID of remotely executing process, it returns 0 for me.

"Marco Shaw [MVP]" wrote:
Quote:

> ChanKaiShi wrote:
Quote:

> > Hi,
> >
> > I have sample script below which I use to archive files on remote server, I
> > need to wait till my remote process exits and then copy archived files over,
> > problem is that method call to ZipFile returns immediately instead of waiting
> > for process to actually terminate, is there an easy way to wait for it to
> > terminate before proceeding further?
> >
> > $ComputerName = "SERVER01"
> > ZipFile $ComputerName
> > Write-Host "All Done"
> >
> >
> > function ZipFile ([string]$ComputerName)
> > {
> > $objWMI = New-Object System.Management.ManagementClass
> > "\\$ComputerName\root\cimv2:Win32_Process"
> > $FullPath = "c:\winnt\system32\7za.exe a -y -tgzip
> > d:\Logs\FTPLogs\MSFTPSVC1\a.gzip d:\Logs\FTPLogs\MSFTPSVC1\ex071127.log"
> > $cmdargs = $FullPath, $null, $null, 0
> > $objWmi.Create($FullPath) | Out-Null
> > }
>
> How about in your ZipFile function, just before it ends, that you create
> a loop checking whether the 7za.exe process is still running? Once
> gone, then exiting the function...
>
> Marco
>
> --
> Microsoft MVP - Windows PowerShell
> http://www.microsoft.com/mvp
>
> PowerGadgets MVP
> http://www.powergadgets.com/mvp
>
> Blog:
> http://marcoshaw.blogspot.com
>
My System SpecsSystem Spec
Old 12-18-2007   #4 (permalink)
Marco Shaw [MVP]
Guest


 

Re: How can I execute WMI method asynchronously?

ChanKaiShi wrote:
Quote:

> I can not figure out how to do what you are offering since I have no idea how
> to find ProcessID of remotely executing process, it returns 0 for me.
I tried this:
$objWMI = New-Object System.Management.ManagementClass
"\\.\root\cimv2:Win32_Process"
$FullPath = "c:\windows\system32\notepad.exe c:\test.txt "
$cmdargs = $FullPath, $null, $null, 0
$objWmi.Create($FullPath) | Out-Null
get-wmiobject -computer . win32_process| `
foreach-object{
if($_.commandline -eq $fullpath){"notepad found"}
}

Just drop something like this into your function:

while ($true) {
$wmi=get-wmiobject -computer . win32_process| `
where-object{ `
$_.commandline -eq $fullpath
}
if($wmi -eq $null){break}
start-sleep -seconds 5
}
My System SpecsSystem Spec
Old 12-18-2007   #5 (permalink)
ChanKaiShi
Guest


 

Re: How can I execute WMI method asynchronously?

Thanks,

But this code will not be specific to my instance of process but any process
with similar command line. It's possible that their will be some stale
processes out there with similar command line option. I really need to find
ProcessID for newly created process for endless loop but code below does not
produce it.
Does code below returs processID for you? It returns 0 for me always and
this the only way I beleive to make endless loop work since this is the only
unique thing about process.


$objWMI = [WmiClass]"\\$ComputerName\root\cimv2:Win32_Process"
$FullPath = "notepad.exe"
$cmdargs = $FullPath, $null, $null, 0
$ProcessID = $objWmi.PsBase.InvokeMethod("Create", $cmdArgs)


"Marco Shaw [MVP]" wrote:
Quote:

> ChanKaiShi wrote:
Quote:

> > I can not figure out how to do what you are offering since I have no idea how
> > to find ProcessID of remotely executing process, it returns 0 for me.
>
> I tried this:
> $objWMI = New-Object System.Management.ManagementClass
> "\\.\root\cimv2:Win32_Process"
> $FullPath = "c:\windows\system32\notepad.exe c:\test.txt "
> $cmdargs = $FullPath, $null, $null, 0
> $objWmi.Create($FullPath) | Out-Null
> get-wmiobject -computer . win32_process| `
> foreach-object{
> if($_.commandline -eq $fullpath){"notepad found"}
> }
>
> Just drop something like this into your function:
>
> while ($true) {
> $wmi=get-wmiobject -computer . win32_process| `
> where-object{ `
> $_.commandline -eq $fullpath
> }
> if($wmi -eq $null){break}
> start-sleep -seconds 5
> }
>
My System SpecsSystem Spec
Old 12-18-2007   #6 (permalink)
Jon
Guest


 

Re: How can I execute WMI method asynchronously?


"ChanKaiShi" <ChanKaiShi@xxxxxx> wrote in message
news:B9C6CAB0-B54F-47C4-8247-12E4491E60BE@xxxxxx
Quote:

>I can not figure out how to do what you are offering since I have no idea
>how
> to find ProcessID of remotely executing process, it returns 0 for me.
>


The object returned by the 'Create' method should contain the process id
eg

$Computer = "."
$c = [WMICLASS]"\\$computer\root\cimv2:WIn32_Process"
$proc = $c.Create("notepad.exe")
$proc.ProcessId


--
Jon



My System SpecsSystem Spec
Old 12-18-2007   #7 (permalink)
ChanKaiShi
Guest


 

Re: How can I execute WMI method asynchronously?

Thanks,

My sample code was too complex, I used your example and it does return
processID, but original question I guess is still unanswered since I can not
use blocking call for "Create" method.

"Jon" wrote:
Quote:

>
> "ChanKaiShi" <ChanKaiShi@xxxxxx> wrote in message
> news:B9C6CAB0-B54F-47C4-8247-12E4491E60BE@xxxxxx
Quote:

> >I can not figure out how to do what you are offering since I have no idea
> >how
> > to find ProcessID of remotely executing process, it returns 0 for me.
> >
>
>
>
> The object returned by the 'Create' method should contain the process id
> eg
>
> $Computer = "."
> $c = [WMICLASS]"\\$computer\root\cimv2:WIn32_Process"
> $proc = $c.Create("notepad.exe")
> $proc.ProcessId
>
>
> --
> Jon
>
>
>
>
My System SpecsSystem Spec
Old 12-18-2007   #8 (permalink)
Jon
Guest


 

Re: How can I execute WMI method asynchronously?

"ChanKaiShi" <ChanKaiShi@xxxxxx> wrote in message
news:3601248B-3079-4227-A1A1-26ACED112329@xxxxxx
Quote:

> Thanks,
>
> My sample code was too complex, I used your example and it does return
> processID, but original question I guess is still unanswered since I can
> not
> use blocking call for "Create" method.
>



You could try using the 'GetProcessById' method to return a process
object,and then use 'WaitForExit' on it. eg something like

$Computer = "."
$c = [WMICLASS]"\\$computer\root\cimv2:WIn32_Process"
$proc = $c.Create("notepad.exe")
$ProcessId =$proc.ProcessId
$p = [diagnostics.process]::GetProcessById($ProcessId,$Computer)
$p.WaitForExit()



Process.GetProcessById Method (Int32, String)
http://msdn2.microsoft.com/en-us/lib...ec(VS.80).aspx


Process..::.WaitForExit Method
http://msdn2.microsoft.com/en-us/lib...itforexit.aspx



--
Jon




My System SpecsSystem Spec
Old 12-19-2007   #9 (permalink)
ChanKaiShi
Guest


 

Re: How can I execute WMI method asynchronously?

WaitForProcess() is not supported for remote machines.

"Jon" wrote:
Quote:

> "ChanKaiShi" <ChanKaiShi@xxxxxx> wrote in message
> news:3601248B-3079-4227-A1A1-26ACED112329@xxxxxx
Quote:

> > Thanks,
> >
> > My sample code was too complex, I used your example and it does return
> > processID, but original question I guess is still unanswered since I can
> > not
> > use blocking call for "Create" method.
> >
>
>
>
>
> You could try using the 'GetProcessById' method to return a process
> object,and then use 'WaitForExit' on it. eg something like
>
> $Computer = "."
> $c = [WMICLASS]"\\$computer\root\cimv2:WIn32_Process"
> $proc = $c.Create("notepad.exe")
> $ProcessId =$proc.ProcessId
> $p = [diagnostics.process]::GetProcessById($ProcessId,$Computer)
> $p.WaitForExit()
>
>
>
> Process.GetProcessById Method (Int32, String)
> http://msdn2.microsoft.com/en-us/lib...ec(VS.80).aspx
>
>
> Process..::.WaitForExit Method
> http://msdn2.microsoft.com/en-us/lib...itforexit.aspx
>
>
>
> --
> Jon
>
>
>
>
>
My System SpecsSystem Spec
Old 12-19-2007   #10 (permalink)
Jon
Guest


 

Re: How can I execute WMI method asynchronously?



"ChanKaiShi" <ChanKaiShi@xxxxxx> wrote in message
news:B9CDA456-C8B3-4606-B3D1-E08DC50B4351@xxxxxx
Quote:

> WaitForProcess() is not supported for remote machines.
>

Had a feeling that that may be the case, but I thought I'd throw it out
there anyway.

You may be able to do something with some of the other members of the
process object eg 'HasExited'


#--------------------------------------------------------------------
$Computer = "."
$c = [WMICLASS]"\\$computer\root\cimv2:WIn32_Process"
$proc = $c.Create("notepad.exe")
$ProcessId =$proc.ProcessId
$p = [diagnostics.process]::GetProcessById($ProcessId,$Computer)

foreach ($i in 1..100) {

If ($p.HasExited) {break}
start-sleep -seconds 1

}

#--------------------------------------------------------------------

Can't test it here at the moment, but that is what I would try.

Pipe $p to Get-Member for more options

$p | gm

--
Jon



My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Execute error: Type Mismatch: 'Execute' kellym-global.co.uk VB Script 2 08-21-2008 10:09 AM
Running an external process asynchronously Romu PowerShell 7 05-15-2008 06:53 AM
Method invocation failed because [System.String] doesn't contain a method B Williams PowerShell 0 03-31-2008 06:00 PM
execute wmi method oneline Kim Oppalfens PowerShell 5 02-12-2007 02:31 PM
Run tasks asynchronously: external files vs. script blocks OK PowerShell 7 11-05-2006 01:38 PM


Update your Vista Drivers Update Your Drivers Now!!

Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008