![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Send command line via WMI - Pipe output to text file on remote mac 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 System Specs![]() |
| | #2 (permalink) |
| Vista Ultimate 32bit | Re: Send command line via WMI - Pipe output to text file on remote mac 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 System Specs![]() |
| | #3 (permalink) |
| Vista Ultimate 32bit | Re: Send command line via WMI - Pipe output to text file on remote mac 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 System Specs![]() |
| | #4 (permalink) |
| | 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: Quote: > > 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...shell-tfm.aspx) > > [image: http://www.scriptinganswers.com/_images/logo_mvp.gif] > |
My System Specs![]() |
| | #5 (permalink) |
| Vista Ultimate 32bit | Re: Send command line via WMI - Pipe output to text file on remote mac 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 System Specs![]() |
| | #6 (permalink) |
| | 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: Quote: > > 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...shell-tfm.aspx) > > [image: http://www.scriptinganswers.com/_images/logo_mvp.gif] > |
My System Specs![]() |
| | #7 (permalink) |
| | 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: Quote: > > 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...shell-tfm.aspx) > > [image: http://www.scriptinganswers.com/_images/logo_mvp.gif] > |
My System Specs![]() |
| | #8 (permalink) |
| Vista Ultimate 32bit | Re: Send command line via WMI - Pipe output to text file on remote mac No. This is a function I wrote for PowerShell v1.0. You can copy and paste it into your PowerShell session then call it. |
My System Specs![]() |
| | #9 (permalink) |
| | RE: Send command line via WMI - Pipe output to text file on remote mac 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: Quote: > 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 System Specs![]() |
| | #10 (permalink) |
| Vista Ultimate 32bit | Re: Send command line via WMI - Pipe output to text file on remote mac 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 System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| how to log output from a command line program | VB Script | |||
| Re: Send command line via WMI - Pipe output to text file on remote | PowerShell | |||
| Pipe a pipe command to a file | VB Script | |||
| Suppress output from a command line tool | PowerShell | |||
| Output Event Log From Command Line | Vista General | |||