0

I have been working on my first .NET Core console program and am working to make it operate through multiple PowerShell scripts. When manually executing my FileRelocation program from PowerShell, I go to the appropriate directory and enter the following into the terminal:

dotnet .\FileRelocation.dll [commands for .dll]

This executes without any issue. However, when trying this from a .ps1 file, I am always given the following error:

dotnet : No executable found matching command "dotnet-.\FileRelocation.dll"
At \\chimera\home\freerey\My Documents\C# Projects\File Relocation Program\0.5.1 Beta\scripts\pstest.ps1:2 char:1
+ dotnet .\FileRelocation.dll -v -e C:\Users\freerey\Desktop\firelloc ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (No executable f...Relocation.dll":String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

Since initially running into this issue, I have tried writing dotnet ef in the front, only to be told Microsoft.EntityFrameworkCore.Tools.CommandException: No project was found. I have tried running the .csproj file for my program instead of the .dll, but this results in the same outcome.

I feel like I'm missing out on something very obvious here, but due to my lack of experience with PowerShell and the fact that nobody I work with uses it, I still haven't found out why the program fails to run from a script when manually entering this data in works perfectly. The program works completely as intended and I know there isn't any issue with it; I just need to figure out how to get these scripts to stop having errors.

And for anyone curious: I'm running .NET Core 2.1.507, with these runtimes installed:

  • Microsoft.AspNetCore.All 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  • Microsoft.AspNetCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  • Microsoft.NETCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]

Thanks in advance!

3
  • 1
    Check the character between dotnet and .\File_Relocation.dll is really a space character - it looks like PowerShell thinks the command you want it to execute is called dotnet-.\FileRelocation.dll rather than dotnet with a parameter .\File_Relocation.dll so the "space" might be some weird unicode character that looks like a space but PowerShell thinks is part of the executable's filename. Try deleting and re-keying dotnet .\File_Relocation.dll again in the script file to make sure... Commented Mar 12, 2021 at 20:36
  • @mclayton I did try that because I noticed there was some weird issue with spacing, but and it still returns the same result regardless of my erasing and re-writing the line. Commented Mar 12, 2021 at 20:48
  • 3
    Actually, the error might be coming from dotnet rather than powershell - see similar errors in stackoverflow.com/questions/40941717/… and stackoverflow.com/questions/51181148/… - as a test, try using the full path to File_Relocation.dll instead of the relative path .\File_Relocation.dll. Commented Mar 12, 2021 at 21:01

1 Answer 1

1

You gotta remember that when PowerShell is running it is running at the user level until told otherwise. There are two different permissions one as if you were running as an administrator.

C:\Windows\system32

and one that's user-level

C:\Users\UserName

In other words, you have to treat your PS1 script as if you opened it for the first time. There are many ways to resolve this issue but if you tell the script exactly where to run the file it will work.

So the PowerShell I script to test my DotNet 2.1 console app that says hello ends up being (demo.ps1)

dotnet "C:\Users\Neil\Source\Repos\DemoForStackOverFlow\DemoForStackOverFlow\bin\Debug\netcoreapp2.1\DemoForStackOverFlow.dll"

Now remember that your application could be more complicated than this and you could pass parameters into it, but that's likely out of scope for this context.

If you need more help attach the ps1 script to this post and we can go from there

enter image description here

Sign up to request clarification or add additional context in comments.

5 Comments

Clarifying the exact location of the .dll was the problem all along! I'm going to wait a bit before I mark this as the answer because we're still not entirely sure where the .dll is going to go, but this is great, ty!
@Freerey I would be curious as to why you went with this particular solution. Remember that you could write a console app that could take in parameters that you could pass to organize files. For that matter, you could write a windows app (Win forms) that does the same thing. I would urge against executing PowerShell scripts with other PowerShell scripts as the main solution as things could get messy really quickly.
This project was meant to replace an old VB program we had that did the same thing. I thought using .NET Core (which nobody else in my workplace has done) would give the program some longevity, and the program can also email me whenever it moves files over -- which isn't even something the VB version did.
@Freerey I forgot to respond earlier in the week sorry about that. If your app needs to move files on a schedule you could easily add it to the windows scheduler to run every day, hour, minute ETC. You can keep your app as is and would take some explaining to set up a windows scheduler. Feel free to add me on discord Neil Busse#8054. Keep in mind you can also write code within the app as well to keep track of time. Lot's of different ways to do the same thing. But I would say this app most likely belongs on a server that is responsible for running tasks would be my opinion.
we actually got the script to run from a server with windows scheduler and now it's all working properly :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.