Script to check if a file is open on another virtual machine

rated by 0 users
This post has 3 Replies | 1 Follower

Top 500 Contributor
Posts 11
Chris Posted: 02-13-2012 8:06 AM

Hello,

initially I wanted to test a scriipt on a Windows Server 2003, but since I don't have the environment to do, I want to run it on two virtual Windows 7 machines.

The script code below, running as a logon script, should check if the file stored in the variable $fileName is open on the other machine. In case it's true a popup box should open to brief a user on the other machine that the file is open.

I'm pretty unsatisfied because something is wrong with the script. Would anybody kindly help me please? Maybe I'm completey wrong.I think I miss the forrest for the trees.

Thank you very much!

Chris

 

$fileName = "\\server\netlogon\testfile.txt"
$file = New-Object -TypeName System.IO.FileInfo -ArgumentList $fileName
$ErrorActionPreference = "SilentlyContinue"
[System.IO.FileStream]
$fs = $file.OpenWrite();

if ($?)
{
    #file open =true
   
    $msg = New-Object -ComObject WScript.Shell
    $msg.Popup("File $filename is open and you are not permitted to edit!", 5, "Message", 0)
   
}

    else
{
    #file open = false
   
    $fs.Dispose()   
    $msg="Accessible to write"
    $msg
}

Top 25 Contributor
Posts 296
Microsoft MVP
Top Contributor

Using openwrite() will only work for the machine the file is on.  My testing shows it doesn't work across a UNC path

Top 500 Contributor
Posts 11

Thank you!

Could you give me a hint how to realize what I intend to do please?

Top 25 Contributor
Posts 296
Microsoft MVP
Top Contributor

I've got an idea that I'll try out tomorrow and let you know

Page 1 of 1 (4 items) | RSS
Copyright 2012 PowerShell.com. All rights reserved.