Mastaxx
Mastaxx

Reputation: 125

Why is invoke-command and new-pssession not working?

I have no idea why this isn't working. Any ideas? I honestly don't know what else I can try.

I plan to add it to a script that prompts the user for a computername / domain creds but I can't get it to work in a shell window for starters.

Here's the line:

Invoke-Command -Session New-PSSession -ComputerName server001 -ScriptBlock {shutdown.exe /r -t 0} 

Here's the error:

Invoke-Command : Cannot bind parameter 'Session'. Cannot convert the "New-PSSession" value of type "System.String" to
type "System.Management.Automation.Runspaces.PSSession".
At line:1 char:25
+ Invoke-Command -Session New-PSSession -ComputerName server001 -Scrip ...
+                         ~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-Command], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.InvokeCommandCommand  

Sorry for not having anything further to add to this but I'm still trying to learn PowerShell and need guidance.

Upvotes: 0

Views: 2331

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174465

As the error indicates, the -Session parameter accepts arguments of type PSSession - and to create such an object we need to invoke New-PSSession (not just pass it's name as an argument):

$session = New-PSSession -ComputerName computer01
Invoke-Command -Session $session { ... }

If you don't plan on reusing the session later, you can simply pass the computer name directly to Invoke-Command, it'll handle session management for you implicitly:

Invoke-Command -ComputerName computer01 { ... }

Upvotes: 2

Related Questions