0
votes

When running the code below I get the error message

Invoke-Command : Missing an argument for parameter 'ComputerName'. Specify a
parameter of type 'System.String[]' and try again.  
At line:11 char:16  
+ Invoke-Command -ComputerName -ScriptBlock $scriptblock -Credential $Cred -Argum ...   
+ ~~~~~~~~~~~~~  
     + CategoryInfo : InvalidArgument: (:) [Invoke-Command], 
ParameterBindingException  
     + FullyQualifiedErrorId : MissingArgument,Microsoft.PowerShell.Commands.InvokeCommandCommand

The code:

$item = "1337"
$username = "username"
$password = "password"
$Cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, ($password | ConvertTo-SecureString -AsPlainText -Force)

$scriptblock = {
    New-PSDrive -Name SampleDC -PSProvider FileSystem -Root \\sampleDC\scripts
    ."C:\scripts\sample.ps1" # include global functions scripts
    new_user $args[0] # new_user is a function in global functions
}

Invoke-Command -ScriptBlock $scriptblock -Credential $Cred -ArgumentList $item
2
Please show us the new_user function aswell. Now it is unclear where the error is coming from.Theo
Also show the full error, not just the message text.Ansgar Wiechers
You need to distinguish among ComputerName, Uri, VMId and VMName parameter sets: (Get-Command Invoke-Command).ParameterSets | Where-Object { 'ScriptBlock' -in $_.Parameters.Name -and 'ArgumentList' -in $_.Parameters.Name -and 'Credential' -in $_.Parameters.Name } | FT -auJosefZ
Thanks for your replies guys - @Theo the new_user function is 2000 lines long and I can assure you the issue is not inside that function. - Ansgar Wiechers the full error message I'll be posting below.Maurice van Dorst
Thanks for your reply JosefZ. I've tried specifying 'localhost' as computername yet that does not work. What I want to do is run the scriptblock under different credentials on my current computer. Would it be wiser to start a New-PSSession and run the scriptblock in that PSSession using the -session parameter?Maurice van Dorst

2 Answers

1
votes

You cannot run Invoke-Command with different credentials without specifying a computer. The error you're getting is because you used the parameter -ComputerName without an argument.

To have Invoke-Command run the scriptblock on the local computer use either of the following commands:

Invoke-Command -Computer . -ScriptBlock $scriptblock -Credential $Cred ...
Invoke-Command -Computer localhost -ScriptBlock $scriptblock -Credential $Cred ...
Invoke-Command -Computer $env:COMPUTERNAME -ScriptBlock $scriptblock -Credential $Cred ...

Note that if the user whose credentials you're passing does not have admin privileges you'll be getting an error like this:

[localhost] Connecting to remote server localhost failed with the following
error message : Access is denied. For more information, see the
about_Remote_Troubleshooting Help topic.
    + CategoryInfo          : OpenError: (localhost:String) [], PSRemotingTransportException
    + FullyQualifiedErrorId : AccessDenied,PSSessionStateBroken

In that case you need to enable PowerShell Remoting for them first. Run the following command in an elevated PowerShell console and add the user or group in the dialog that pops up.

Set-PSSessionConfiguration Microsoft.PowerShell -ShowSecurityDescriptorUI

From the documentation:

HOW TO ENABLE REMOTING FOR NON-ADMINISTRATIVE USERS

ERROR:  ACCESS IS DENIED

To establish a PSSession or run a command on a remote computer, the user must have permission to use the session configurations on the remote computer.

By default, only members of the Administrators group on a computer have permission to use the default session configurations. Therefore, only members of the Administrators group can connect to the computer remotely.

To allow other users to connect to the local computer, give the user Execute permissions to the default session configurations on the local computer.

The following command opens a property sheet that lets you change the security descriptor of the default Microsoft.PowerShell session configuration on the local computer.

Set-PSSessionConfiguration Microsoft.PowerShell -ShowSecurityDescriptorUI
1
votes

The computername parameter is missing. I believe you need to be at an elevated prompt to invoke-command on localhost.