Solved

Powershell - "open with" equal

Posted on 2010-08-28
5
1,175 Views
Last Modified: 2012-05-10
I have several XML files that are encrypted and require opening with a specific application.  I would like to use powershell to:

1. open the file in the specific application
2. save the file as txt or xml
3. parse the file using XML controls.

I will address step 3 later.  1 and 2 is where I could use some help. Is there a switch or bar I could add to a get cmdlet to specify an application to access a file with?

The application I need to use is not an installed app.  It is a single exe file.
0
Comment
Question by:kabaam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 13

Accepted Solution

by:
soostibi earned 500 total points
ID: 33550664
Let's assume, that your specific application is notepad.exe. You can open a file from PowerShell:
$sh = new-object -com wscript.shell
$sh.Run("notepad c:\ee\10.txt")

(it might not work for you app, but you have to test it. You could also open it by:
notepad c:\ee\10.txt
but we have to further control the app, see below.)

To save it, it's a bit harder to do. But it might work like this with the help of the wscript.shell com object and sending keys to the app:

$sh.AppActivate("notepad")
$sh.SendKeys("%(F)A")                   # send Alt+F and A to notepad to open the Send As dialog
start-sleep 1                              # give time for the dialog to open
$sh.SendKeys("abcdefghijk.txt")      # send a filename to Save As dialog
start-sleep 1                              # wait till the characters are entered
$sh.SendKeys("~")                        # send an Enter
0
 
LVL 11

Author Comment

by:kabaam
ID: 33550839
wow, that is insanely closer to what I need than I thought I would get.  One additional request if you can?

in the save as dialog box I would like to change the default file type to txt.

the above works great...thanks
0
 
LVL 11

Author Comment

by:kabaam
ID: 33550872
Okay, I am almost there.  I added these two lines and it almost works but 1 flaw.
the Alt-f is applied to the window with focus.  Once the app opens it loses focus to the script.

$sh.AppActivate($applicationName)
$sh.SendKeys("%(F)A")                   # send Alt+F and A to notepad to open the Send As dialog
start-sleep 1                              # give time for the dialog to open
$sh.SendKeys("$newFile")      # send a filename to Save As dialog
$sh.SendKeys("{tab}")
$sh.SendKeys("t")
start-sleep 1                              # wait till the characters are entered
$sh.SendKeys("~")                        # send an Enter

thanks a bunch
0
 
LVL 11

Author Comment

by:kabaam
ID: 33550936
ahh.. figured it out.  
First up was the fact that my app doesn't like the use of atl-F shortcut.  So what I did was :
send Alt
send down
send down
send down
send down
send enter

this still did not allow focus on the correct app and I found that was due to the application was not fully awake yet so I put in a two second sleep.

Thank you for the code and especially for adding the comments so it was easy for me to figure out and to experiment on.



$sh = new-object -com wscript.shell

$sh.Run("$applicationName $filename")                           #open application with specific file
start-sleep 1                                                   #give time for application to open
$sh.AppActivate($strApplicationName);$sh.SendKeys("%")          #sends an ALT key press
$sh.AppActivate($strApplicationName);$sh.SendKeys("{DOWN}")     #sends a Down arrow key press 
$sh.AppActivate($strApplicationName);$sh.SendKeys("{DOWN}")     #sends a Down arrow key press              
$sh.AppActivate($strApplicationName);$sh.SendKeys("{DOWN}")     #sends a Down arrow key press 
$sh.AppActivate($strApplicationName);$sh.SendKeys("{DOWN}")     #sends a Down arrow key press 
$sh.AppActivate($strApplicationName);$sh.SendKeys("~")          #sends an Enter key press 
start-sleep 1                                                   #give time for the dialog to open
$sh.AppActivate($strApplicationName);$sh.SendKeys("$newFile")   #send a filename to Save As dialog
$sh.AppActivate($strApplicationName);$sh.SendKeys("{tab}")      #sends a Tab key press 
$sh.AppActivate($strApplicationName);$sh.SendKeys("t")          #sends an "T" key press 
start-sleep 1                                                   #wait till the characters are entered
$sh.SendKeys("~")                                               #send an Enter

Open in new window

0
 
LVL 11

Author Comment

by:kabaam
ID: 33550938
for those that found this question on a search check out this link for many more sendkeys options
http://technet.microsoft.com/en-us/library/ff731008.aspx
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A procedure for exporting installed hotfix details of remote computers using powershell
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question