Invoke-Command with credentials using invoke PowerShell activity

Hello everyone,
I am trying to execute a PowerShell command on multiple remote servers using UiPath’s Invoke Power Shell activity. The command I want to run works perfectly when executed directly in PowerShell using Invoke-Command
i tried using :

[string]$username = "username"
[string]$password = "password"
[string[]]$servers = "server1","server2"

# Convert plain password to SecureString
$secPassword = ConvertTo-SecureString $password -AsPlainText -Force

# Create PSCredential object
$cred = New-Object System.Management.Automation.PSCredential ($username, $secPassword)

# Invoke command on remote servers
Invoke-Command -ComputerName $servers -Credential $cred -ErrorAction Stop -ScriptBlock {
    Get-Date -Format 'yyyy-MM-dd HH:mm:ss zzz'
}

the script run correctly in powershell but doesn’t execute with invoke powershell in UiPath also i want to use the credentials from orchestrator.

I would really appreciate any guidance on this.

hi, @YOUSSEF_BARJ1 Use UiPath’s Get Credential activity to fetch username and password securely from Orchestrator. Convert the password to SecureString in UiPath. Pass the username, password, and server list as parameters in Invoke Power Shell. Make sure remote PowerShell is enabled on target servers. This way you keep credentials secure and run commands remotely without hardcoding. Let me know if you want a sample workflow!

i did that but for simplicity i hard coded everything in the command to test execution first, then use of get credential for user and password!

Hi @YOUSSEF_BARJ1

When you say, script doesn’t run with UiPath, do you get any error?

Also could you show us the properties selected for invoke powershell activity? Please note for script, you will need to check the property “is script”

Please refer below knowledge article on same