1

I am using following Sub to pass arguments to Powershell.

     Sub testpower()
      Set WshShell = CreateObject("WScript.Shell")
      WshShell.Run ("Powershell.exe -file .\test.ps1 -path ""Shell.txt"" ")
     End Sub

But,the output is not generated when run in VB,but if try to run directly from run command,it gives the desired result.Please help.

test.ps1 script:

 Param([String]$path)
    Get-AuthenticodeSignature $path | Out-File  "C:\Documents and Settings\acmeuser1\output.txt"
Musaab Al-Okaidi
  • 3,734
  • 22
  • 21
abhinov
  • 125
  • 1
  • 3
  • 12
  • have you tried to use the full path names of the file? – Musaab Al-Okaidi Oct 03 '13 at 10:25
  • Yes,i tried both ways.. – abhinov Oct 03 '13 at 10:33
  • that's a bit strange as it worked for me. Is the C:\Documents and Settings\acmeuser1\output.txt file created after you run the script? have you tried to output something else to it other than the output of the Get-AuthenticodeSignature? – Musaab Al-Okaidi Oct 03 '13 at 11:23
  • I was making a mistake its working for me now..I want to change the value of the path everytime ,which is "Shell.txt" in this case.how? – abhinov Oct 03 '13 at 11:51
  • This is what i want to do now..Sub testpower() Set WshShell = CreateObject("WScript.Shell") k = Chr(34) & "C:\Documents and Settings\acmeuser1\1.vbs" & Chr(34) 'filepath = "" & "C:\Documents and Settings\acmeuser1\1.vbs" & "" WshShell.Run ("C:\WINDOWS\system32\WindowsPowerShell\v1.0\Powershell.exe -file test.ps1 -path k ") End Sub – abhinov Oct 03 '13 at 12:02
  • I could be mistaken about the error but it looks like you're going to pass a relative path that resolves to the PS Install directory... I understand that k is supposed to be a variable but in the context shown above, you're going to most likely get k as your path, because it wasn't passed as "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -File "\Test.ps1" -Path k. Keep in mind this all needs to have the same quoting in your script -- the example is the necessary command line. For the PowerShell exe, you won't need the quotes, though. – PSGuy Dec 18 '15 at 05:54

1 Answers1

0

This question is essentially answered in VBscript code to capture stdout, without showing console window - there are several techniques there that are stated as working. To summarize, use WShell.Exec instead of WShell.Run, and then capture the output via WShell.StdOut.ReadLine() or WShell.StdOut.ReadAll().

Community
  • 1
  • 1
Jeff Zeitlin
  • 9,773
  • 2
  • 21
  • 33