How to return the value of a variable from a function

0

I need to return the value of the variables from a function. This is the function, but the variables return them to me empty.

function listcert
{
    Get-ChildItem "P:\" -name
    Write-Host ""
    [string] $path = Read-Host -Prompt "Specify the path or the certificate "

    if ((Get-Item "P:\$path") -is [System.IO.DirectoryInfo])
    {
        Get-ChildItem "P:\$path" -name
        Write-Host ""
        [string] $certificate = Read-Host -Prompt "Specify the certificate "
        $pass = Read-Host -Prompt "Set the password for the certificate: "
        [Security.SecureString] $mypwd = ConvertTo-SecureString -String $pass -Force –AsPlainText
    }

    else
    {
        $pass = Read-Host -Prompt "Set the password for the certificate: "
        [Security.SecureString] $mypwd = ConvertTo-SecureString -String $pass -Force –AsPlainText
    }

    Remove-PSDrive P
    return ,$path
    return ,$certificate
    return ,$mypwd
}
    
asked by Carlos Sánchez 24.01.2017 в 20:02
source

3 answers

1

When doing the return, it immediately returns the value of the variable you associated, I advise you to build an array with the variables $ path, $ certificate, $ mypwd and make the return of the array

$arreglo['path']=$path
$arreglo['certificate']=$certificate
$arreglo['mypwd ']=$mypwd 

return $arreglo;
    
answered by 09.03.2018 в 14:47
0

When you use the return instruction, the value is returned immediately and the execution returns to the caller.

In PowerShell , all values sent to the pipleine (output stream) are the return value, so you only need the return instruction if you want to stop the function immediately.

You also do not need to use the comma unary to create an array. You are already returning 3 variables to be a matrix, so you can only separate all the values with commas:

return $path,$certificate,$mypwd

... and since it returns them at the end of the function, you do not need the return declaration:

$path,$certificate,$mypwd

Additional information

You must change the parameters of your take function instead of using Read-Host to read them at run time. This offers more flexibility when calling the function, and if you set the parameters _mandatory (required), PowerShell will prompt the user if the value is not provided.

Consider using a [PSCredential] object instead of [SecureString] . It's easier to use and the .password property of the credential is already a secure string.

Example:

function listcert
{
    param(
        [Parameter(Mandatory=$true)]
        [String]
        $Path ,

        [Parameter(Mandatory=$true)]
        [String]
        $Certificate ,

        [Parameter()] # No es obligatorio para que podamos personalizar
        [PSCredential]
        $Credential = (Get-Credential -UserName Ignorado -Message 'Set the password for the certificate')
    )
    Get-ChildItem "P:\" -name

    if ((Get-Item "P:\$path") -is [System.IO.DirectoryInfo])
    {
        Get-ChildItem "P:\$path" -name
    }
    Remove-PSDrive P

    $path,$certificate,$mypwd
}
    
answered by 21.05.2017 в 20:36
0

You could return the variables as a PSObject:

return New-Object PSObject -Property @{
   Path = $path
   Certificate = $certificate
   Password = $mypwd
}
    
answered by 24.05.2017 в 16:34