See Code below, Why doesn't the second Invoke-Command work? This command gives me the error:
PS C:\Users\dude\Desktop\scripts> .\setup2.ps1
=> 1
Invoke-Command : Parameter set cannot be resolved using the specified named parameters.
At C:\Users\dude\scripts\setup2.ps1:19 char:1
Invoke-Command @parameters
~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidArgument: (:) [Invoke-Command], ParameterBindingException
FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Commands.InvokeCommandCommand
The code:
$machine = "pv3039.mydomain.com"
$credential = Get-Credential -Credential "$env:userdomain\$env:username"
Enable-WSManCredSSP -Role Client -DelegateComputer $machine -Force | out-null
$s = New-PSSession -ComputerName $machine
Invoke-Command -Session $s -ScriptBlock {Enable-WSManCredSSP -Role Server -Force} | out-null
$parameters = @{
Session = $s
ScriptBlock = { Get-Item \\copper\prj_342423 }
Authentication = "CredSSP"
Credential = $credential #"$env:userdomain\$env:username"
}
write-host "=> 1"
Invoke-Command @parameters
write-host "=> 2"
What I don't get is that this code is taken directly from microsoft's Invoke-Command Documentation on their website.. So It should work right? See Snapshot picture as proof:
CodePudding user response:
That example is buggy. The session stored in $s
is already established - you can't change its connection parameters after the fact.
Either use -ComputerName Server02
with the other parameters you've supplied:
$parameters = @{
ComputerName = 'Server02'
ScriptBlock = { Get-Item \\copper\prj_342423 }
Authentication = "CredSSP"
Credential = $credential #"$env:userdomain\$env:username"
}
Invoke-Command @parameters
... or establish a new session using Credssp with New-PSSession -Authentication
:
$s2 = New-PSSession Server02 -Authentication Credssp
$parameters = @{
Session = $s2
ScriptBlock = { Get-Item \\copper\prj_342423 }
}
Invoke-Command @parameters
CodePudding user response:
My conclusion is that you shouldn't use microsoft's example for doing this. Use this code instead:
$machine = "pv3039.somewhere.com"
$credential = Get-Credential -Credential "$env:userdomain\$env:username"
Enable-WSManCredSSP -Role Client -DelegateComputer $machine -Force | out-null
$parameters = @{
Computer = $machine
ScriptBlock = { Enable-WSManCredSSP -Role Server -Force }
}
Invoke-Command @parameters | out-null
$parameters = @{
Computer = $machine
ScriptBlock = { Get-Item \\copper\someprj }
Authentication = "CredSSP"
Credential = $credential #"$env:userdomain\$env:username"
}
write-host "=> 1"
Invoke-Command @parameters
write-host "=> 2"