dfinke / edge-ps

PowerShell compiler for edge.js

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

WMI ends up with multiple executions

brianehlert opened this issue · comments

I am using a Node.js app to launch the edge-ps and invoke the following PowerShell script:
$printers = Get-WmiObject -Class Win32_Printer -Filter "Name LIKE '%$printerName%'"
$printers[0].SetDefaultPrinter()

If $printers returns with only one object I see multiple SetDefaultPrinter() events in the event log. (usually two)
If $printers returns with an array of objects I see many SetDefaultPrinter() events in the event log. (anywhere from 11 - 14 if the array only contains two objects)
In this second case both printers in the array are attempted at being set. And I have verified that I am only getting one object, and the correct object.

I didn't know if there was some interaction that I needed to be aware of.

Don't know of any interactions. You may want to check this out. https://github.com/bitsofinfo/powershell-command-executor

It is far more active and the SDK I built this with is not.

Thanks Doug.
Looking back at the Edge.js documentation, it appears that it does not require edge-ps to call PowerShell. http://tjanczuk.github.io/edge/#/16
Is that correct?

(I have to admit, I am stretching my abilities a bit as I am trying to sort out someone else's code problems.)

Correct. edge-ps needs edge.js not the other way.

I finally got the set printer looping sorted out.
Still do not understand why it was doing it in the first place.
And, yes, edge does require edge-ps for PowerShell support. Keep it going!
I have an interesting scenario for a mode messaging agent and PowerShell - using fibers to spawn off non-blocking PowerShell commands.

Glad to here. How did you solve it?

Would love to hear more about the agent.

I totally re-wrote the PowerShell that the developer left me with.
I added a number of tests and more feedback.

The PowerShell part looks like this:

$allPrinters = Get-WmiObject -Class Win32_Printer | Sort-Object -Property Name

$printers = @()

foreach ( $printer in $allPrinters ) {
if ( $printer.Default ) { "Printer " + $printer.Name + " is the default printer" }
if ( $printer.Name -match $inputFromJS ) { $printers += $printer }
}

if ( $printers[0] -ne $null ) {
if ( !$printers[0].Default) {
"Setting " + $printers[0].Name + " as default printer"
$printers[0].SetDefaultPrinter() | Out-Null
} else { $printers[0].Name + " already was the default printer" }
} else { "No matching printer was found" }

Clear-Variable printers, allPrinters

I will blog about the agent once I get it all together.