0

I am trying to enter a PSSession using -Computername $Server which was previously defined, but I can't seem to get this to work.

I have tried single, double, and no quotes around the variable at all. What am I doing wrong?

$Servers = Import-Csv "C:\Users\username\Desktop\DNS.csv"
$secpass = ConvertTo-SecureString 'mypassword' -AsPlainText -Force
$myCred = New-Object System.Management.Automation.PSCredential("username", $secpass)

foreach ($Object in $Servers) {
    $Server = $Object.Name

    Enter-PSSession -ComputerName "$Server" -Credential $myCred
    sl HKLM:\SYSTEM\CurrentControlSet\Services\NetBT\Parameters
    Invoke-Command -ScriptBlock {Get-Item -Path HKLM:\SYSTEM\CurrentControlSet\Services\NetBT\Parameters}
    Exit-PSSession
}

enter image description here

2
  • No quotes or double quotes should be fine, no substitution is performed with single quotes so they won't work here. See about_quoting_rules for more info on this. Have you enabled remoting on the servers you're trying to connect to? Commented Jul 17, 2017 at 15:30
  • Yes, remoting is enabled on all servers Commented Jul 17, 2017 at 15:34

2 Answers 2

1

We use enter pssession for creating an interactive session with the remote computer.

In your case, you do not need to have an interaction with the remote system. You just need to fetch the details from the remote systems which are present in the csv file.

So, Instead of this:

foreach($Object in $Servers) {

$Server = $Object.Name

Enter-PSSession -ComputerName "$Server" -Credential $myCred

    sl HKLM:\SYSTEM\CurrentControlSet\Services\NetBT\Parameters

        Invoke-Command -ScriptBlock {Get-Item -Path HKLM:\SYSTEM\CurrentControlSet\Services\NetBT\Parameters}

Exit-PSSession

}

Do This:

foreach($Object in $Servers) 
{
$Server = $Object.Name
Invoke-Command -ComputerName $Server -ScriptBlock {Get-Item -Path HKLM:\SYSTEM\CurrentControlSet\Services\NetBT\Parameters} -Credential $myCred
}

Note: I believe you have enabled PSRemoting and have edited trusted hosts.

Sign up to request clarification or add additional context in comments.

4 Comments

This is running now, but it is still returning the information from my local registry instead of remote.. Any idea why?
NO its not. You can check the value coming in $Server. Let me modify one more time.
@4c74356b41: What i meant is that for getting the information from the remote system, he don't need to create an interactive PSSession. A non-interactive remote session is enough,
After implementing your suggestion, it now shows PSComputerName in a "PS Table" format, but under property it is still pulling from the local machine. Any ideas?
0

The ComputerName param of Invoke-Command will accept an array of servers so you can do away with the foreach loop entirely and simplify your code to:

$Servers = Import-Csv "C:\Users\username\Desktop\DNS.csv" | Select-Object -ExpandProperty Name
$secpass = ConvertTo-SecureString 'mypassword' -AsPlainText -Force
$myCred = New-Object System.Management.Automation.PSCredential("username", $secpass)

Invoke-Command -ComputerName $Servers -ScriptBlock {Get-Item -Path HKLM:\SYSTEM\CurrentControlSet\Services\NetBT\Parameters} -Credential $myCred

2 Comments

This sounds easier. However, it is still pulling from the local machine instead of the remote.
Works perfectly for me, not sure how you're getting that result. Try changing the Path to HKLM:\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName as this contains the Computers Name which will double check that you are indeed getting what you think.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.