Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to execute some PowerShell code in a remote computer using the following:

$session = New-PSSession -Credential "myDomain\myUserName" -ComputerName "remoteCompName"
$result = Invoke-Command -Session $session -ScriptBlock {
    New-Item -type file C:\test10.txt
}

I am prompted to enter my password in a GUI. I do that. It then errors out with:

New-PSSession : 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.

I replaced the computer name with the FQDN. Still no luck. What is going on here?

There are other questions on stackoverflow on executing PowerShell scripts on remote machines of course but none address this error.

BTW, the machine is part of the domain and is running.

Additional info added later [EDIT]

Things to know:

  1. The machine is part of the domain and is running.
  2. I checked if PS remoting is enabled. It was.
  3. I checked if WinRM is running. It is.
  4. The remote machine is a VM and it is a 2012 R2.

Here is what I've tried:

  1. I replaced the computer name with the FQDN. Still no luck.
  2. I removed the credential parameter
  3. I tried another remote machine (also a VM)
  4. I tried another source machine, i.e. the machine I am running the command from)

Thanks!

-Rohan.

share|improve this question
    
Check if PowerShell Remoting is enabled on the remote host. Also check if the host name resolves to an IP address. –  Ansgar Wiechers Mar 19 at 20:21
    
PowerShell remoting was already enabled. And I pinged the machine - it does reply –  GigaRohan Mar 19 at 21:14
    
Do you have any other remote hosts you could try? Do they all behave the same? Can you try running without the -credential parameter? –  andyb Mar 20 at 5:26
    
I tried another remote host. Same result. I tried without the credential parameter. Same result... –  GigaRohan Mar 20 at 17:12
add comment

1 Answer

up vote 0 down vote accepted

When I use a remote machine with a name that is just alphanumeric (no underscores, dashes, etc.), it works! The name of all machines I tried before had leading '_' in them. That was the cause of the error. (The answer was suggested by Rhys W Edwards on the Windows PowerShell TechNet forum, which is within Windows Server forums).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.