none
Invoke-command cannot validate argument computername RRS feed

  • Question

  • Hi guys,

    Below is my simple script testing:

    Invoke-Command -ComputerName $remoteFQDN -ScriptBlock {Write-Host "hello world !"} -Authentication Credssp -Credential $CustomCred

    The above script work when i did not put it inside as a function. After putting it as a function i am getting error:

    Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Provide an argument that is not null or 
    empty, and then try the command again.

    I have also print out the variable $remoteFQDN and ensure that it is not null or empty.

    Tuesday, July 23, 2019 10:18 AM

All replies

  • The variable $remoteFQDN might not be declared before or inside the function so not holding any value. Try to declare the same inside the function as a local variable.
    • Edited by DumbleD0re Tuesday, July 23, 2019 10:21 AM
    Tuesday, July 23, 2019 10:21 AM
  • Hi dumbledore,

    I have tried setting the $remoteFQDN as a local variable:

    $localRemoteFQDN = $remoteFQDN;

    $remoteFQDN was a variable that pass as a parameter. I have printed out both variable and it state there were values.

    Tuesday, July 23, 2019 10:29 AM
  • There is no function here. How can you say it doesn't work in a function when there is no function.

    To access a variable in a  function you must declare the variable outside of the function and assign it a value.  It cannot be assigned inside of another function.

    Assigning a carriable that is null to another local variable does not solve anything.

    You following line is pointless:

    localRemoteFQDN = $remoteFQDN;

    If the variable is null assigning it to a local variable will just make the local variable null.

    You need to start by learning basic PowerShell:  Getting Started with Microsoft PowerShell

    Once you learn the basics all of this will makes sense.


    \_(ツ)_/

    Tuesday, July 23, 2019 10:53 AM
  • Please post your function.
    Tuesday, July 23, 2019 10:57 AM
  • Hi JRV,

    It obvious that i didnt post out all i had as i am not able to do it due to company policy.

    Secondly i am just following what dumbledore said.

    As for the function i dont see a point posting everything out as the only thing that not working was that line of code which was basically copy over from a normal script.

    Also i am not sure if you have finish reading my post (or maybe my grammar was too bad for you) i said i have ensure that $remoteFQDN has a value i did it by write-host $remoteFQDN on the function itself before running the invoke-command.

    Lastly can i kindly request you not to reply to my post Thanks. You maybe good on what your doing however i really cant communicate with someone that has a jehovah complex (I read so many post when you reply to others honestly just make me think if they had offended you in some other ways).
    Tuesday, July 23, 2019 3:04 PM
  • Hi dumbledore,

    Below here my function:

    function createCluster($var1, $var2, $remoteFQDN, $var3, $var4) {
        $username = "abc"
        $password = "ISHALLNOTBEAJERK!"
        $secureStringPwd = $password | ConvertTo-SecureString -AsPlainText -Force 
        $CustomCred = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $secureStringPwd

        Write-Host $var1, $var2, $remoteFQDN, $var3, $var3, 
        try{
             Invoke-Command -ComputerName $localRemoteFQDN -ScriptBlock {Write-Host "hello world !"} -Authentication Credssp -Credential $CustomCred
        }catch{
            $createClusterError = $_.Exception.Message;
        }

        $returnValue = ifError($createClusterError);
        return $returnValue;
    }

    Based on the write-host output i am able to see all variable value.

    Tuesday, July 23, 2019 3:12 PM
  • The variable may also be the wrong kind such as an array with a null first. In that case doe this to find out:

    Write-Host $remoteFQDN.GetType()

    In any case you need to start by learning basic PowerShell usage.  You will learn how to debug, validate variables and control things like scope.

    The issue is not your grammar, it is your lack of training in scripting/PowerShell.  If you learn PowerShell you will be able to see what your code is doing wrong.  Without your code we cannot guess and you should not ask us to guess.


    \_(ツ)_/

    Tuesday, July 23, 2019 3:12 PM
  • Please try by writing your code correctly for what you are doing:

    function Create-Cluster{
        Param(
            [ValidateNotNullOrEmpty()]
            [string]$var1,
            [ValidateNotNullOrEmpty()]
            [string]$var2,
            [ValidateNotNullOrEmpty()]
            [string]$remoteFQDN,
            [ValidateNotNullOrEmpty()]
            [string]$var3,
            [ValidateNotNullOrEmpty()]
            [string]$var4
        )
        $username = "abc"
        $password = "ISHALLNOTBEAJERK!"
        $secureStringPwd = $password | ConvertTo-SecureString -AsPlainText -Force
        $CustomCred = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $secureStringPwd
        
        Write-Host $var1, $var2, $remoteFQDN, $var3, $var3
        try{
            Invoke-Command -ComputerName $RemoteFQDN -ScriptBlock {
                Write-Host "hello world !"
            } -Authentication Credssp -Credential $CustomCred -ErrorAction Stop
        }
        catch{
            Throw $_
        }
    }
    This  will validate your passed variables.  Also the try/catch will work.


    \_(ツ)_/





    • Edited by jrv Tuesday, July 23, 2019 3:32 PM
    Tuesday, July 23, 2019 3:26 PM
  • I also fixed a few programming errors and variables name issues.


    \_(ツ)_/

    Tuesday, July 23, 2019 3:32 PM
  • Note also - with try/catch "erroraction stop" is usually required but with "Invoke-Command " it is optional. It should be used anyway for clarity.


    \_(ツ)_/

    Tuesday, July 23, 2019 3:40 PM
  • Hi JRV,

    Thanks for helping. I manage to find out what going on. Issue was me calling my function like i am writing other programming language.

    funtion test(var1, var2) {...}

    my calling syntax was test($param1, $param2);

    where it should just be test $param1 $param2.


    Wednesday, July 24, 2019 1:22 AM
  • Which is why I posted this:

    You need to start by learning basic PowerShell:  Getting Started with Microsoft PowerShell

    You also need to read the following:

    How to post code in Technet Forums

    If you learn PowerShell correctly and lose your misunderstandings learned by guessing and asking untrained users you will do much better and get answers faster and they will be easier for you to detect the bad answers.


    \_(ツ)_/

    Wednesday, July 24, 2019 1:27 AM