Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

detect when running scripts is disabled on Windows #2

Open
gfspiteri opened this issue Dec 26, 2022 · 15 comments
Open

detect when running scripts is disabled on Windows #2

gfspiteri opened this issue Dec 26, 2022 · 15 comments
Labels
bug Something isn't working enhancement New feature or request

Comments

@gfspiteri
Copy link

I am trying to used the to_pdf() function to convert a word document to pdf. I kept getting an error with my docx file so tested with the examples from the documentation:

docx_file <- system.file(package = "doconv", "doc-examples/example.docx")
to_pdf(docx_file, output = "docx_example.pdf")
Error: could not convert C:/Users/gspiteri/AppData/Local/R/win-library/4.2/doconv/doc-examples/example.docx

doconv::msoffice_available() returns TRUE

I am using this version of Word: Microsoft® Word for Microsoft 365 MSO (Version 2210 Build 16.0.15726.20188) 64-bit

Appreciate any help. Thanks and Merry Christmas!

@davidgohel
Copy link
Member

Hello

Thanks and happy new year :)

Sorry, I only have one clue for now:

I remember a colleague had to switch on a permission to execute PowerShell. On Windows, a PowerShell script is launched and it uses Component Object Model from Microsoft.

Could you check your permissions if any relative to that?

@davidgohel
Copy link
Member

when you "thumb up", does that mean this was the problem and it worked or do that mean "I'll check"? :)

@gfspiteri
Copy link
Author

Means I'll check!

Do you have some a sample PowerShell script I could test?

@davidgohel
Copy link
Member

here is a sample https://powershellbyexample.dev/post/hello-world/

@gfspiteri
Copy link
Author

Oh yes, sorry wasn't clear. I can use PowerShell and the examples you linked work (I tested a few different commands). I meant, do you have a PowerShell script using the Component Object Model I could test.

@davidgohel
Copy link
Member

(Not tested as I don't have Windows for now), change $Filename to an existing name, it should be opened by the script

$Filename='C:\anypath\doc.docx'
$Word=NEW-Object –comobject Word.Application
$Document=$Word.documents.open($Filename)

@gfspiteri
Copy link
Author

Yes this works.

@davidgohel
Copy link
Member

ok, sorry, I have no more clue for now

@gfspiteri
Copy link
Author

I will try to look at the code later this week and see if I figure out where the problem is.

@gfspiteri
Copy link
Author

gfspiteri commented Jan 16, 2023

It was a problem with permissions. Running this adapted from docx2pdf_win():
system2("powershell", args = c("-file", script_path))
gave
File C:\Users\xxxx\AppData\Local\Temp\RtmpEHDrN9\file718416e4da3.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170. + CategoryInfo : SecurityError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : UnauthorizedAccess

@davidgohel
Copy link
Member

Haaaa! perfect, thanks for the feedback

I'll try to add a function to test this case

@davidgohel davidgohel added bug Something isn't working enhancement New feature or request labels Jan 16, 2023
@davidgohel davidgohel changed the title to_pdf() Error: could not convert C:/Users/xxxx/AppData/Local/R/win-library/4.2/doconv/doc-examples/example.docx detect when running scripts is disabled on Windows Jan 16, 2023
@FGL-Linear
Copy link

I had the same problem. My execution policy was set to "RemoteSigned". To try it out, I set it to "Unrestricted" and it worked.

I'm not well versed in PowerShell or IT security, so I'd like to ask if what I did is too risky and if there's a better way to enable the scripts used by doconv while keeping "RemoteSigned" policy.

@davidgohel
Copy link
Member

Thanks for your comment

I would say, if you use the CRAN version, the risk of getting a hacked version is low, specially if you are in a managed and administrated environment. But if you switch that thing off, then you're exposed to other vulnerabilities. Your IT may recommend you not to do so if you ask them. I don't think my script is dangerous but some others may be.

I use the product to help me with doc and visual testing. I am not really able to advise on this subject.

@FGL-Linear
Copy link

Thank you very much for your quick reply. I'll ask IT then. I guess I could keep that policy as it was and switch it only when I need to run doconv.

Anyways, thank you for this and your other packages. They are really helpful!

markheckmann added a commit to markheckmann/doconv that referenced this issue Oct 3, 2024
Stop if PowerShell (PS) execution strategy does not allow running scripts and advise user. PS scripts are required for certain actions on Windows (ardata-fr#2).
@markheckmann
Copy link
Contributor

After going slightly mad about the functions not executing on Windows 😭 , I finally stumbled across this issue.
It would be really good to advise the user what is wrong and save them some tears 😆

I prepared this PR #9

davidgohel added a commit that referenced this issue Oct 6, 2024
Internals: Fail with clear error message if scripts not allowed by PS execuction policy (#2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants