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?

Matt
  • 45,022
  • 8
  • 78
  • 119
Jendizer
  • 27
  • 1
  • 1
  • 4
  • Syntax is `function test() { ... }` – algorhythm Oct 20 '14 at 16:29
  • `$_` is not defined in your function. Use a function parameter. – algorhythm Oct 20 '14 at 16:30
  • How do i use a function parameter? – Jendizer Oct 20 '14 at 16:50
  • Look here: http://stackoverflow.com/questions/4988226/how-do-i-pass-multiple-parameters-into-a-function-in-powershell – algorhythm Oct 20 '14 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. – Jendizer Oct 20 '14 at 16:59
  • Yeah but why is your variable called `$_`. It's reserved for the current value in an iteration e.g. `foreach`. And I cannot see your function call. Is there some more code? If you call your function as @x0n said, that 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. – algorhythm Oct 20 '14 at 17:08
  • Oh man i didn't realise that $_ was reserved i just renamed it and it worked! Thanks man..I appreciate it. – Jendizer Oct 20 '14 at 17:24
  • Ok, I posted it as my answer... – algorhythm Oct 20 '14 at 17:25

2 Answers2

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.

x0n
  • 51,312
  • 7
  • 89
  • 111
  • 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 – Jendizer Oct 20 '14 at 16:41
  • 1
    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 – Paul Oct 20 '14 at 17:08
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

Community
  • 1
  • 1
algorhythm
  • 8,530
  • 3
  • 35
  • 47