C
C#4w ago
The Guy

C# pnputil not recognized

public void InstallPrinter(PrinterInfo printer) { string script = $@"script to install printer. install driver using pnputil." using (PowerShell ps = PowerShell.Create()) { ps.AddScript(script); try { var results = ps.Invoke(); if (ps.HadErrors) { foreach (var error in ps.Streams.Error) { Console.WriteLine($"Error: {error}"); } } else { foreach (var result in results) { Console.WriteLine(result.ToString()); } } } catch (Exception ex) { Console.WriteLine($"An error occurred: {ex.Message}"); } } I checked the log and the error occur during the driver installation and it's because it doesn't recognize pnputil. No problem running in powershell console.
15 Replies
jcotton42
jcotton424w ago
show the script you're trying to run $code
MODiX
MODiX4w ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
jcotton42
jcotton424w ago
but ps1 instead of cs @The Guy
The Guy
The Guy4w ago
the entire script?
jcotton42
jcotton424w ago
at least the pnputil bits
The Guy
The Guy4w ago
oh okay
jcotton42
jcotton424w ago
also the exact error message you're getting would help
The Guy
The Guy4w ago
BlazeBin - syfsaaqkdzoo
A tool for sharing your source code with the world!
The Guy
The Guy4w ago
2024-06-10 14:41:41 - Failed to add driver to the driver store. Error: The term 'pnputil' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. 2024-06-10 15:10:11 - Driver added to the driver store successfully. 2024-06-10 15:10:12 - Failed to register printer driver. Error: The specified driver does not exist in the driver store.
jcotton42
jcotton424w ago
huh, yeah that looks fine...
jcotton42
jcotton424w ago
I will say, you may want to use raw strings for your own sanity https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/raw-string
Raw string literals - ''' - C# reference
Raw string literals can contain any arbitrary text without the need for special escape sequences. You begin and end a raw string literal with a minimum of three double-quote characters.
jcotton42
jcotton424w ago
like, this line here
pnputil /add-driver '{{printer.DriverPath}}' /install
pnputil /add-driver '{{printer.DriverPath}}' /install
I assume you meant to interpolate that, but the double brace means it's escaped, so no interpolation
The Guy
The Guy4w ago
so just wrap the string script in 3 double quotes? or do i need to make any other adjustment?
jcotton42
jcotton424w ago
I would do
var script = $$"""
"quotes work like normal"
{single braces don't need escaping}
{{double braces trigger interpolation (because two $)}}
""";
var script = $$"""
"quotes work like normal"
{single braces don't need escaping}
{{double braces trigger interpolation (because two $)}}
""";
(ignore the weird coloring, discord's formatter hasn't been updated)
The Guy
The Guy4w ago
okay, i will try that. thanks!