Summary: Microsoft MVP Sean Kearney teaches a cool trick to work around legacy redirection issues in Windows PowerShell.
Weekend Scripter: Legacy Man Strikes Again
Microsoft Scripting Guy Ed Wilson here. Once again, Microsoft PowerShell MVP Sean Kearney has been writing some guest blog posts. He continues with his thoughts on legacy and Windows PowerShell. Here is a link to the beginning of the legacy posts. Now, on to today’s blog.
The Scripting Guy picks up his phone. It’s campus security in Redmond.
“Sir, there’s a large ruckus coming from the Scripting Room. We think you should check into it. Youknowwho upstairs is asking questions about the noise.”
The Scripting Guy nods. Normally a quiet place, the Scripting Room never hears more than a tap-tap on the keyboard. Occasionally a “w00t!” but that is all. Calmly placing upon his body the ceremonial robes as the Script Master, he walks toward the room. As security reported, there was indeed a ruckus.
“….Aiaighiahgihiahihgaighihgiahi!!!!!! It won’t Work! It’ WON’T WORK! Aaihgihaighihiahghighai!!!!”
A mostly bald creature running about madly with arms flailing about and moving like Benny Hill is scampering about in a panic. It is the student. He turns and sees the Scripting Guy.
“….. IT’S NOT GOING TO WORK!!! NO! IT WON’T WORK!!! AIGIAHGIHAHHGHAIHGIHH!!!”
The Howl echoes through the halls of Redmond into its darkest corner.
Scripting Guy lifts his finger and touches the student’s head and mutters something. The young scattered one pauses and calms down. Scripting Guy nods thinking it was a good thing he watched that “Crocodile Dundee” movie the other night. “The trick actually works!
He looks at our little friend. “What seems to be the problem?”
“MASTER!” he blurts out. “I need to pass text DIRECTLY to an old console command in Windows PowerShell, b-but it doesn’t work!”
Calm as the wind, the Scripting Guy listens. “Show me what you are trying to do, young cockroach.”
“Well, in DOS if I wanted to pass a typed command directly to a program, I could do something like this with redirection from a file containing the necessary commands”:
GPUPDATE /FORCE <C:CAVEBATMAN.TXT
“In which case, if the file contained an “N” it would type it in for the next prompt. Normally, that command would prompt for a logoff and this was how to pre-answer the answer.”
The Scripting Guy nods. “I remember this well.”
“But! I tried it in Windows PowerShell and got this message!: ‘The '<' operator is reserved for future use.’”
The Scripting Guy grabs his Windows Phone 7 to jot down a note to speak to the Windows PowerShell team about what “future use” is and why he wasn’t informed of it. “You do realize you could STILL run it the old way, don’t you?”
The student pauses. “Huh?”
The Master continues. “If we need to run a legacy application in a way that only works in the console world from Windows PowerShell, we need to launch the application WITHIN the console world. You should have read online. There was a WHOLE SERIES on the Hey, Scripting Guy! Blog about how to make legacy applications work with Windows PowerShell. Did you not study? For example, if the feature you need seems to work only in the console, you can just RUN it there like this”:
CMD /c “GPUPDATE /FORCE <C:CAVEBATMAN.TXT”
“This would cause the application to run EXACTLY as it did before.”
Our troublesome student looks up. “That feels like cheating!”
The Scripting Guy scratches his head. This one IS trouble. I must make a note to speak to those DPE fellows in Canada about him.
“Well, there is ANOTHER way we used to do it in DOS. Would it interest you to know that STILL works in Windows PowerShell? You ECHO the character and PIPE it to the application”:
ECHO N | GPUPDATE /FORCE
“This command works IDENTICALLY in Windows PowerShell as well as CMD.EXE. But of course, I didn’t show you the coolest trick. I fibbed. There IS a way to make your first method work in Windows PowerShell. Windows PowerShell has a newer way. We just use GET-CONTENT instead and pipe that output directly to the command instead of using the older redirector”:
GET-CONTENT C:CAVEBATMAN.TXT | GPUPDATE /FORCE
The student is floored. Again, the Master has shown that it all still works.
“And now young one, for disturbing naptime at Microsoft you must be punished. I want you to go upstairs now and count all the semicolons the developers dropped in all the buildings. Hop to it or I shall not teach you any more scripting this week.”
The student bolts off into the night, emboldened by a newer knowledge of Windows PowerShell and the fact he’ll be eating semicolons for the next week. “Crunchy,” he thinks to himself.
Thank you, Sean, for providing today’s article. Join us tomorrow for another lesson on the legacy and Windows PowerShell with Sean leading the way.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at email@example.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy