0

I'm having trouble trying to check a list of remote machines if CredSSP is enabled. When my script connects to the machine and executes the command. It ends up returning false. If I connect to that same machine via RDP and execute the same command, it will return true. Here is my script:

foreach ($server in $servers.Split(",").Trim()) {
   $pw = ConvertTo-SecureString 'password' -AsPlainText -Force
   $cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentList "Domain\user", $pw
   $session = New-PSSession -ComputerName $server -Credential $cred

   $output = (Get-Item  WSMan:\localhost\Service\Auth\CredSSP).value

   Write-Host $server : $output

   Remove-PSSession -Session $session
}

Does anyone have an insight into this?

Anthony
  • 806
  • 7
  • 14

1 Answers1

1

You're not running Get-Item remotely.

Try:

$ServerList = $servers.Split(",").Trim();
$pw = ConvertTo-SecureString 'password' -AsPlainText -Force;
$cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentList "Domain\user", $pw;

Invoke-Command -ComputerName $ServerList -Credential $cred -ScriptBlock { Get-Item  WSMan:\localhost\Service\Auth\CredSSP; } |
    Select-Object PSComputerName, Value;

You could use Invoke-Command -Session $session instead Invoke-Command -ComputerName $ServerList, but there's no need to make a loop and mess around creating and removing sessions by hand.

Bacon Bits
  • 30,782
  • 5
  • 59
  • 66
  • Thank you very much, this was helpful. I'm still fairly new to PS. So, correct me if I'm wrong, but by wrapping Get-Item with those session calls was essentially doing nothing? It was just executing locally? Is there a way to accomplish the same thing with a session? I might expand upon this script and it becomes much larger. – Anthony Aug 04 '16 at 21:56
  • @Anthony Yes, that's it exactly. It was executing locally. You can use sessions with Invoke-Command, but that's typically for more complex situations where you need to run several different script blocks or otherwise need to maintain session state remotely. I don't think I've written anything that's required it in a long time. It does work, however. – Bacon Bits Aug 04 '16 at 23:07