I have a really simple PowerShell script that does the following:
I had a requirement to run this via VBA, so I saved the script, and wrote this up in VBA
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
It appears to run, but no IP is entered into A1.
I tried this just a test:
But that also doesn't work.
This does work though
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
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: