简体   繁体   中英

Running Powershell Script in Excel VBA

So I'm trying to create a workbook that will pull down all info from AzureAD using powershell.(C:\\Script\\VBA Testing\\ExportOffice365UsersAndGroups.ps1)

and then send commands to powershell and run them.

I've thought of different ways such as creating a ps1 file:

strpath = "C:\Script\VBA Testing\"
Set oFile = fso.CreateTextFile(strpath & "Testing.ps1")
oFile.WRiteline "Connect-AzureAD"
oFile.Close

then just running it to creating a batch file that will run the script after creating it as running powershell scripts seem to be hard.

I am able to start powershell and run the command but the issue is that for some reason its not noticing the modules i have run.

"Powershell -noexit -File ""C:\Script\VBA Testing\Testing.ps1"""

just returns that the module AzureAD does not exist but if i just start powershell and try it, it works fine.

Same if i set it to run from C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe

Anyone have any bright ideas that might help me?

Pretty much I just need it to do Connect-AzureAD then run a few lines.. What im thinking is that every change i do will update the ps1 file and in the end i press a button and that will run the entire script.

A lot of users are running 32bit excel so need something that will check if its running 32bit and if so, run powershell 64bit instead of 32bit.

Based on my test it works in the 64 bit Excel VBA project.

So please have a try to use the 64 bit Excel to test.

If I run the command in the 32 bit excel VBA I also can reproduce the issue that you mentioned.

We could know that Connect-AzureAD in the powershell AzureAD or AzureAD preview module. I also found that I can't Import-Module AzureAD in the 32 bit VBA project.

In the AzureADPreview/AzureAD.psd1 file, I found that

ProcessorArchitecture = 'Amd64'

And I also try to update the ProcessorArchitecture = "None" or ProcessorArchitecture = "X86" , but both of don't work.

powershell script:

Import-Module AzureAD -Force  //force to load the AzureAD module 
Connect-AzureAD

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM