Call Powershell Script from another Script. RRS feed

  • Question

  • Hello everyone,

    I am stuck and would like your help. I am trying to call one posershell script from another script and so far i haven't been able to come up with a solution. My code as follows.

    $computername = Get-Content 'C:\Computer Lists\Desktop Center for Servers.txt'
    $sourcefile = "\\3474-VM\Desktop Central Install\*.*"
    #This section will install the software 
    foreach ($computer in $computername) 
    $destinationFolder = "\\$computer\C$\download"
    #This section will copy the $sourcefile to the $destinationfolder. If the Folder does not exist it will create it.
    if (!(Test-Path -path $destinationFolder))
    New-Item $destinationFolder -Type Directory
    Copy-Item -Path $sourcefile -Destination $destinationFolder
    Invoke-Command -ComputerName $computer C:\download\psinstallagent.ps1 -DeploymentType Install -DeploymentMode Interactive

    Thursday, September 24, 2020 6:15 PM

All replies

  • To remotely call a script stored on a remote computer:

    $remoteScript = {
        C:\download\psinstallagent.ps1 -DeploymentType Install -DeploymentMode Interactive
    Invoke-Command -ScriptBlock $remoteScript -ComputerName $computer

    To remotely execute a local script on a remote computer.

    $script = 'C:\download\psinstallagent.ps1'  
    $argumentList = '-DeploymentType Install -DeploymentMode Interactive'
    Invoke-Command -File $script -ArgumentList $argumentList -ComputerName $computer


    Thursday, September 24, 2020 8:15 PM
  • @jrv Thank you very much for that! now i just have to figure out "script not digitally signed" error. I know that i have to use "Set-Execution Policy" i just have to figure out where in the script it belongs. 
    Thursday, September 24, 2020 8:58 PM
  • Execution policy is set globally by GP and can only be set outside of a \script.

    Please take the time to l3earn basic PowerShell and all of this will become understandable.


    Friday, September 25, 2020 2:35 AM