none
Put output of function in varialbe? RRS feed

  • Question

  • Hi,

    I have this function which  I like to store in a variable but the variable does not store it:

    $pcname = Get-pcname -user myuser

    Ping $pcname does not work.

    Please advise.
    J.


    Jan Hoedt

    Thursday, September 24, 2015 11:08 AM

Answers

  • You are not returning anything from the function.

    help about_functions


    \_(ツ)_/

    • Marked as answer by janhoedt Thursday, September 24, 2015 12:45 PM
    Thursday, September 24, 2015 12:14 PM

All replies

  • What is "Get-PcName"?


    \_(ツ)_/

    Thursday, September 24, 2015 11:28 AM
  • That's the outcome of this:

    function get-pcname
        {
         Param(
            [string]$user)
        $share  = "\\server\share"
        $path = Join-Path -Path $share -ChildPath "$user.txt"
        $LastLine = Get-Content $path | Select-Object -last 1
        $ComputerName = ($LastLine -split ' ')[4]
        write-host $ComputerName
        }


    Jan Hoedt

    Thursday, September 24, 2015 11:37 AM
  • So I guess the problem resides in the function get-pcname, since I get a result when I use

    get-pcname user

    but when I put it in a variable

    $user = myuser
    $pcname = get-pcname $user

    there is no output when I type $pcname


    Jan Hoedt

    Thursday, September 24, 2015 12:11 PM
  • You are not returning anything from the function.

    help about_functions


    \_(ツ)_/

    • Marked as answer by janhoedt Thursday, September 24, 2015 12:45 PM
    Thursday, September 24, 2015 12:14 PM
  • As simple as that indeed.

    return$ComputerName


    Jan Hoedt

    Thursday, September 24, 2015 12:46 PM