header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
multiple variables don't pass correctly to function
Last Post 26 Jul 2010 01:52 PM by 0ptikGhost. 2 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages Not Resolved
jaysunUser is Offline
New Member
New Member
Posts:1
Avatar

--
24 Jul 2010 05:04 AM
    I am writing a script to copy files to multiple computers by taking source file, hostname list and destination location as the inputs. Following is the script (removed actual copy-item command for debugging);

    function mycopyfile{
    param($sourcefile, $hostlist, $filedestination)
    Write-host $sourcefile
    Write-host $hostlist
    Write-host $filedestination
    }
    $sourcefile = $args[0]
    $hostlist = $args[1]
    $filedestination = $args[2]
    mycopyfile($sourcefile, $hostlist, $filedestination)

    When I run this I get the following output;

    PS C:\powershell scripts> .\paratest.ps1 c:\abc.txt d:\cde.txt scripts
    c:\abc.txt d:\cde.txt scripts

    As you can see all the three variables are assigned to "$sourcefile" inside the function and therefore when used with the copy-item command it fails because $hostlist and $filedestination are null.

    How can I pass the variables ( "c:\abc.txt", "d:\cde.txt" and "scripts") correctly?

    PS : I have tried with single and double quotes but all failed. If I do write-host just before calling mycopyfile then the variables are showing correctly. Just running function with three values also display them properly so I think this relates to error is value passing.
    George HowarthUser is Offline
    Basic Member
    Basic Member
    Posts:360
    Avatar

    --
    26 Jul 2010 01:37 AM

    Try this:

    param (
        [String[]]$Computer,
        [String]$Path,
        [String]$Destination
    )

    function mycopyfile
    {
        param (
            [String[]]$Computer,
            [String]$Path,
            [String]$Destination
        )
       
        Write-Host $Computer
        Write-Host $Path
        Write-Host $Destination
    }

    mycopyfile -Computer $Computer -Path $Path -Destination $Destination

    I changed the variable names to make it more consistent with existing variable names in PowerShell.

    0ptikGhostUser is Offline
    Basic Member
    Basic Member
    Posts:296
    Avatar

    --
    26 Jul 2010 01:52 PM

    The reason you got all your values assigned to $sourcefile is that is that your invocation of the mycopyfile function passed only one object to the function:

    mycopyfile($sourcefile, $hostlist, $filedestination)

    Is equivalent to:

    mycopyfile -sourcefile @($sourcefile, $hostlist, $filedestination)

    The correct way to invoke the function is:

    mycopyfile -sourcefile $sourcefile -hostlist $hostlist -filedestination $filedestination

    Or by using positional parameters:

    mycopyfile $sourcefile $hostlist $filedestination

    You really should consider changing the parameter names to be more consistent with powershell conventions.

    I think it is unfortunate that powershell "functions" are declared as functions because they are very clearly not used like functions.

    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer