none
Invoke-Command error one or more computer names not valid RRS feed

  • Question

  • Hi everyone,

    I'm new to PowerShell and I'm having trouble getting a script to work properly, and I was hoping to get some help. I have a script that should check for old antivirus software, and then uninstall it by running the new AV vendor's Competitor Removal Tool, and also check for the presence of our new AV software, and install it if it's not there. I just enabled PS Remoting this morning and I'm trying to use Invoke-Command to run the script on remote computers. Here is the script itself:

    #Variables that store the path to 32-bit and 64-bit versions of Sophos
    $Sophos64 = Test-Path 'C:\Program Files\Sophos'
    $Sophos32 = Test-Path 'C:\Program Files (x86)\Sophos'
    
    #Variables that store the path to 32-bit and 64-bit versions of Kaspersky
    $Kaspersky64 = Test-Path 'C:\Program Files\Kaspersky Lab'
    $Kaspersky32 = Test-Path 'C:\Program Files (x86)\Kaspersky Lab'
    
    #The following block will run the Sophos-Installation batch file, removing any instance of Kaspersky and installing Sophos
          If ($Sophos64 -eq $false -or $Sophos32 -eq $false) 
          {
          Copy-Item -Path \\dc-01\NETLOGON\SophosInstall -Destination C:\Windows\Temp\AVInstall -Recurse
          Start-Process -FilePath C:\Windows\Temp\AVInstall\SophosInstall\Sophos-Installation.bat
          Get-ADComputer "$env:COMPUTERNAME" | Select-Object -Property Name |Out-File -FilePath "\\CCIT-4BXZPN2LP\c$\Sophos Results\Test_Output.txt" -Append
          }
    #The following block will remove Kaspersky from a machine if it is present and Sophos is already installed.
            Elseif (($Kaspersky64 -eq $true -or $Kaspersky32 -eq $true) -and ($Sophos64 -eq $true -or $Sophos32 -eq $true)) 
            {
            Copy-Item -Path \\dc-01\NETLOGON\SophosInstall -Destination C:\Windows\Temp\AVInstall -Recurse
            Start-Process -FilePath C:\Windows\Temp\AVInstall\SophosInstall\AVRemoveW.exe
            Get-ADComputer "$env:COMPUTERNAME" | Select-Object -Property Name |Out-File -FilePath "\\CCIT-4BXZPN2LP\c$\Sophos Results\Test_Output.txt" -Append
            }
    #The last block will only be executed if Sophos is installed and Kaspersky isn't, and makes no changes to the machine.
            else {Write-Host "This machine has the proper AV software installation"
            Get-ADComputer "$env:COMPUTERNAME" | Select-Object -Property Name |Out-File -FilePath "\\CCIT-4BXZPN2LP\c$\Sophos Results\Test_Output1.txt" -Append}

    And here is the command I'm trying to use to run this script on remote computers:

    $computer = Get-ADComputer -Identity CCIT-HCPS27LP
    
    Invoke-Command -ComputerName $computer -FilePath 'C:\Sophos Results\InstallSophos_UninstallKaspersky_Test.ps1' -ErrorAction SilentlyContinue

    When I run that command I get the following error:

    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.
    At line:1 char:1
    + Invoke-Command -ComputerName $computer -FilePath 'C:\Sophos Results\I ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (System.String[]:String[]) [Invoke-Command], ArgumentException
        + FullyQualifiedErrorId : PSSessionInvalidComputerName,Microsoft.PowerShell.Commands.InvokeCommandCommand

    What is confusing to me is if I don't use the $computer variable, but just explicitly state the computer name under the invoke-command -computername parameter, it works. I don't understand why this would happen. I've also tried just copying the file to the remote computer and then running it from the remote machine but don't have any luck there either, I get an error that says Path doesn't exist (but it does). Again, when I explicitly state a computer name the Copy-Item command works, if I try and use a $computer variable it throws an error. Any help I could get on this would be much appreciated. Thanks in advance!

    Wednesday, July 18, 2018 4:55 PM

Answers

  • Replace this:


    $computer = Get-ADComputer -Identity CCIT-HCPS27LP

    with just this:


    $computer = "CCIT-HCPS27LP"

    There's no need for Get-ADComputer unless you are retrieving AD properties from that computer object.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, July 18, 2018 5:07 PM
    Moderator

All replies

  • Replace this:


    $computer = Get-ADComputer -Identity CCIT-HCPS27LP

    with just this:


    $computer = "CCIT-HCPS27LP"

    There's no need for Get-ADComputer unless you are retrieving AD properties from that computer object.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, July 18, 2018 5:07 PM
    Moderator
  • Awesome, thanks!
    Wednesday, July 18, 2018 5:16 PM