PowerShell VBAでPowerShellを実行して結果を取得する(Exec編)【初実験編07】 | システムトラスト技術ブログ
https://it-engineer-info.com/language/powershell/1899/
"===================================================================
" PowerShellの実行(Execコマンド使用バージョン)
"===================================================================
" スリープAPIを使用する宣言
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
"===================================================================
"===================================================================
Sub ボタン1_Click()
"===================================================================
Sub ボタン1_Click()
Dim cmdStr As String
Dim strResult As String
cmdStr = "(get-item ($env:SystemDrive + "\Program Files\Google\Chrome\Application\chrome.exe")).VersionInfo.FileVersion"
strResult = ExecPowerShell(cmdStr)
" MsgBox "Chrome = " & strResult
Range("B5").Value = strResult
cmdStr = "(get-item ($env:SystemDrive + "\Program Files (x86)\Microsoft\Edge\Application\msedge.exe")).VersionInfo.FileVersion"
strResult = ExecPowerShell(cmdStr)
" MsgBox "Edge = " & strResult
Range("B6").Value = strResult
End Sub
" PowerShellの実行(Execコマンド使用バージョン)
"===================================================================
Private Function ExecPowerShell(cmdStr As String) As String
" WshShellオブジェクト
Dim oExec As Object
" Execコマンドで実行する
" -NoLogo 著作権の見出しを出さない
" -ExecutionPolicy RemoteSigned 実行権限を設定
" -Command コマンド引数(これ以降にPowerShellのコマンドレット構文を記載)
Set oExec = CreateObject("Wscript.shell").Exec("powershell -NoLogo -ExecutionPolicy RemoteSigned -Command " & cmdStr)
" ジョブが実行中(0)の間は、スリープしながら完了(1)まで待つ
Do While oExec.Status = 0
" 100ミリ秒
Sleep 100
Loop
" 標準出力取得
ExecPowerShell = oExec.StdOut.ReadAll
End Function
PowerShell
(get-item ($env:SystemDrive + "\Program Files\Google\Chrome\Application\chrome.exe")).VersionInfo.FileVersion
(get-item ($env:SystemDrive + "\Program Files (x86)\Microsoft\Edge\Application\msedge.exe")).VersionInfo.FileVersion
■END