Invoke-command question

This topic contains 4 replies, has 3 voices, and was last updated by  Kevyn 2 weeks ago.

  • Author
    Posts
  • #76412

    Jaapie bakker
    Participant

    Hi guys,

    Due to the of kyvn here, i looked into the invoke-command.

    it is working but when i use the script below i get:

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

    When i set as computername as server1,server2,server3 the script will working. But i have like 150 servers can't i use a varialble??

    $test = Get-Content C:\temp\servers.txt
    Invoke-Command -FilePath D:\Scripting\Files\SetRebootTask.ps1 -ComputerName $test

  • #76414

    Simon B
    Participant

    you could put the invoke command in a for each loop
    something like

    $test = Get-Content C:\temp\servers.txt
    foreach ($t in $test) {
    Invoke-Command -ComputerName $t -FilePath D:\Scripting\Files\SetRebootTask.ps1
    }

    but you may want to put your Invoke-command in a try catch block to stop it giving lots of errors for servers it can reach also this would enable you to log the servers it failed on to a text file. Also you need powershell remoting enabled on the servers you with to remote to

  • #76492

    Kevyn
    Participant

    What you have for your code is fine. You can have the list of servers be read into a variable like what you're doing. This is a formatting issue with the server names that are being read from the file, like a space at the beginning or end of one of your server names. Make sure there aren't any spaces at the beginning, or end, of your server names and that there are no server names with invalid characters in them. You can use a txt editor like Notepad++ to help you see any spaces that are after the server name(View — "Show Symbols" — "Show All Characters").

  • #76535

    Jaapie bakker
    Participant

    Thanks guys for the quick replies. Both option are true and works. Will also look into the try and catch option that is new for me love to learn this.

  • #76575

    Kevyn
    Participant

    Glad to hear it, Jaapie. To learn about the Try/Catch/Finally blocks, the help file information is the place to start. You can bring it up by typing in Help about_Try_Catch_Finally -Full. One thing to keep in mind...You need to add -ErrorAction Stop to the end of whatever command(s) you run in the Try block, otherwise the code in the Catch block won't execute for non-terminating errors (Ex: The Get-Service cmdlet below can't reach server1.).

    For Example:

    $ErrorLog = C:\Errors.txt
    Try
    {
      Get-Service -ComputerName server1 -ErrorAction Stop
    }
    Catch
    {
      Write-Verbose "Failed to get the service information from server1.  See $ErrorLog for details."
      $ServiceErrMsg = "$(Get-Date): [ERROR]: Failed to get the service information from server1.  Error message was: $($_.Exception.Message)"
      $ServiceErrMsg | Out-File -FilePath $ErrorLog -Append
    }
    

You must be logged in to reply to this topic.