• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Send command line via WMI - Pipe output to text file on remote mac

R
#1
Hi All,

I'm trying to send a command line to a remote machine using WMI and saving
the out to a text file on the remote machine so i can pull that file back to
parse. The following is just a test script and it works but it will not
output to a text file. I know it works because i send a different command to
stop a vmware image.

$command = "dir c:\ > c:\output.txt"
$computer = "172.16.98.185"

$ProcessClass = get-wmiobject -query "SELECT * FROM Meta_Class WHERE
__Class = 'Win32_Process'" -namespace "root\cimv2" -computername $computer
$results = $ProcessClass.Create($command)
$results | format-list *

Any help or advice would be appreciated.

Thanks,
Ripp
 

My Computer

sapienscripter

Scripting Guru
Vista Pro
Messages
59
Location
Syracuse, NY
#2
You don't need to parse text in PowerShell. The Get-WMIObject cmdlet will return an object. Access its properties directly. It looks like you are trying to run a command remotely using WMI. I think you're making this harder than it needs to be. You can simply use a PSDrive mapped to the remote computer's C$ share. Of use a WMI query with the CIM_DATAFILE.
 

My Computer

System One

  • CPU
    Intel Core2 T5600 @1.83GHz
    Memory
    4GB

sapienscripter

Scripting Guru
Vista Pro
Messages
59
Location
Syracuse, NY
#3
If you really want to use WMI to launch a remote process, this function should work for you:
Code:
Function New-RemoteProcess {
    Param([string]$computername=$env:computername,
          [string]$cmd=$(Throw "You must enter the full path to the command which will create the process.")
        )
    
    $ErrorActionPreference="SilentlyContinue"
     
    Trap {
        Write-Warning "There was an error connecting to the remote computer or creating the process"
        Continue
    }    
        
    Write-Host "Connecting to $computername" -ForegroundColor CYAN
    Write-Host "Process to create is $cmd" -ForegroundColor CYAN

    [wmiclass]$wmi="\\$computername\root\cimv2:win32_process"
    
    #bail out if the object didnt' get created
    if (!$wmi) {return}
    
    $remote=$wmi.Create($cmd)
    
    if ($remote.returnvalue -eq 0) {
        Write-Host "Successfully launched $cmd on $computername with a process id of" $remote.processid -ForegroundColor GREEN
    }
    else {
        Write-Host "Failed to launch $cmd on $computername. ReturnValue is" $remote.ReturnValue -ForegroundColor RED
    }
}

New-RemoteProcess -comp "server01" -cmd "c:\windows\notepad.exe"
 

My Computer

System One

  • CPU
    Intel Core2 T5600 @1.83GHz
    Memory
    4GB
R
#4
Re: Send command line via WMI - Pipe output to text file on remote

I have several workstations running vmware player (free version) and on each
workstation exists an excutable called vmrun.exe. I'm executing "vmrun.exe
list" on the remote machine to find out what image file is being run in
vmware player. i can not do this remotely so i have to issue the command via
wmi and pipe the results to a file on that remote machine. I then grab the
file via \\<workstation IP>\c$\results.txt and parse the path out.

If you have a better way please let me know, i will look into what you wrote
soon as i get a chance.

Thanks,
Ripp

"sapienscripter" wrote:

>
> You don't need to parse text in PowerShell. The Get-WMIObject cmdlet
> will return an object. Access its properties directly. It looks like
> you are trying to run a command remotely using WMI. I think you're
> making this harder than it needs to be. You can simply use a PSDrive
> mapped to the remote computer's C$ share. Of use a WMI query with the
> CIM_DATAFILE.
>
>
> --
> sapienscripter
>
> Now Available: '-Managing Active Directory with Windows PowerShell:
> TFM-'
> (http://www.scriptingoutpost.com/p-39-managing-active-directory-with-windows-powershell-tfm.aspx)
>
> [image: http://www.scriptinganswers.com/_images/logo_mvp.gif]
>
 

My Computer

sapienscripter

Scripting Guru
Vista Pro
Messages
59
Location
Syracuse, NY
#5
Try using my function:

New-RemoteProcess -comp "server01" -cmd "cmd /c vmrun.exe list > c:\results.txt"

You might need to specify the path to vmrun.exe.
 

My Computer

System One

  • CPU
    Intel Core2 T5600 @1.83GHz
    Memory
    4GB
R
#6
Re: Send command line via WMI - Pipe output to text file on remote

Cool, thanks im going to look into and see if it will work for me. =)

"sapienscripter" wrote:

>
> If you really want to use WMI to launch a remote process, this function
> should work for you:
>
> Code:
> --------------------
>
> Function New-RemoteProcess {
> Param([string]$computername=$env:computername,
> [string]$cmd=$(Throw "You must enter the full path to the command which will create the process.")
> )
>
> $ErrorActionPreference="SilentlyContinue"
>
> Trap {
> Write-Warning "There was an error connecting to the remote computer or creating the process"
> Continue
> }
>
> Write-Host "Connecting to $computername" -ForegroundColor CYAN
> Write-Host "Process to create is $cmd" -ForegroundColor CYAN
>
> [wmiclass]$wmi="\\$computername\root\cimv2:win32_process"
>
> #bail out if the object didnt' get created
> if (!$wmi) {return}
>
> $remote=$wmi.Create($cmd)
>
> if ($remote.returnvalue -eq 0) {
> Write-Host "Successfully launched $cmd on $computername with a process id of" $remote.processid -ForegroundColor GREEN
> }
> else {
> Write-Host "Failed to launch $cmd on $computername. ReturnValue is" $remote.ReturnValue -ForegroundColor RED
> }
> }
>
> New-RemoteProcess -comp "server01" -cmd "c:\windows\notepad.exe"
>
>
> --------------------
>
>
> --
> sapienscripter
>
> Now Available: '-Managing Active Directory with Windows PowerShell:
> TFM-'
> (http://www.scriptingoutpost.com/p-39-managing-active-directory-with-windows-powershell-tfm.aspx)
>
> [image: http://www.scriptinganswers.com/_images/logo_mvp.gif]
>
 

My Computer

R
#7
Re: Send command line via WMI - Pipe output to text file on remote

Is new-remoteprocess a Powershell 2.0 cmdlet? And if so is Powershell 2.0
what i should be using now or is it still in testing stages?

"sapienscripter" wrote:

>
> Try using my function:
>
> New-RemoteProcess -comp "server01" -cmd "cmd /c vmrun.exe list >
> c:\results.txt"
>
> You might need to specify the path to vmrun.exe.
>
>
> --
> sapienscripter
>
> Now Available: '-Managing Active Directory with Windows PowerShell:
> TFM-'
> (http://www.scriptingoutpost.com/p-39-managing-active-directory-with-windows-powershell-tfm.aspx)
>
> [image: http://www.scriptinganswers.com/_images/logo_mvp.gif]
>
 

My Computer

R
#9
Okay, I'm able to run the command on the remote machine and pipe the output
to the a text file and access via \\<machine>\c$\file.txt

My problem is the WMI command executes and moves on to the next command
before the text file is actually done being written, so the following command
that parses that file errors out because the data is still processing.

I found some code but not sure if i can use it for my particular
application, here is some sample code i found using .net

PS C:\Users\Administrator> $tpid =
[diagnostics.process]::start("notepad.exe")PS C:\Users\Administrator>
$tpid.WaitForExit()

Is it possible to use this to pause the script until the WMI command
finishes completely on the remote machine?

"Ripp" wrote:

> Hi All,
>
> I'm trying to send a command line to a remote machine using WMI and saving
> the out to a text file on the remote machine so i can pull that file back to
> parse. The following is just a test script and it works but it will not
> output to a text file. I know it works because i send a different command to
> stop a vmware image.
>
> $command = "dir c:\ > c:\output.txt"
> $computer = "172.16.98.185"
>
> $ProcessClass = get-wmiobject -query "SELECT * FROM Meta_Class WHERE
> __Class = 'Win32_Process'" -namespace "root\cimv2" -computername $computer
> $results = $ProcessClass.Create($command)
> $results | format-list *
>
> Any help or advice would be appreciated.
>
> Thanks,
> Ripp
 

My Computer

sapienscripter

Scripting Guru
Vista Pro
Messages
59
Location
Syracuse, NY
#10
After you kick off the remote process use the Start-Sleep cmdlet to pause script execution for as long as you think it will take for the vmrun command to complete.
 

My Computer

System One

  • CPU
    Intel Core2 T5600 @1.83GHz
    Memory
    4GB
R
#11
Re: Send command line via WMI - Pipe output to text file on remote

Thanks, will do that. No other way to monitor that wmi process? Like wait
until the cmd window exits?

"sapienscripter" wrote:

>
> After you kick off the remote process use the Start-Sleep cmdlet to
> pause script execution for as long as you think it will take for the
> vmrun command to complete.
>
>
> --
> sapienscripter
>
> Now Available: '-Managing Active Directory with Windows PowerShell:
> TFM-'
> (http://www.scriptingoutpost.com/p-39-managing-active-directory-with-windows-powershell-tfm.aspx)
>
> [image: http://www.scriptinganswers.com/_images/logo_mvp.gif]
>
 

My Computer

R
#12
Re: Send command line via WMI - Pipe output to text file on remote

How about the .net code?

example:
$tpid = [diagnostics.process]::start("notepad.exe")
$tpid.WaitForExit()


"sapienscripter" wrote:

>
> After you kick off the remote process use the Start-Sleep cmdlet to
> pause script execution for as long as you think it will take for the
> vmrun command to complete.
>
>
> --
> sapienscripter
>
> Now Available: '-Managing Active Directory with Windows PowerShell:
> TFM-'
> (http://www.scriptingoutpost.com/p-39-managing-active-directory-with-windows-powershell-tfm.aspx)
>
> [image: http://www.scriptinganswers.com/_images/logo_mvp.gif]
>
 

My Computer

S

Shay Levy [MVP]

#13
Re: Send command line via WMI - Pipe output to text file on remote

Hi Ripp,


I'm not sure WaitForExit() works for remote processes however you can try:

cmd /c 'start /wait notepad.exe'


---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
PowerShell Toolbar: http://tinyurl.com/PSToolbar



R> How about the .net code?
R>
R> example:
R> $tpid = [diagnostics.process]::start("notepad.exe")
R> $tpid.WaitForExit()
R> "sapienscripter" wrote:
R>

>> After you kick off the remote process use the Start-Sleep cmdlet to
>> pause script execution for as long as you think it will take for the
>> vmrun command to complete.
>>
>> -- sapienscripter
>>
>> Now Available: '-Managing Active Directory with Windows PowerShell:
>> TFM-'
>> (http://www.scriptingoutpost.com/p-39-managing-active-directory-with-
>> windows-powershell-tfm.aspx)
>>
>> [image: http://www.scriptinganswers.com/_images/logo_mvp.gif]
>>
 

My Computer

sapienscripter

Scripting Guru
Vista Pro
Messages
59
Location
Syracuse, NY
#14
When you start a remote process using WMI, as in my function, the object that gets created includes a processID property. You can use separate code to watch that processID from your computer and once it is gone, continue on with the script.
 

My Computer

System One

  • CPU
    Intel Core2 T5600 @1.83GHz
    Memory
    4GB
R
#15
Re: Send command line via WMI - Pipe output to text file on remote

Thanks for that info. I was calling a vmware executable that was outputing
to a text file on the remote machine and the "start /wait" didn't stop the
powershell process just left the output file open where i had to go to the
remote machine and kill the cmd. Thanks tho, it looked promising =(

Im using a start-sleep 8 for now, wish there was a cleaner way.

"Shay Levy [MVP]" wrote:

> Hi Ripp,
>
>
> I'm not sure WaitForExit() works for remote processes however you can try:
>
> cmd /c 'start /wait notepad.exe'
>
>
> ---
> Shay Levy
> Windows PowerShell MVP
> http://blogs.microsoft.co.il/blogs/ScriptFanatic
> PowerShell Toolbar: http://tinyurl.com/PSToolbar
>
>
>
> R> How about the .net code?
> R>
> R> example:
> R> $tpid = [diagnostics.process]::start("notepad.exe")
> R> $tpid.WaitForExit()
> R> "sapienscripter" wrote:
> R>

> >> After you kick off the remote process use the Start-Sleep cmdlet to
> >> pause script execution for as long as you think it will take for the
> >> vmrun command to complete.
> >>
> >> -- sapienscripter
> >>
> >> Now Available: '-Managing Active Directory with Windows PowerShell:
> >> TFM-'
> >> (http://www.scriptingoutpost.com/p-39-managing-active-directory-with-
> >> windows-powershell-tfm.aspx)
> >>
> >> [image: http://www.scriptinganswers.com/_images/logo_mvp.gif]
> >>
>
>
>
 

My Computer

S

Shay Levy [MVP]

#17
Re: Send command line via WMI - Pipe output to text file on remote

Try this:

try {
while ([diagnostics.process]::GetProcessById($results.processId,$serverName))
{sleep 1}
} catch {
continue;
}

"process has exited"






---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
PowerShell Toolbar: http://tinyurl.com/PSToolbar



R> How about the .net code?
R>
R> example:
R> $tpid = [diagnostics.process]::start("notepad.exe")
R> $tpid.WaitForExit()
R> "sapienscripter" wrote:
R>

>> After you kick off the remote process use the Start-Sleep cmdlet to
>> pause script execution for as long as you think it will take for the
>> vmrun command to complete.
>>
>> -- sapienscripter
>>
>> Now Available: '-Managing Active Directory with Windows PowerShell:
>> TFM-'
>> (http://www.scriptingoutpost.com/p-39-managing-active-directory-with-
>> windows-powershell-tfm.aspx)
>>
>> [image: http://www.scriptinganswers.com/_images/logo_mvp.gif]
>>
 

My Computer

R
#18
Re: Send command line via WMI - Pipe output to text file on remote

Thanks for the function it works great, what would you use to monitor the
processID until it finishes?

"sapienscripter" wrote:

>
> When you start a remote process using WMI, as in my function, the object
> that gets created includes a processID property. You can use separate
> code to watch that processID from your computer and once it is gone,
> continue on with the script.
>
>
> --
> sapienscripter
>
> Now Available: '-Managing Active Directory with Windows PowerShell:
> TFM-'
> (http://www.scriptingoutpost.com/p-39-managing-active-directory-with-windows-powershell-tfm.aspx)
>
> [image: http://www.scriptinganswers.com/_images/logo_mvp.gif]
>
 

My Computer

R
#19
Re: Send command line via WMI - Pipe output to text file on remote

so i used something like the following code to check the process:

$processTest = "999"
While($processtest -ne $null)
{
$processTest = Get-WmiObject -computername $computername `
-query "select * from win32_process where processid='$processid'"
}

It actually works i tested with the notepad process on remote machine. It
looks like the process for the command line i send the remote computer is
ending before the actual output file i piped to is being created and
populated. Great help though, i learned alot with your function and the
process check loop.

"sapienscripter" wrote:

>
> When you start a remote process using WMI, as in my function, the object
> that gets created includes a processID property. You can use separate
> code to watch that processID from your computer and once it is gone,
> continue on with the script.
>
>
> --
> sapienscripter
>
> Now Available: '-Managing Active Directory with Windows PowerShell:
> TFM-'
> (http://www.scriptingoutpost.com/p-39-managing-active-directory-with-windows-powershell-tfm.aspx)
>
> [image: http://www.scriptinganswers.com/_images/logo_mvp.gif]
>
 

My Computer

R
#20
Re: Send command line via WMI - Pipe output to text file on remote

Thank you! Nice piece of code i will be using alot i'm sure =)

"Shay Levy [MVP]" wrote:

>
> Try this:
>
> try {
> while ([diagnostics.process]::GetProcessById($results.processId,$serverName))
> {sleep 1}
> } catch {
> continue;
> }
>
> "process has exited"
>
>
>
>
>
>
> ---
> Shay Levy
> Windows PowerShell MVP
> http://blogs.microsoft.co.il/blogs/ScriptFanatic
> PowerShell Toolbar: http://tinyurl.com/PSToolbar
>
>
>
> R> How about the .net code?
> R>
> R> example:
> R> $tpid = [diagnostics.process]::start("notepad.exe")
> R> $tpid.WaitForExit()
> R> "sapienscripter" wrote:
> R>

> >> After you kick off the remote process use the Start-Sleep cmdlet to
> >> pause script execution for as long as you think it will take for the
> >> vmrun command to complete.
> >>
> >> -- sapienscripter
> >>
> >> Now Available: '-Managing Active Directory with Windows PowerShell:
> >> TFM-'
> >> (http://www.scriptingoutpost.com/p-39-managing-active-directory-with-
> >> windows-powershell-tfm.aspx)
> >>
> >> [image: http://www.scriptinganswers.com/_images/logo_mvp.gif]
> >>
>
>
>
 

My Computer

Users Who Are Viewing This Thread (Users: 1, Guests: 0)