-1

I think this is a problem with escaping certain characters.

I have this VBScript used to call a PowerShell command silently without a window (line 3) :

Dim objShell
Set objShell=CreateObject("WScript.Shell")
strExpression="get-printer | Where-Object {$_.DriverName -notlike "*microsoft*"} | export-csv C:\x\temp\printers.csv -encoding UTF8 -force"
strCMD="powershell -sta -noProfile -NonInteractive -executionpolicy Bypass  -nologo -command " & Chr(34) &_
"&{" & strExpression &"}" & Chr(34) 
objShell.Run strCMD,0

Which gives me this error:

Erreur : Type incompatible: '[string: "get-printer | Where-"]'
Code : 800A000D

If I remove the Where-Object and just do :

strExpression="get-printer | export-csv C:\x\temp\printers.csv -encoding UTF8 -force"

It works fine.

What seems to be the problem and what is the character used to "escape" in VBScript?

Rakha
  • 1,361
  • 1
  • 14
  • 42
  • Wondering, is there any reason why you using both VBScript and PS at the same time? Like is there something you can do in VBS and not int PS? – Mike Twc Nov 05 '18 at 16:40
  • @MikeTwc Starting Powershell with `-executionpolicy Bypass` is one of the things that Powershell cannot do. ;-) – Tomalak Nov 05 '18 at 16:51
  • The goal is having a scheduled task run in the current user profile. If you run a PowerShell command this way, the user will see a window flashing, even with WindowStyle Hidden. So for this, you make the scheduled task call this VBS which contains the PowerShell command. You get your info from the current user session without him seeing anything. Very useful to gather currently connected printers in the user's environement. – Rakha Nov 05 '18 at 19:17

1 Answers1

0

Found it, quite simple, the double quotes were causing the problem so i just used single quotes in the command:

strExpression="get-printer | Where-Object {$_.DriverName -notlike '*microsoft*'} | export-csv C:\x\temp\printers.csv -encoding UTF8 -force"
Rakha
  • 1,361
  • 1
  • 14
  • 42
  • You just didn't escape them right, this would have also worked without needed single quotes: `strExpression="get-printer | Where-Object {$_.DriverName -notlike ""*microsoft*""} | export-csv C:\SAAQ\temp\printers.csv -encoding UTF8 -force"`. Double quotes are escaped in strings by doubling them, see the [duplicate answer](https://stackoverflow.com/a/2942573/692942). – user692942 Nov 05 '18 at 22:58