locked
How to execute a Function from Powershell V2. RRS feed

  • Question

  • Hi,

    I have created a function in the Powershell script having file name remotedesktop.ps1

    function FindDuplicate($server,$config)

    {

    }

    when i execute the function using below:

    Ps>c:\>users>administrator>FindDuplicate server1 config.xml

    The term 'FindDuplicate' is not recognized as the name of a cmdlet, function, s
    cript file, or operable program. Check the spelling of the name, or if a path w
    as included, verify that the path is correct and try again.
    At line:1 char:14
    + FindDuplicate <<<<  RADEV1WCF2 c:\users\administrator.ade\config.xml
        + CategoryInfo          : ObjectNotFound: (FindDuplicate:String) [], Comma
       ndNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException

    Please let me know how to execute the function.

    Thanks

    Jaspreet Singh

     

     

     

     

    • Moved by Bill_Stewart Wednesday, July 26, 2017 6:43 PM "answer" link no longer exists
    Tuesday, November 23, 2010 9:10 AM

Answers

  • your function is in the powershell script, so the shell wont know about the function.

    you have to do a "dot sourcing"

    so you have to call it like this as mentioned in this url:

    http://lo0m.blogspot.com/2008/08/powershell-calling-functions-from-file.html

    thanks

    thiyagu


    Thiyagu | MCTS/MCITP - Exchange 2007 | MCSE 2003[Messaging] | http://www.myExchangeWorld.com. This posting is provided "AS IS" with no warranties, and confers no rights.
    • Edited by Thiyagu14 Tuesday, November 23, 2010 10:29 AM updated.
    • Marked as answer by Jaspreet Singh New Wednesday, November 24, 2010 9:32 AM
    Tuesday, November 23, 2010 10:28 AM

All replies

  • your function is in the powershell script, so the shell wont know about the function.

    you have to do a "dot sourcing"

    so you have to call it like this as mentioned in this url:

    http://lo0m.blogspot.com/2008/08/powershell-calling-functions-from-file.html

    thanks

    thiyagu


    Thiyagu | MCTS/MCITP - Exchange 2007 | MCSE 2003[Messaging] | http://www.myExchangeWorld.com. This posting is provided "AS IS" with no warranties, and confers no rights.
    • Edited by Thiyagu14 Tuesday, November 23, 2010 10:29 AM updated.
    • Marked as answer by Jaspreet Singh New Wednesday, November 24, 2010 9:32 AM
    Tuesday, November 23, 2010 10:28 AM
  • Thanks Thiyagu for your Reply -:)....it worked that way....

    just a small question..i am doing remoting with powershell and passing variable to script block...

    function FindDuplicate($server,$config)

    {

    $configfile=$config

    $script=

    {

    Write-host $configfile  

    [xml]$e=GC $configfile | ?{$_ -ne""}

    }

     

     

    invoke-command -computername $servername -authentication CredSSP -Credential $cred -scriptblock $script -EA Stop;

     }

    Output: System.Management.Automation.RemoteException: Cannot bind argument to parameter 'Path' because it is null.
    Please let me know how i can use variable $configfile in Script block.

    Thanks

    Jaspreet Singh

     

     

     

    Tuesday, November 23, 2010 11:55 AM
  • Does this work?  I can't do any remote testing from where I am, and I question why that  | ?{$_ -ne"" is necessary.

    function FindDuplicate($server,$config)

    {

    $configfile=$config

    $script_str=@"
    Write-host $configfile 
    [xml]$e=GC $configfile | ?{$_ -ne""}
    "@

    $script = [scriptblock]::create($script_str)
     
    invoke-command -computername $servername -authentication CredSSP -Credential $cred -scriptblock $script -EA Stop;

     }


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Tuesday, November 23, 2010 12:25 PM
  • Okay, I just realize why that might be necessary.  This will probably work better ( I think):

    function FindDuplicate($server,$config)

    {

    $configfile=$config

    $script_str= 'Write-host ' + $configfile +  ';[xml]$e=GC ' + $configfile + ' | ?{$_ -ne""}'

    $script = [scriptblock]::create($script_str)
     
    invoke-command -computername $servername -authentication CredSSP -Credential $cred -scriptblock $script -EA Stop;

     }



    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Tuesday, November 23, 2010 12:34 PM
  • Hi Mjolinor,

    I think i need to clarify a bit.

    I am reading the xml file that is on the remote machine and finding duplicate entires in that xml file.

    So i am passing the complete path of the xml file of the remote machine from the PS prompt.

    For Example:

    PS> Find-Duplicate server1 c:\Power\config.xml

    Now issue is occuring is in function:

    Function Find-Duplicate($server,$config)
    
    try
    
    {
    
    $servername=$server 
    
    $Configfile=$config
    
    
    Set-item WSMan:\localhost\shell\MaxMemoryPerShellMB 1000
    
    
    $Script=
    
    {
    
    [xml]$e=GC $configfile
    
    //When i try to use write-host just to see what is the value of variable $configfile in the $Script block , it is coming as Null. But above script block it's value is complete path of xml file what i give on Powershell prompt.
    
    //some logic 
    
    }
     invoke-command -computername $servername -authentication CredSSP -Credential $cred -scriptblock $script -EA Stop; 
    
     }
    
    Output: System.Management.Automation.RemoteException: Cannot bind argument to parameter 'Path' because it is null.
    
    
    
    

    Now question is how i can use the value of variable $configfile in Script block.

     Thanks

    Jaspreet

     

     

    Wednesday, November 24, 2010 5:40 AM