0

I'm using onenote as a documentation tool for some trivial tests and I sometimes use scripts. I was using aside multiple scripts for automating stuff.

I would like to include these scripts in Microsoft OneNote (it is possible to include file in Onenote pages). Then the objective is to execute the script from there.

It is not working; after double-clicking, a powershell window appear and disappear instantly.

Any ideas?

Edit The error message was caught in a screenshot:

enter image description here

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
  • Does [PowerShell: Press any key to continue](https://stackoverflow.com/questions/20886243/press-any-key-to-continue) give you any inspiration? – Andrew Morton Jan 19 '20 at 16:45
  • @AndrewMorton: I think you're assuming that the O.P. is asking how to make the script pause. Might not be the case. – VA systems engineer Jan 19 '20 at 16:51
  • Might be that the PS window appears and disappears instantly because whatever your script is executing very fast. The PS window persists for only as long as it takes the script to execute. What does your script do? Is it doing it? If not, you may want to temporarily add a pause as suggested by Andrew just to see if your script is throwing an error (exceptions are displayed in red in the PS window). If it is, debug the script using the [ISE](https://docs.microsoft.com/en-us/powershell/scripting/components/ise/introducing-the-windows-powershell-ise?view=powershell-7), then try again in OneNote. – VA systems engineer Jan 19 '20 at 16:54
  • Hi, welcome to stackoverflow. See [How to ask](https://stackoverflow.com/help/how-to-ask) if you want to raise your chances to get an answer. We'd expect to see code of what you tried and what is going wrong (error message, weird behaviors, etc) in order to assist you. To give you a general answer, yes it is possible. You'd need some Powershell in the first place to query OneNote API, retrieve the script and execute it. Look into one note API doc to get started.: https://learn.microsoft.com/en-us/graph/integrate-with-onenote, – Sage Pourpre Jan 19 '20 at 19:36
  • Once you have obtained your script from the API, you can do something like Chocolatey `Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))` except you'd replae the `DownloadString` bit for a variable or expression containing the result of your OneNote api call. – Sage Pourpre Jan 19 '20 at 19:37
  • https://stackoverflow.com/a/58245206/45375 shows you how to redefine the default operation (e.g. by double-clicking) for `*.ps1` files, via the registry, so that scripts open in a PowerShell window that stays open after execution. If this is what you're looking for and it works for you, we can mark your question as a duplicate. – mklement0 Jan 19 '20 at 19:39
  • Oh... also, if the window disappear instantly, for debug purpose, start your Powershell console or IDE then call the script from there. The script will still exit instantly but the error message will persist in the console output. – Sage Pourpre Jan 19 '20 at 19:41
  • Hello, Thank you for your answer. This is problem seems like code independent. For explanation purpose let's take the code below : `$test = Read-Host -Prompt 'Test'` The path of the file is : C:\Users\TomSnow\AppData\Local\Temp\OneNote\16.0\Exported\{750F0FF0-E69B-4FC4-AE09}\NT\8 1. When executed from powershell prompt : it is working. Result : PS C:\Users\TomSnow\AppData\Local\Temp\OneNote\16.0\Exported\{750F0FF0-E69B-4FC4}> .\Final_test.ps1 Test: aze 2. When executed directly from the folder with double-click : it is not working Result : window appear and disappear G – gonepap Jan 19 '20 at 20:54
  • Executing the file directly with double click in the path C:\Users\TomSnow\AppData\Local\Temp\OneNote\16.0\Exported\{750F0FF0-E69B-4FC4-AE09}\NT\8 is the same as double-clicking the script in onenote. It seems like onenote stores attachment there. – gonepap Jan 19 '20 at 20:57
  • Also please note that the objective is not to retrieve a script from onenote api. – gonepap Jan 19 '20 at 21:02
  • You are saying you double click on the script to run it. I assume you changed the .ps1 file association to run `powershell.exe` instead of notepad? If so what arguments are you using? Do you have a `$profile` setup that is interfering? – Brandon McClure Jan 21 '20 at 21:53

0 Answers0