run a ps1 script in a ps1 script

Dear people,

I hope that some one can give me some information

i have a script for pnp powershel to check if a teamsite exists, if not then create it, if it exists first delete and then recreate.

Now i have a another script that is for provisioning tempate for a teamsite.

now i whant to run that script after my script for site creation is finished i have the following

 

<hr />

 

#create the admin credentials for connecting to sharepoint online services
$Username = "username"
$Password = ConvertTo-SecureString "this will be a password" -AsPlainText -force
$cred = New-Object -TypeName System.management.automation.pscredential -ArgumentList $Username, $Password
$org = "organization"
$siteurl = "https://$org.sharepoint.com/sites/Project001"

Connect-PnPOnline -Url "https://$org-admin.sharepoint.com/" -credential $cred
$Project001 = Get-PnPTenantSite -Identity $siteurl -ErrorAction SilentlyContinue

<#With the if else statement the script will verify if the sitecollection already exisits
If the site doesnt exists it will be created based on the new-pnptenantsite criteria.
If the site already exists it will delete and skip the recycle bin time before recreate the site
#>
if ([string]::IsNullOrEmpty($Project001)) {

Write-Output "a new project site will be created"
New-PnPTenantSite `
-Title "Project001" `
-Url $siteurl `
-Description "test" `
-Owner $username `
-TimeZone 4 `
-Template "STS#3" `
-Wait
Write-output "Site is created"

} else {
Write-Output "the project site already exist and will now be deleted"
Remove-PnPTenantSite -url "url" -Force -SkipRecycleBin
Write-Output "Old site is now deleted"

Write-Output "The project site will now be re-created"
New-PnPTenantSite `
-Title "Project001" `
-Url $siteurl `
-Description "provisioning testing for communities" `
-Owner $Username `
-TimeZone 4 `
-Template "STS#3" `
-Wait
Write-output "Site is created"

}

$scriptfilelocation = "C:\temp\testscript.ps1"
Write-Output "The template provisioning for projects will nou start"
Invoke-Expression -Command $scriptfilelocation <#what must be done here#>
Write-Output "The template provisioning is succesful runned"

 

Please format you code as code here in the forum. Use the code tag buttons “pre” right above the edit windows when you create a post. Thanks.

When you like to invoke another Powershell script from inside a Powershell script you simply provide the name of the script - ideally including the complete path. You don’t need any special syntax or cmdlet. If you want to run the seconds script inside the same scope you have to provide it dot sourced. (a dot followed by a space followed by the name of the script)

do you have a example?

… example for what??

'C:\abitrary path\to another Folder\ with another\script.ps1'

. 'c:\another abitrary path\ to another Folder\with another script.ps1'

The first line executes the script in a seperate scope, the second line executes another script in the same scope as the script it is launched from.

Why the two scripts. Simplify this effort.
Write one script with 2 functions in one script.
Call the first function, check for complete, that call the second function.