Powershell Command via VBA

Zakkaroo

Active Member
Joined
Jul 6, 2009
Messages
383
I have a really simple PowerShell script that does the following:

Code:
$PubIPSource = "ipinfo.io/ip"
[String]$currentIP = (Invoke-WebRequest -uri $PubIPSource -UseBasicParsing)
Write-Output $currentIP

I had a requirement to run this via VBA, so I saved the script, and wrote this up in VBA

VBA Code:
Sub PowerShellScript()
    Dim strCommand As String
    strCommand = "Powershell -file ""C:\MyScripts\ipAddressScript.ps1"""
    Set WshShell = CreateObject("WScript.Shell")
    Set WshShellExec = WshShell.Exec(strCommand)
    strOutput = WshShellExec.StdOut.ReadAll
    Sheets("Sheet1").Range("A1").Value = strOutput
End Sub

That outputs the IP address of the machine it runs on, to A1 - and it works just fine.
The end use is actually not to store it in a Cell, but use it elsewhere. Storing it in A1 just let's me see that it's working

But I had an additional requirement, not to store the script locally and instead, to attempt to just run it directly from VBA.
So after a bunch of googling, I replaced the strCommand = " ... line with this

Code:
strCommand = "PowerShell -ExecutionPolicy Bypass -Command ""$PubIPSource = ""ipinfo.io/ip"" | [String]$currentIP = (Invoke-WebRequest -uri $PubIPSource -UseBasicParsing) | Write-Output $currentIP"

It appears to run, but no IP is entered into A1.

I tried this just a test:

Code:
strCommand = "PowerShell -ExecutionPolicy Bypass -Command ""[String]$currentIP = ""testing123"" | Write-Output $currentIP"

But that also doesn't work.

This does work though

Code:
strCommand = "PowerShell -ExecutionPolicy Bypass -Command ""Write-Output ""testing123"""

Which suggests it's a problem with the multi-line version that I wrote? Am I doing it totally wrong :D :D is it even possible to run it this way?

Any help appreciated
 
Last edited:

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top