0

I have a situation. I'm installing network printer on remote desktops using a batch file that calls up a Powershell script, but after printer is installed on the remote desktop, Powershell returns nothing - so my batch file wont be able to continue...I must manually hit ENTER twice to make it echo DONE and run the rest of the scripts. Any idea why?

@echo off

Copy /Y "\\AddPrinter\AddPrinter.ps1" "C:\scripts" 

powershell.exe -ExecutionPolicy Bypass -Command "C:\scripts\AddPrinter.ps1" 

Echo DONE

Del /F /Q "C:\ntfs3\scripts\AddPrinter.ps1"

This is my powershell script, I even put a exit in the end of script, but powershell on remote pc just wont exit.....so it will not return to batch

$PrinterPath = "\\server-01\Printer0101"
$net = new-Object -com WScript.Network
$net.AddWindowsPrinterConnection($PrinterPath)
exit

BTW, I m using psexec to call up this batch file with a txt file containing all pc names.

psexec @%1 -u admin -p xxxxxx -c -f "C:\AAA\AddPrinter.bat"

I did some research, some one said need to use call with %1 > null to make Powershell return to batch. But it does not work either.

Call powershell.exe -ExecutionPolicy Bypass -Command "C:\scripts\AddPrinter.ps1"  %1 > null

I could only use batch command to install printer, but after installation, the new printer always setup as a default printer. but if use powershell to install it, it wont change default printer on remote desktop.

rundll32 printui.dll PrintUIEntry /ga /n\\server-01\printer0101

Hint please.

6
  • Maybe your powershell script takes time to finish its execution. Can you paste the contents of the powershell file? Commented Apr 8, 2014 at 19:20
  • I pasted powershell script, it will only take 15 seconds to finish. Commented Apr 8, 2014 at 19:24
  • If you run the PowerShell script manually from a PowerShell prompt, does it prompt for input? Commented Apr 8, 2014 at 19:24
  • NO, it just run and finish. no prompt at all Commented Apr 8, 2014 at 19:28
  • It works with or without Call in the batch file for me. Maybe add the -NoProfile switch to your powershell.exe line? What account are you running this under? Commented Apr 8, 2014 at 19:50

2 Answers 2

2

I have regularly seen problems calling PowerShell through psexec.exe. I would recommend ditching psexec.exe and configuring PowerShell Remoting instead. Then, you can deploy your script using the Invoke-Command command.

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

1 Comment

I agree with you. the thing i m not allowed to open PSRemoting....too risky said by upper level.....
0

I could also suggest for DOS like commands to use invoke-expression. PowerShell will process the command that you are passing as it is. The great is that you will be able to pass also variables

Comments

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.