Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Powershell openfiles.exe output

1,619 views
Skip to first unread message

Morphius

unread,
Sep 4, 2009, 5:30:01 AM9/4/09
to
Hello

Excuse what may be a stupid question but I am trying to output the result
from running openfiles.exe to either a correctly formatted .csv or a .html
file.

I can output the result to text without a problem but i need to show it in a
web page.

I have tried the export-csv and the convertto-html commands without any
success as it just exports a seemingly random string of numbers rather than
the list of which files are open.

I have included code snippits below if anyone has any suggestions?

openfiles /query /s \\fileserver <--- This shows the data
openfiles /query /s \\fileserver | convertto-html <--- This shows the
html but for meaningless data
openfiles /query /s \\fileserver | export-csv c:\test.csv <--- This
exports a csv but with meaningless data


Thanks for any help!

Marco Shaw [MVP]

unread,
Sep 4, 2009, 10:03:15 PM9/4/09
to
When you run a DOS-based command-line like this, what is outputted isn't
easily processed by PowerShell.

I don't have any shares I can use to provide a demo. Are you able to
provide some sample output from the command that you want to wrap into a CSV
or HTML?

Marco

"Morphius" <Morp...@discussions.microsoft.com> wrote in message
news:492FB882-EB12-4E25...@microsoft.com...

Morphius

unread,
Sep 7, 2009, 4:39:01 AM9/7/09
to
Hi Marco

The output I get when I run the command looks like this...

ID Accessed By Type Open File (Path\executable)
======== ==================== ========== =====================================
3626682 MSPENCER Windows D:\Data\DFSData\Malaysia Shared
3628212 PTIBBS Windows D:\..\609 PSR - Bechtel.XLS
3628566 MSPENCER Windows D:\..\H01-4101 & H02-4101
3628607 MSPENCER Windows D:\..607 ETD003D H01-2601 250809.xls
3630086 HWURTZ Windows D:\Data\DFSData\Malaysia Shared
3631001 HWURTZ Windows D:\..\From Client\07.09.09
3631766 PHALSTEAD Windows D:\..\Contract Invoicing Control.xls
3632791 ASNELLING Windows D:\..\GA\H01-4101-STACK.SLDPRT

However when I try to pipe it into html or csv the output comes out like
this..

PS C:\Users\dsmith> openfiles /query /s \\file1a | convertto-html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>HTML TABLE</title>
</head><body>
<table>
<colgroup>
<col>
</colgroup>
<tr><th>*</th></tr>
<tr><td>0</td></tr>
<tr><td>78</td></tr>
<tr><td>78</td></tr>
<tr><td>72</td></tr>
<tr><td>68</td></tr>
<tr><td>66</td></tr>
<tr><td>77</td></tr>
<tr><td>72</td></tr>
<tr><td>67</td></tr>
<tr><td>69</td></tr>
<tr><td>68</td></tr>
<tr><td>72</td></tr>
<tr><td>77</td></tr>
<tr><td>77</td></tr>
<tr><td>72</td></tr>
<tr><td>77</td></tr>
<tr><td>69</td></tr>
<tr><td>72</td></tr>
(truncated as it goes on forever in the same fashion)


What we really want is the html to work so we can list who has what files
open on a specific server and paste it on the intranet for people to see....

if there is an alternative code that would show this, that would also be
great!

Thanks

Thomas Lee

unread,
Sep 7, 2009, 5:48:35 AM9/7/09
to
In message <492FB882-EB12-4E25...@microsoft.com>,
Morphius <Morp...@discussions.microsoft.com> writes

>Excuse what may be a stupid question but I am trying to output the
>result from running openfiles.exe to either a correctly formatted .csv
>or a .html file.

I just tried this and get this error running the openfiles.exe program:

PS D:\Users\tfl> openfiles.exe
ERROR: The target system must be running a 32 bit OS.

I'm running Win7/x64

Thomas
--
Thomas Lee
doct...@gmail.com

Marco Shaw [MVP]

unread,
Sep 7, 2009, 7:28:55 AM9/7/09
to
Needs some tweaking below. You'd pass your command-line to $in.

Instead of splitting on spaces, perhaps using positional characters would be
best.

Is there a blank space before the output starts?

-------------------
$in=your_command

$out=@()

foreach($line in $in){
$obj=new-object psobject
$obj|add-member noteproperty ID $line.split("")[0]
$obj|add-member noteproperty User $line.split("")[2]
$obj|add-member noteproperty Type $line.split("")[15]
$obj|add-member noteproperty File $line.split("")[19]
$out+=$obj
}
$out
-------------------

From there, $out will be in a format that you can pass to export-csv or
convertto-html.

"Morphius" <Morp...@discussions.microsoft.com> wrote in message

news:68150345-A937-4258...@microsoft.com...

Morphius

unread,
Sep 7, 2009, 8:23:01 AM9/7/09
to
Hi Marco.

That is great and is getting me nearer but the output is still a bit broken.
Could that code be modified to seperate out the columns based on a comma as
that command can output in a csv type format.

Morphius

unread,
Sep 7, 2009, 8:38:01 AM9/7/09
to
Hello

Worked out the code to split it differently after all.

Next question, is it possible to sort the output are is that really pushing
my luck with powershell?

Thanks again for all your help!

Morphius

unread,
Sep 7, 2009, 10:02:01 AM9/7/09
to
Ok, so i get that $out | sort-object FILE will sort the results, and a filter
should be something like select-string 'CAD' would only select the entries
according to those with CAD in the file name however if i try to run the
following it breaks the code...

$in = openfiles /query /s \\file1a /fo CSV /v /nh | select-string 'CAD'
$out=@()

foreach($line in $in){
$obj=new-object psobject

$obj|add-member noteproperty User $line.split(",")[2]
$obj|add-member noteproperty Type $line.split(",")[5]
$obj|add-member noteproperty File $line.split(",")[6]
$out+=$obj
}
$out | sort-object File


It just generates the following:


+ $obj|add-member noteproperty File $line.split( <<<< ",")[6]
Method invocation failed because [Microsoft.PowerShell.Commands.M
At line:3 char:48
+ $obj|add-member noteproperty User $line.split( <<<< ",")[2]
Method invocation failed because [Microsoft.PowerShell.Commands.M
At line:4 char:48
+ $obj|add-member noteproperty Type $line.split( <<<< ",")[5]
Method invocation failed because [Microsoft.PowerShell.Commands.M
At line:5 char:48
+ $obj|add-member noteproperty File $line.split( <<<< ",")[6]


I am sure this is possible but I cant get the order of commands right and I
am a beginner when it comes to PS.

Morphius

unread,
Sep 7, 2009, 10:46:02 AM9/7/09
to

NeverMind..... Googling found me an answer / work around.

Running the commands in the following order preserves the formatting, but
loses the html, body and table tags so an additional bit of code added on the
bottom fixes the missing tags.

The following script should show who has what files open on server
FILE1A,sorted by directory name filtered for a certain string (CAD in this
case)...

$in = openfiles /query /s \\FILE1A /fo CSV /v /nh
$out=@()

foreach($line in $in){
$obj=new-object psobject
$obj|add-member noteproperty User $line.split(",")[2]
$obj|add-member noteproperty Type $line.split(",")[5]
$obj|add-member noteproperty File $line.split(",")[6]
$out+=$obj
}
$out | sort-object File

$html = $out | sort-object FILE | convertto-html
$html | select-string 'CAD' > C:\openfiles.html

${C:\openfiles.html} = @"
<html>
<body>
<table>
${C:\openfiles.html}
</table>
</body>
</html>
"@


Messy I am sure but gets the job done!

Thanks for all of your help!

Shay Levy [MVP]

unread,
Sep 8, 2009, 3:52:54 AM9/8/09
to

PS > openfiles /query /s \\FILE1A /fo CSV /v > openfiles.csv
PS > import-csv openfiles.csv | convertto-html | openfiles.html
PS > invoke-item openfiles.html

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

M> NeverMind..... Googling found me an answer / work around.
M>
M> Running the commands in the following order preserves the formatting,
M> but loses the html, body and table tags so an additional bit of code
M> added on the bottom fixes the missing tags.
M>
M> The following script should show who has what files open on server
M> FILE1A,sorted by directory name filtered for a certain string (CAD in
M> this case)...
M>
M> $in = openfiles /query /s \\FILE1A /fo CSV /v /nh
M> $out=@()
M> foreach($line in $in){
M> $obj=new-object psobject
M> $obj|add-member noteproperty User $line.split(",")[2]
M> $obj|add-member noteproperty Type $line.split(",")[5]
M> $obj|add-member noteproperty File $line.split(",")[6]
M> $out+=$obj
M> }
M> $out | sort-object File
M> $html = $out | sort-object FILE | convertto-html
M> $html | select-string 'CAD' > C:\openfiles.html
M> ${C:\openfiles.html} = @"
M> <html>
M> <body>
M> <table>
M> ${C:\openfiles.html}
M> </table>
M> </body>
M> </html>
M> "@
M> Messy I am sure but gets the job done!
M>
M> Thanks for all of your help!
M>
M> "Morphius" wrote:
M>

Morphius

unread,
Sep 8, 2009, 6:52:01 AM9/8/09
to
Hi Shay

Thanks for that, it is definately a nice clean solution which seems stupidly
simple now.

Only advantage to the messy script is it has enabled us to filter and sort
the results of the commant.

Thanks though!

Shay Levy [MVP]

unread,
Sep 8, 2009, 9:37:05 AM9/8/09
to
Hi Morphius,

You can sort after you import the csv. I see that I also missed the out-file
cmdlet at the end of the pipe:

import-csv openfiles.csv | sort <property> | convertto-html | out-file openfiles.html

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

M> Hi Shay
M>
M> Thanks for that, it is definately a nice clean solution which seems
M> stupidly simple now.
M>
M> Only advantage to the messy script is it has enabled us to filter and
M> sort the results of the commant.
M>
M> Thanks though!
M>
M> "Shay Levy [MVP]" wrote:
M>

Marco Shaw [MVP]

unread,
Sep 9, 2009, 9:52:06 PM9/9/09
to
Thanks Shay...

Yes, much easier. I should have stopped to think about this earlier...

Marco

"Shay Levy [MVP]" <n...@addre.ss> wrote in message
news:37162f6da03428...@news.microsoft.com...

Morphius

unread,
Sep 10, 2009, 6:48:01 AM9/10/09
to

Although the longer winded solution has helped me learn much more about how
the PS code works so it has definately been useful.

Your method also allows me to put a meta tag into the header which the
convertto-html doesnt like as it has too many " marks inside!

Thanks again

CodeSlinger

unread,
Jan 5, 2010, 4:08:01 AM1/5/10
to
Thomas Lee,

What exactly did you try. I ask because this is one of 3 hits on the entire
Internet with this message and which I get when I try to run an openfiles
command from Windows 7 on Server 2008 using my own software that runs remote
commands.

If you are just running "openfiles" in a win7 powershell, that works for me
so am hoping you are doing something different.

Thanks, Dave

Thomas Lee

unread,
Jan 6, 2010, 4:21:19 AM1/6/10
to
In message <26DFB5FB-9A91-4ED4...@microsoft.com>,
CodeSlinger <CodeS...@discussions.microsoft.com> writes
>Thomas Lee,

Hello!

>What exactly did you try. I ask because this is one of 3 hits on the entire
>Internet with this message and which I get when I try to run an openfiles
>command from Windows 7 on Server 2008 using my own software that runs remote
>commands.

Sorry - I am not sure what you are referring to - can you provide me
more details?

>If you are just running "openfiles" in a win7 powershell, that works for me
>so am hoping you are doing something different.
>
>Thanks, Dave

Not sure...

Robert Aldwinckle

unread,
Jan 10, 2010, 3:51:44 PM1/10/10
to

"Thomas Lee" <t...@psp.co.uk> wrote in message news:NLCTWSZPYFRLFA$6...@mail.psp.co.uk...

> In message <26DFB5FB-9A91-4ED4...@microsoft.com>, CodeSlinger <CodeS...@discussions.microsoft.com> writes
>>Thomas Lee,
>
> Hello!
>
>>What exactly did you try. I ask because this is one of 3 hits on the entire
>>Internet with this message and which I get when I try to run an openfiles
>>command from Windows 7 on Server 2008 using my own software that runs remote
>>commands.
>
> Sorry - I am not sure what you are referring to - can you provide me more details?


OP is using the web interface to newsgroups and not quoting
from a thread which has been flushed from msnews. ; ]
Perhaps gone from your server too? <eg>


>> [X-Newsreader: Microsoft CDO for Windows 2000]


9/4/2009 2:29 AM PST
http://www.microsoft.com/communities/newsgroups/en-us/default.aspx?dg=microsoft.public.windows.powershell&tid=492fb882-eb12-4e25-8dca-2b03cba84047&mid=361cc78d-f1eb-4a2b-82e8-7c023495cd64&cat=&lang=&cr=&sloc=&p=14

(BING search for
communities powershell openfiles lee
)


FYI

Robert
---

Thomas Lee

unread,
Jan 11, 2010, 1:04:11 PM1/11/10
to
In message <u1jM5ajk...@TK2MSFTNGP06.phx.gbl>, Robert Aldwinckle
<rob...@techemail.com> writes
>

>> Sorry - I am not sure what you are referring to - can you provide me
>>more details?
>
>
>OP is using the web interface to newsgroups and not quoting
>from a thread which has been flushed from msnews. ; ]
>Perhaps gone from your server too? <eg>

Ah...

>>>If you are just running "openfiles" in a win7 powershell, that works for me
>>>so am hoping you are doing something different.
>>>
>>>Thanks, Dave
>>
>> Not sure...

I think I must have run that command from a 32-bit PowerShell prompt,
not the 64-bit. Just tired it now and i runs fine from 64-bit
PowerShell, but gives the same error in 32-bit.

0 new messages