windowsWriteScript function
Writes the script's stdin, as raw bytes, to file path: into a temporary
file first, and onto path only once all of it has arrived; the
temporary file is removed either way.
path is opened as it is (OpenOrCreate) and emptied, then filled: the
file itself stays, with its attributes. Replacing it — File.Copy over
it, or FileMode.Create — would be refused for a Hidden or System file,
and would take on the temporary file's attributes.
Implementation
String windowsWriteScript(String path) => _psTry(
'\$t = [IO.Path]::GetTempFileName(); '
'try { \$i = [Console]::OpenStandardInput(); '
'\$f = [IO.File]::Create(\$t); '
'try { \$i.CopyTo(\$f) } finally { \$f.Dispose() }; '
'\$s = [IO.File]::OpenRead(\$t); '
'try { \$o = [IO.File]::Open(${_psq(path)}, [IO.FileMode]::OpenOrCreate, '
'[IO.FileAccess]::Write); '
'try { \$o.SetLength(0); \$s.CopyTo(\$o) } finally { \$o.Dispose() } } '
'finally { \$s.Dispose() } } '
'finally { [IO.File]::Delete(\$t) }',
);