none
Calling one PowerShell Script from Another RRS feed

  • Question

  • Hi,

    I am trying to write a PowerShell script that will call another PowerShell script when the first one finishes. Can't get it to work.

    1. First script downloads some folders and files from a network share to the users' local C: Drive

    2. When the files have finished copying, then fire off another PowerShell script that is part of the files that were copied to the local computer.

        

    # Specify the source and destination paths
    $Source = "\\SERVER\SHARE$\PSAPPDEPLOY\"
    $Destination = "C:\PSAPPDEPLOY"

    # Check if the folder exists and if not create it and copy the PSAppDeploymentKit files to the local C: Drive

    If (!(Test-Path $Destination)) {

       Copy-Item -Path $Source -Destination $Destination -Recurse

    }

    Else {
       Write-Host "Directory already exists!"
       Exit
    }

    $scriptpath = C:\PSAPPDEPLOY\Deploy-Application.ps1

    $ArgumentList = -DeploymentType Install - DeployMode Interactive

    Invoke-Expression "$scriptPath $argumentList"

    Also, is there a way to bypass UAC and get these to fire off without the prompt? I see a way to do it via a batch file, but was hoping to use PowerShell for the whole thing.

    Here's a batch file example:

    @ECHO OFF
    PowerShell.exe -NoProfile -Command "& {Start-Process PowerShell.exe -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""C:\PSAPPDEPLOY\Deploy-Application.ps1""' -Verb RunAs}"
    PAUSE


    Wednesday, August 24, 2016 10:01 PM

Answers

All replies

  • Just call the script.  You do not need a new copy of PowerShell.

    C:\PSAPPDEPLOY\Deploy-Application.ps1 -DeploymentType Install - DeployMode Interactive

    That is all you need.


    \_(ツ)_/

    Wednesday, August 24, 2016 10:16 PM
    Moderator
  • ok thanks. I will try it.

    Any thoughts on how to run the second PS script 'As Administrator'? It must be run in that context otherwise it does not work.

    Wednesday, August 24, 2016 11:33 PM
  • You cannot run a scripts as admin under a non-admin account.

    \_(ツ)_/

    Wednesday, August 24, 2016 11:46 PM
    Moderator
  • what about as a Logon script or a scheduled task?
    Thursday, August 25, 2016 12:09 AM
  • what about as a Logon script or a scheduled task?

    Not a problem.

    \_(ツ)_/

    Thursday, August 25, 2016 12:52 AM
    Moderator
  • Hi Vegas,

    >>I am trying to write a PowerShell script that will call another PowerShell script when the first one finishes.

    You could consider using workflow, here is a basic quick start:

    https://blogs.technet.microsoft.com/heyscriptingguy/2012/12/26/powershell-workflows-the-basics/

    Besides, I suppose we could also try this:

    # Specify the source and destination paths
     $Source = "\\SERVER\SHARE$\PSAPPDEPLOY\"
     $Destination = "C:\PSAPPDEPLOY"
     $cred = Get-Credential corp\administrator #this variable hold the credential if you need to use, just add -credential $cred
     $scriptpath = "C:\PSAPPDEPLOY\Deploy-Application.ps1"
    
    # Check if the folder exists and if not create it and copy the PSAppDeploymentKit files to the local C: Drive
    begin{
        Set-ExecutionPolicy Bypass
        $VerbosePreference = 'continue'
    }
    process{
        if(!(Test-Path $Destination)) {
            Copy-Item -Path $Source -Destination $Destination -Recurse
         }
         Else {
            Write-Host "Directory already exists!"
            Exit
         }
        Write-Warning "coyied to destination success!"
        Write-Verbose "Begin the next script"
        Invoke-Expression -Command $scriptpath
    }
     
    

    Invoke-Expression

    https://technet.microsoft.com/en-us/library/hh849893.aspx?f=255&MSPPError=-2147217396

    Best regards,

    Andy_Pan


    Please remember to mark the replies as an answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft..com.

    Thursday, August 25, 2016 5:52 AM
    Moderator
  • Andy_Pan

    I'll definitely try your ideas. My biggest problem is getting around the fact that the second script *must* be Run As Administrator. No way around it as the PS script uses files that are somewhat unique and they can only do their job if run under that context. I have tried and tried, but cannot get it to work.

    Friday, August 26, 2016 11:36 PM
  • # Specify some global settings
    $CurDate = get-Date -format yyyyMMdd_hhmmss
    $CurDir = "C:\Temp"
    $DirectoryFound = "$CurDir\DirectoryFound_$CurDate.log"
    $DirectoryNotFound = "$CurDir\DirectoryNotFound_$CurDate.Log"
    $O16C2RFound = "$CurDir\O16C2RFound_$CurDate.log"
    
    # Specify the source and destination paths
    $Source = "\\SERVER\SHARE$\PSAPPDEPLOY\"
    $Destination = "C:\PSAPPDEPLOY"
    
    # Credentials
    $secpass = ConvertTo-secureString "password" -asPlainText -Force
    
    $cred = New-Object System.Management.Automation.PSCredential ("DOMAIN\localadmin",$secpass)
    
    # Check if the folder exists and if not create it and copy the PSAppDeploymentKit files to the local C: Drive
     
    begin{
        Set-ExecutionPolicy Bypass
        $VerbosePreference = 'continue'
    }
    process{
        if(!(Test-Path $Destination)) {
            Copy-Item -Path $Source -Destination $Destination -Recurse
            Write-Output "PSAPPDEPLOY folder created and contents copied from network share." | Out-File $DirectoryNotFound -append 
         }
         Else {
            Write-Host "Directory already exists!"
            Write-Output "PSAPPDEPLOY folder already exists" | Out-File $DirectoryFound -append
            Exit
         }
        Write-Warning "copied to destination success!"
        Write-Verbose "Begin the next script"
      # Invoke-Expression -Command $scriptpath
    }
    # Set the initial Office folder
    [string] $dirOfficeC2RX86 = Join-Path -Path "C:\Program Files (x86)" -ChildPath "Microsoft Office"
    [string[]]$officeExecutables = 'excel.exe', 'onenote.exe', 'outlook.exe', 'powerpnt.exe', 'winword.exe'
    
    ForEach ($officeExecutable in $officeExecutables) {
                If (Test-Path -Path (Join-Path -Path $dirOfficeC2RX86 -ChildPath "root\Office16\$officeExecutable") -PathType Leaf) {
                Write-Output "Microsoft Office 2016 C2R was detected. Ending script." | Out-File $DirectoryFound -append
                Exit
                }
    Else {
           Write-Output "Microsoft Office 2016 Will Be Installed." | Out-File $DirectoryNotFound
           C:\PSAPPDEPLOY\Deploy-Application.ps1 -DeploymentType Install -DeployMode Interactive
           }

    Friday, August 26, 2016 11:55 PM
  • You cannot avoid the need to runas administer.  If you need to elevate use "RunAs" but you will be prompted fro elevation.

    \_(ツ)_/

    Saturday, August 27, 2016 12:31 AM
    Moderator
  • ok. How would I add that into the code then?

    I can probably create a temporary local admin on the machines and give the password to the users. It's only temporary anyway.

    Saturday, August 27, 2016 2:07 PM
  • You cannot bypass the requirement for elevation.

    Please look up the instructions on how to install Office in a network. 

    Note that it is recommended that Office 32 bit be used on all systems for compatibility reasons.

    https://www.microsoft.com/en-us/download/details.aspx?id=49117


    \_(ツ)_/

    Saturday, August 27, 2016 2:23 PM
    Moderator