
VBAから
Power Shellのコマンドレットを実行して…


その結果を
VBAで取得できます!

VBAコード
ここでは例として、
・コマンドレット「(Invoke-WebRequest -Uri ‘https://www.yahoo.co.jp/’).StatusCode」を実行して
・結果を取得
します。
※上記のコマンドレットは「指定したURLのHTTPステータスコードを取得するコマンドレット」です。
※例として「YahooのHTTPステータスコードを取得」します。
'変数の宣言を必須
Option Explicit
Sub sample()
Const START_ROW As Long = 2
Dim psCommand As String
Dim wsh As Object
Dim execObj As Object
Dim psCommandResult As String
'実行するPower Shellのコマンドレットを指定
psCommand = "(Invoke-WebRequest -Uri 'https://www.yahoo.co.jp/').StatusCode"
Set wsh = CreateObject("WScript.Shell")
'Power Shellのコマンドレットを実行
Set execObj = wsh.exec("powershell -NoProfile -ExecutionPolicy Unrestricted " & psCommand)
'Power Shellのコマンドレットの実行結果を取得
psCommandResult = execObj.stdOut.ReadAll
'Power Shellのコマンドレットの実行結果を表示
MsgBox psCommandResult
'後片付け
Set execObj = Nothing
Set wsh = Nothing
End Sub
実行結果
Power Shellの実行結果を取得できました。

参考①
「Power Shellファイルを実行」することもできます。
詳細は以下の記事をご確認ください。
参考②
上記のVBAコードで使用した以下の詳細は、公式サイトをご確認ください。
●「WScript.Shell」の「exec」メソッド
●「WScript.Shell」の「stdOut」プロパティ
●Invoke-WebRequestコマンドレット
参考③
HTTPステータスコードの詳細は、以下をご確認ください。