1

I am getting the following error when I run the function below:

Invoke-Command : Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again. At C:\Users\usernameone\Desktop\script.ps1:16 char:29 + Invoke-Command -ComputerName <<<< $_ -ScriptBlock $s -Credential $cred + CategoryInfo : InvalidData: (:) [Invoke-Command], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeCommandCommand

$username = "username"
$password = "password"
$secstr = New-Object -TypeName System.Security.SecureString
$password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username,    $secstr
$_="192.168.10.4"

function test{
    $s = $ExecutionContext.InvokeCommand.NewScriptBlock("mkdir C:\'Documents and    Settings'\username\Desktop\Testfolder")
    Invoke-Command -ComputerName $_ -ScriptBlock $s -Credential $cred 
}

Do you have any ideas as to what I've done wrong?

8
  • Syntax is function test() { ... } Commented Oct 20, 2014 at 16:29
  • $_ is not defined in your function. Use a function parameter. Commented Oct 20, 2014 at 16:30
  • How do i use a function parameter? Commented Oct 20, 2014 at 16:50
  • Look here: stackoverflow.com/questions/4988226/… Commented Oct 20, 2014 at 16:53
  • I just want to run the function without entering parameters.. So i just want to type the function name test! and would like to execute the command and make a new directory on the remote computer. Commented Oct 20, 2014 at 16:59

2 Answers 2

1

The special variable $_ is only populated when the function or scriptblock containing it is involved in an active pipeline. It should work if you invoke test like this:

PS> "computername" | test

Follow?

As an aside, why are you creating a scriptblock in that awkward fashion? Use literal syntax:

$s = { mkdir 'c:\...\blah' }

If you want to create scriptblocks programmatically from text, there's an easier way:

$s = [scriptblock]::create('...') 

Don't forget to use single quotes if you don't want references to variables to be resolved before the scriptblock is parsed.

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

2 Comments

it does not work! I am still getting the same error :S..I just typed "computername" | test within the console in powershell ISE and i received the same error.. Sorry i am new to powershell
thats because your function does not use pipeline input, you should eiter work with parameters, hardcode your values or read up on the basics first
0

Rename your variable $_ and it will work...

Explanation:

$_ is "reserved" for the current value in an iteration e.g. foreach or a pipeline. See here: What does $_ mean in PowerShell?

If you call your function as @x0n said, then it could make sense. If you want your function to create a folder everytime on the same machine, then don't use a variable. But if you want your function to create the folder on different machines your function should know on which computer you want the folder. That is normally done with a function parameter.

[...]
$ipaddress = "192.168.10.4";

function test([string]$ip, [System.Management.Automation.PSCredential]$credentials) {
    $s = $ExecutionContext.InvokeCommand.NewScriptBlock("mkdir C:\'Documents and    Settings'\username\Desktop\Testfolder");
    Invoke-Command -ComputerName $ip -ScriptBlock $s -Credential $credentials;
}

// then call it like this
test $ipaddress $cred;

Here is a list of all those reserved things: http://www.neolisk.com/techblog/powershell-specialcharactersandtokens

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.