0

Currently I am trying to run a test that will power-down the machine abruptly when a file is being written. When power-down occurs the last part of data is not present as it is never flushed to disk before power down.

Perl (in Linux) allows direct un-buffered writes when a file is opened in the following manner.

sysopen(F, $file, O_WRONLY|O_DIRECT) or die "Couldn't direct open file $file\n";

This allows data to be written till the last character in-case machine crashes due to some reason. Likewise, for windows, C++ allows direct writes when a file is opened for writes using "FILE_FLAG_NO_BUFFERING" flag.

How can I allow direct/un-buffered writes to disk using powershell ?

Pranav
  • 675
  • 1
  • 8
  • 13

1 Answers1

1

You could drop down to .NET and use a FileStream object to write the file. Use the constructor that takes FileOptions and try the FileOptions.WriteThrough enum value, e.g.:

$fileStream = New-Object System.IO.FileStream($file,
    [System.IO.FileMode]::Create, 
    [System.Security.AccessControl.FileSystemRights]::Modify,
    [System.IO.FileShare]::None,
    1,
    [System.IO.FileOptions]::WriteThrough)
Community
  • 1
  • 1
Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • An example: `$fileStream = New-Object System.IO.FileStream($file, [System.IO.FileMode]::Create, [System.Security.AccessControl.FileSystemRights]::Modify, [System.IO.FileShare]::None, 1, [System.IO.FileOptions]::WriteThrough)`. I don't feel confident enough to edit a 32.8k user's answer! – Simon MᶜKenzie Aug 23 '12 at 09:01
  • 1
    @SimonMcKenzie Bah, go for it. :-) The unfortunate downside of answering questions on a tablet is that it's painful to provide lengthy answers. :-( – Keith Hill Aug 23 '12 at 16:55