0

I have a script for removing things on remote machine like browsers cache. Script worked fine when I used hardcoded value for a machine name. After I implemented GUI, I need to read machine name from textbox and I have situation like this ->

function where I read textbox value -> value is OK, Write-Host will print HR-SYSMACHINE2

$Button.Add_Click(
    {    
    Write-Host "0---->"$textBox.text
    Execute-Script($textBox.text)
    }
)

function for controlling script flow -> value is OK, Write-Host will print HR-SYSMACHINE2

Function Execute-Script([string]$RemoteMachineName) {
    Write-Host "1---->" $RemoteMachineName
    Delete-Chrome-Cache($RemoteMachineName,$ListOfUserProfiles)

But when I try to use this $RemoteMachineName (which should be HR-SYSMACHINE2) to Invoke-Command my script will crash with an error:

Function Delete-Chrome-Cache([string]$RemoteMachine, [System.Collections.ArrayList]$ListOfUserProfiles){
    Write-Host "2---->" $RemoteMachine
    $logs2 = Invoke-Command -ConnectionURI $RemoteMachine -ArgumentList (,$ListOfUserProfiles) -ScriptBlock {

Invoke-Command : One or more computer names are not valid. If you are trying to pass a URI, use the -ConnectionUri parameter, or pass URI objects instead of strings.

Analyzing this crash, I found out that in this last function, variable $RemoteMachine has value:

HR-SYSMACHINE2 System.Object[]

Instead of just:

HR-SYSMACHINE2

How can I get rid of that System.Object[]?

Keep on mind that everything works fine with hardcoded value of machine name, so problem is not with other parts of code, there is something with passing arguments from function to function, but I can't get it.

punky
  • 93
  • 1
  • 10
  • In short: PowerShell functions, cmdlets, scripts, and external programs must be invoked _like shell commands_ - `foo arg1 arg2` - _not_ like C# methods - `foo('arg1', 'arg2')`. If you use `,` to separate arguments, you'll construct an _array_ that a command sees as a _single argument_. See [this answer](https://stackoverflow.com/a/65208621/45375) for more information. – mklement0 Dec 20 '21 at 06:58

0 Answers0