Windows Vista Tips

Windows Vista Tips > Newsgroups > Windows Server > Scripting > Re: New to WMIC

Reply
Thread Tools Display Modes

Re: New to WMIC

 
 
Pegasus [MVP]
Guest
Posts: n/a

 
      10-21-2009

"Tom Lavedas" <> wrote in message
news:512b7d7c-f012-466b-b2e5-...
On Oct 21, 10:21 am, Tom Lavedas <tglba...@cox.net> wrote:
> On Oct 21, 9:20 am, Tom Lavedas <tglba...@cox.net> wrote:
>
>
>
> > On Oct 20, 6:08 pm, RayRedSox <RayRedSox.40d...@DoNotSpam.com> wrote:

>
> > > I'm trying to write a batch file using WMIC to delete files out of the
> > > windows\temp folder on a remote server. So far I've done the
> > > following,
> > > but it keeps saying invalid query:

>
> > > WMIC /node:sup24app01 path cim_datafile WHERE "path='%WINDIR%\\TEMP\\'
> > > AND Extension ='tmp'" delete

>
> > > I'm taking precautions in case the windows temp folder is not on the C
> > > drive, hence the reason for the %WINDIR%. Can I use parentheses in
> > > WMIC
> > > command?

>
> > > Thank you.

>
> > > --
> > > RayRedSox
> > > ------------------------------------------------------------------------
> > > RayRedSox's Profile:http://forums.techarena.in/members/146555.htm
> > > View this
> > > thread:http://forums.techarena.in/server-scripting/1260851.htm

>
> > >http://forums.techarena.in

>
> > The SQL syntax is VERY particular. The PATH specification must be just
> > that, it cannot contain the drive letter. Plus, your original was not
> > escaping all of the backslashes. This is the way it worked for me ...

>
> > set tmp=%WINDIR:~2%\
> > set qry="drive='%WINDIR:~0,2%' and path='%tmp:\=\\%' and
> > extension='tmp'"
> > WMIC /node:'sup24app01' path cim_datafile WHERE %qry% delete

>
> > Note that I also had to enclose the machine ID in single quote marks
> > for it to work for me.
> > _____________________
> > Tom Lavedas

>
> It occurred to me just after I posted that this approach still has a
> fatal flaw. That is that the WINDIR variable needs to be determined
> for the target machine. The way it is written, it is accessing the
> local machine's environment, not the target machine. So I tried this
> query to get the information from the target machine ...
>
> set compname=sup24app01
> set qry="name='windir'"
> WMIC /node:'%compname%' environment WHERE %qry% get 'VariableValue' /
> value
>
> For my local machine test (I don't have domain admin privileges here),
> the result was ...
>
> VariableValue=%SystemRoot%
>
> Which is not helpful. So I altered the query to request the
> SystemRoot value instead and received a 'No Instance(s) Available.'
> for my troubles. I don't know why this happened and I have not yet
> figured out how to work around this problem. Maybe someone else does.
> _____________________
> Tom Lavedas


I think I have a viable solution now using the Win32_OperatingSystem
class instead ...

@echo off
setlocal
set compname=sup24app01
for /f "usebackq tokens=2 delims==" %%a in (
`WMIC /node:'%compname%' os get 'WindowsDirectory' /value`
) do set _WinDir=%%a
set _drv=%_WinDir:~0,2%
set _path=%_WinDir:~2%\temp\
set qry="drive='%_drv%' and path='%_path:\=\\%' and extension='tmp'"
WMIC /node:'%compname%' path cim_datafile WHERE %qry% delete

I did run into a 'Generic Error' when the procedure tried to delete a
file that was attached to a running process. I think the only way
around that is to get a list of the matching files and then work
through the list one file at a time, deleting them in turn.
Otherwise, the process is aborted at the first error, which may miss
files listed after the one in use. That is an exercise best left to
the student
_____________________
Tom Lavedas

=============

I tried your routine on a Win2000 machine. It works very well. Unfortunately
it is very slow, taking 8 seconds to execute. The VB Script I suggested
takes 1 second, but only when I remove the error I have discovered just now.


 
Reply With Quote
 
 
 
 
Pegasus [MVP]
Guest
Posts: n/a

 
      10-21-2009


"Tom Lavedas" <> wrote in message
news:9dbcc6f1-c046-4743-bbf4-...
On Oct 21, 2:11 pm, "Pegasus [MVP]" <n...@microsoft.com> wrote:
> "Tom Lavedas" <tglba...@cox.net> wrote in message

{snip}
>
> I tried your routine on a Win2000 machine. It works very well.
> Unfortunately
> it is very slow, taking 8 seconds to execute. The VB Script I suggested
> takes 1 second, but only when I remove the error I have discovered just
> now.


But doesn't your approach require the SystemRoot\Temp folder to be
shared? FSO can use the UNC, but unless a share permission to delete
files is granted, it can't do anything can it. The only way it would
be possible would be to be using domain admin credentials and go in
through the admin share (C$, D$, etc.). In fact, your script does not
even seem to be constructing a UNC - or am I missing something?

==========

My script relies on the admin shares C$, D$, except that it uses C: instead
of C$ due to the mistake I made. Seeing that the OP appears to treat this
newsgroup much like a candyshop where he can hold out his hand and get any
amount of free code/candy without even saying "Thank you", I will let him
work out the correction to my mistake by himself if he wants to use the
code. It did not take me much time to cobble it together but I suspect that
you put a fair bit of effort into your solution.



 
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59