0

How to call one function inside another function along with passsing arguments in Powershell?

 Add-PSSnapIn Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

[void][System.Reflection.Assembly]::LoadWithPartialName  
("Microsoft.SharePoint")
[void][System.Reflection.Assembly]::LoadWithPartialName  
("Microsoft.SharePoint.Workflow")

#region  UploadConfidWSPSolnToGallery

function UploadConfidWSPSolnToGallery($mDisciSiteURL,$mfullConfidWFfilepath)
{
 Write-Host  -ForegroundColor Yellow  "inside  
 UploadConfidWSPSolnToGallery     with url.... " $mDisciSiteURL  "and ..." 
 $mfullConfidWFfilepath
  #foreach($singleSiteCollec in  $disciplineNamesArray)
 # call the child function
 #region UploadCreateApproverInfoOnRootWeb
 function UploadCreateApproverInfoOnRootWeb($paramSiteURL)
 {
  Write-Host  -ForegroundColor Yellow  "inside   
  UploadCreateApproverInfoOnRootWeb with url.... " $paramSiteURL
  $siteObj = Get-SPSite $paramSiteURL
  $roottWebb= $siteObj.RootWeb
  function UploadListTemplate($roootWebURL, $TemplateFilePath) 
  { 
   Write-Host  -ForegroundColor Yellow  "inside  UploadListTemplate with 
    url.... " $roootWebURL "..and templatefilepath..." $TemplateFilePath
    try
    {
        #Get the Web 
        $rweb = Get-SPWeb $roootWebURL 
        #Get the List template Gallery Folder 
        $TemplateFolder = $rweb.GetFolder("List Template Gallery") 
        #Get the Files collection 
           $TemplateFileCollection = $TemplateFolder.Files 
    }

   #####################calling main function below  
             ##################################################
       Write-Host "calling function  ActivateConfidentailWorkFlow"

        ActivateConfidentailWorkFlow($xml_disciplineurl)
     ####################################################################
   the issue is here that,i am not able to call the functions which is written above.
1

To call a function in PowerShell the function needs to have been declared before it is called (int the document order or if in an external module the module must have been loaded before the call.)

To pass arguments to a function write it like this functionName $argument1 $argument2 so your call should look like this ActivateConfidentailWorkFlow $xml_disciplineurl

hope it works.

Edit: Passing arguments to a function can be done in multiple ways but the above example is based on your code example.

Edit2: Could also be a scoping issue as you have a function in a function in a function. Which, to me, is a weird way of writing things, but it if work it works. Then it could be that your 3rd level function cannot see the function ActivateConfidentialWorkflow, which is not included in the code sample, because it is outside the scope of the child function. Scoping is a big topic but you can read more about it here about_Scopes

| improve this answer | |
  • that was helpful information and reply.So since my code looks wierd, can you please suggest how to refine this. – userAZLogicApps Apr 22 '15 at 9:15
  • My intention was to split the functionality into small functions and execute in a modular way – userAZLogicApps Apr 22 '15 at 9:40
  • 1
    That sounds like a good idea using modules, it will make it more readable and reusable. Now i have read up myself on the functions in functions and I understand that it is used to hide the functions, like setting the scope to private(just think 3 levels are a bit deep). If you do it in a Modules then it would also be possible to make the functions available in the scope where the module is loaded, which would also hide the functions from the rest of the script. It doesn't sound like you need any tip from me. :) – Robban1980 Apr 22 '15 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.