Smart Way To Call Another Power Shell Script Inside A Script

Introduction 

 
Hi guys, let's explore a smart way to call another Power Shell script inside a script.
 
Let my given script be a test.ps1 which only does some write-host that prints some statement.
 
Now I'm adding one more line that calls a PowerShell script to execute.
 
Write-Host "Hello World!";
 
Write-Host "Invoking or Calling another PnP PS Script";
 
Powershell .\CreateFolder_PnP
 
 
 
 
 
 
The above CreateFolder_PnP script is shown below:
  1. #Config Variables  
  2. $SharePointSiteURL = "#######################"  
  3. $FolderName = "Extra Folderss"  
  4. $RelativeURL = "/Reports Archive"  
  5. #Relative URL of the Parent Folder  
  6. Try {  
  7.     #Connect to PnP Online  
  8.     # Connect - PnPOnline - Url $SharePointSiteURL - CurrentCredentials  
  9.     Connect - PnPOnline - Url $SharePointSiteURL - UseWebLogin  
  10.     # sharepoint online create folder powershell  
  11.     Add - PnPFolder - Name $FolderName - Folder $RelativeURL - ErrorAction Stop  
  12.     Write - host - f Green "New Folder '$FolderName' Added!"  
  13. catch {  
  14.     write - host "Error: $($_.Exception.Message)" - foregroundcolor Red  
  15. }  

Conclusion

 
We can automate the above process to call any kind of verified, tested PS scripts from a master PS script file and run it across a CMD, Windows ISE, PowerShellManagement Shell Tool, or Azure PS Runbook, and implement scheduled timer automated web jobs also established using the above simple process.
 
Sharing is Caring!
 
Cheers!! 
Ebook Download
View all
Learn
View all