.bat file to run multiple PowerShell commands and pass parameters

I am trying to run .bat file using “Start Process” activity and pass parameters - $un, $pwd but getting error during execution.

PowerShell script :

param(
[Parameter(Mandatory=$true)] $un,
[Parameter(Mandatory=$true)] $pwd
)

#Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

$cred = New-Object System.Management.Automation.PsCredential($un, (ConvertTo-SecureString $pwd -AsPlainText -Force))

Import-Module MSOnline

Connect-MsolService -Credential $cred

Get-MsolAccountSku

Hi @Sonalk,
Here you have a nice tutorial for the Powershell scripting:

1 Like

@Pablito : Bot is trying to run commands in PowerShell(x86) which is causing errors - could not load module or assembly file.

Is it possible for bot to run commands in PowerShell (64 bit) instead of PowerShell (x86)?

1 Like

I don’t think this is related to your issue. Could you send an example of your project and eventually the full error stack you get?

@Pablito : Please find the attachment. It contains workflow, error details and PowerShell script.

It seems this module can’t be installed in PowerShell (x86). I am intrested whether it is possible to connect to Microsoft 365 using PowerShell (x86) or do i have to trigger PowerShell (64 bit) from PowerShell (x86) during the execution of this build.MSOnline.zip (69.6 KB)

Thank you. Indeed it looks like something that maybe we could improve (honestly I’m not super sure about it). I pushed this case to our dev team to check.

Btw. Please also check this site → Connect to Microsoft 365 with PowerShell - Microsoft 365 Enterprise | Microsoft Learn

They said that there are also some dependencies needed on the computer to run this Office365 Module. Maybe this is somehow related?

@Pablito : Have you found any issue in shared workflow?

Modules are actually available in their respective paths. Issue is coming during import modules.

I opened an internal ticket and the case is still under investigation process.

@ptrobot @Lakshmi_Holla : Any inputs or thoughts to perform alternative solution.

This script solves this issue for Bot to run commands in 32 bit using Invoke PowerShell activity.

Set-ExecutionPolicy Unrestricted -Scope CurrentUser
$PathToPowerShell64bit = Get-ChildItem -Path $Env:Windir\WinSxS | Where-Object {$_.FullName -match ‘amd64_microsoft-windows-powershell-exe’}
$PowerShell64bitExe = Get-ChildItem -Path $PathToPowerShell64bit.FullName -Filter PowerShell.exe | Select-Object -ExpandProperty FullName
& $PowerShell64bitExe -file ‘Path\MSOnline.ps1’

MSOnline.ps1 contains below commands :

$cred = Get-Credential
Import-Module MSOnline
Connect-MsolService -Credential $cred
Get-MsolAccountSku
Get-Process | Out-File -FilePath .\Process.txt
Start-sleep 10

Now i am building and testing script to pass credentials (instead of passing manually) and file path of MSOnline.ps1 as input parameters to script.

Is there way to use only one string as script? I don’t want to complicate and call another PowerShell script from specified path.

Objective is to use invoke PowerShell activity with two arguments only - Botusername and Botpassword.

I have the same issue. I searched all over the internet and found no solution. @Sonalk How did you manage to import MsOnline module using 64 bit PS? Solution you provided here did not work for me.

@Pablito Is there any updates on this? I see lots of people having the similar issue.

I try to invoke script below and I get this error:“Create Migration Batch: The term ‘New-MigrationBatch’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.”,"

That means MsOnline module did not get imported :confused:

Param
(
[Parameter(Mandatory=$true)] [string]$userUPN
)

$migrationuser = “$userUPN”

$admin = “admin@credential.com

$Path = Split-Path -Parent “C:\Path\CSV*.*”
$csvfile = $Path + “$migrationuser.csv”
Add-Content -Path $csvfile -Value ‘EmailAddress’
Add-Content -Path $csvfile -Value “$migrationuser”

Set-ExecutionPolicy Unrestricted -Scope CurrentUser
$PathToPowerShell64bit = Get-ChildItem -Path $Env:Windir\WinSxS | Where-Object {$_.FullName -match ‘amd64_microsoft-windows-powershell-exe’}
$PowerShell64bitExe = Get-ChildItem -Path $PathToPowerShell64bit.FullName -Filter PowerShell.exe | Select-Object -ExpandProperty FullName
& $PowerShell64bitExe -file “C:\Path\ConnectMSOnline.ps1”

New-MigrationBatch -Name $migrationuser -SourceEndpoint contoso.com -CSVData ([System.IO.File]::ReadAllBytes(“$csvfile”)) -AutoStart -TargetDeliveryDomain contoso.mail.onmicrosoft.com -AutoComplete -NotificationEmails $admin

Remove-PSSession $O365Session

Hi @Muhammet_Ozkan : I haved used below script to run via PowerShell activity : first switch PS 64 bit and then invoke another script to import MSOnline module and run required command in O365 admin center.

$PathToPowerShell64bit = Get-Children -Path $Env:Windir\WinSxS | Where-Object {$_.FullName -match ‘amd64_microsoft-windows-powershell-exe’}
$Powershell64bitExe = Get-ChildItem -Path $PathToPowerShell64bit.FullName -Filter Powershell.exe | Select-Object -ExpandProperty FullName
& $PowerShell64bitExe -file $FilePath -Botcredentials $cred

Use Invoke PowerShell activity , pass parameters (credentials etc), PowerShell variable (another script contains MSOnline commans) and type argument : Object. You can also validate output after execution.

Hope this help to you!

Hi @Sonalk

This is to inform you that the latest System 23.12-preview introduced a new property to the activity: Execution mode, with which you can have finer control over your PowerShell runtime (and hopefully resolve the above issue).