0

Invoking script and trying to run PowerShell commands available in script. Passing credentials from secure credential store as parameters. I am always getting error in this command "connect-msolService -credential $cred"

param([String]$un,
[SecureString]$pwd
)

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

#$un = "xxxx@xxx.com"

#$pwd = Read-Host -AsSecureString

$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $un,$pwd

Install-Module -Name AzureAD

Install-Module -Name MSOnline -Scope CurrentUser
connect-msolService -credential $cred 

Get-MsolAccountSku

In parallel, I tried running these commands manually by passing credentials inside script and it is working fine for me.

Not sure whether issue is due to passing credentials as parameters. i.e. data types of parameters un = string, pwd = SecureString

The 'connect-msolService' command was found in the module 'MSOnline', but the module could not be loaded. For more information, run 'Import-Module MSOnline'.

iRon
  • 20,463
  • 10
  • 53
  • 79
SKhati
  • 1
  • 2
  • Please, [DO NOT post images of code, data, error messages, etc.](https://meta.stackoverflow.com/a/285557), see [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – iRon Aug 11 '21 at 07:10
  • I quess the issue is where you invoke your script. E.g. `CMD` vs `PowerShell` command prompt. There are a lot of q&a's on how to quote/escape special characters in in the arguments of a command line. – iRon Aug 11 '21 at 07:19
  • @iRon : bot is actually invoking script in PowerShell command prompt. – SKhati Aug 11 '21 at 07:34
  • How do you create a `SecureString` in your bot? And how do you enter that *in PowerShell command prompt*? (Please add these details to the question.) You might do something like that with string but for objects (as a `SecureString`) you need to [serialize the object](https://docs.microsoft.com/powershell/module/microsoft.powershell.core/about/about_remote_variables#serialization-of-variable-values). Instead, You could just consider to pass the encrypted key string: [`ConvertTo-SecureString -AsPlainText "p@ssw0rd" | ConvertFrom-SecureString`](https://stackoverflow.com/a/62609833/1701026) – iRon Aug 11 '21 at 08:31
  • @iRon : I am actually storing credentials in centralised management tool and then extracting credentials I.e. username as string, password as Secure string. If you see my script. I am actually passing credentials as parameter. Do I have to mention $pwd in place of "p@ssword in given command : ConvertTo-SecureString -AsPlainText "p@ssw0rd" | ConvertFrom-SecureString – SKhati Aug 11 '21 at 08:34
  • @iRon : I am actually using customised activity to run script in PowerShell : https://docs.uipath.com/activities/docs/invoke-power-shell. – SKhati Aug 11 '21 at 08:42
  • Anyways, you can't just pass objects (as a `SecureString`) between Out-of-process sessions you will need to [serialize] them, See: [Is there a way to pass serializable objects to a PowerShell script with start-process?](https://stackoverflow.com/q/34076478/1701026). Your issue is with *invoking* the script not with the script (in the question) itself. You might also consider [dot sourcing](https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_scripts#script-scope-and-dot-sourcing) your script rather than starting a new (presumed) `PowerShell.exe` session. – iRon Aug 11 '21 at 08:51

0 Answers0