Monday, March 16, 2009

This will be worth some more investigation.  I can send a Powershell array of cmd.exe strings to the cmd.exe interpreter and pass cmd.exe a Powershell "here string" that will passthru a Powershell variable to the cmd.exe interpreter. Be interesting to next see if I can reverse the process.


# writes out time and date from cmd.exe
write "Time and Date from CMD.EXE:" 
$Global:command = 
"time /t",
"date /t"
out-file -inputobject $command -encoding ASCII -filepath $pwd\cmd.txt
Start-Process cmd.exe -argument /Q -nonewwindow -wait -redirectstandardinput $pwd\cmd.txt

#set date in cmd.exe with UNIX style 'get-date' 
write "Set cmd.exe date with (get-date -uformat)"
$DNT= get-date -uformat %m/%d/%Y
$Global:command =
@"
date $DNT
"@
out-file -inputobject $command -encoding ASCII -filepath $pwd\cmd.txt
Start-Process cmd.exe -argument /Q -nonewwindow -wait -redirectstandardinput $pwd\cmd.txt

This also works well to enter multiple lines to be interpreted by cmd.exe:

function copy_con {[console]::In.ReadToEnd()}
write "Enter lines into copy_con to be interpreted by cmd.exe. End the list with CTRL-C:"
$Global:command =  copy_con
out-file -inputobject $command -encoding ASCII -filepath $pwd\cmd.txt
Start-Process cmd.exe -argument /Q -nonewwindow -wait -redirectstandardinput $pwd\cmd.txt
prompt

This doesn't work yet, but probably could with some work:

function Start-Cmd($parameter1) {Start-Process cmd.exe -argument /Q -nonewwindow -wait -redirectstandardinput $parameter1}
Start-cmd dir
Start-Process : This command cannot be executed because either the parameter 'RedirectStandardInput 'D:\PS1\dir'' has an invalid value or cannot be u
sed with this command. Give a valid input and Run your command again.
At line:1 char:47
+ function Start-Cmd($parameter1) {Start-Process <<<<  cmd.exe -argument /Q -nonewwindow -wait -redirectstandardinput $parameter1}
    + CategoryInfo          : InvalidOperation: (:) [Start-Process], FileNotFoundException
    + FullyQualifiedErrorId : FileNotFoundException,Microsoft.PowerShell.Commands.StartProcessCommand

No comments: