run a ps1 file with parameters on a remote computer

corne nietnodig 196 Reputation points
2021-02-17T13:10:07.083+00:00

It looked simple but as a beginner it takes a lot of time starting to run a ps1 file on a remote computer from a fileshare in a network. The goal is to install Microsoft Teams or another software package. How is not important, but i want it thru powershell.

Searching some things together:
find a ps1 file script to install Teams, it must be run with parameter: -source \fileshare to teams executable
check execution policy on remote computer but this seems not nessecairy given the -filepath i believe..

Tried this on my machine to run the script on a remote laptop from a netwerkshare:
powershell.exe -executionpolicy bypass -file "\share\Install-MicrosoftTeams.ps1 -Source \fsnl\gpodiverse\teams\"
A parameter cannot be found that meets parameter SOURCE.
I have tried to run it without source:
powershell.exe -executionpolicy bypass -file "\share\Install-MicrosoftTeams.ps1
This hangs, does nothing keeps blinking the cursor and i break this with ctrl-c

Tried also
Invoke-command -filepath \share\Install-MicrosoftTeams.ps1 -source \share\teams\ -computername laptop111
Here parameter source is not recognised

That simple is so difficult how to run a ps1 file with parameter from my machine on a remote machine and the ps1 file resists on a fileshare...how can i best do this with a batchfile is maybe better?

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,383 questions
0 comments No comments
{count} votes

6 answers

Sort by: Most helpful
  1. MotoX80 32,071 Reputation points
    2021-02-17T19:20:54.703+00:00

    With -Filepath you can pass positional arguments with the -ArgumentList switch.

    Invoke-command -filepath "\\share\Install-MicrosoftTeams.ps1" -ArgumentList "\\share\teams\" -Computername "laptop111"  
    

    I have not been able to find a way to use parameter names. So if the script requires -Source, you won't be able to use -filepath.

    See this site for more info.

    https://stackoverflow.com/questions/4225748/how-do-i-pass-named-parameters-with-invoke-command

    In your case it appears that you have both the script and the install files for the software sitting on a server. You can use -ScriptBlock and have laptop111 pull both the script and the install files directly from the server.

    $script =   
     {  
    	\\share\Install-MicrosoftTeams.ps1  -source \\share\teams\  
     }  
     $User = ".\admin"  
     $PWord = ConvertTo-SecureString -String "password" -AsPlainText -Force  
     $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord  
     Invoke-Command -ComputerName "laptop111" -credential $Credential -ScriptBlock $script   
       
    
     
    

    Because you have to access a second machine (\share) you will run into the double hop problem.

    https://blog.ipswitch.com/the-infamous-double-hop-problem-in-powershell
    https://learn.microsoft.com/en-us/powershell/scripting/learn/remoting/ps-remoting-second-hop?view=powershell-7.1

    If you are using Active Directory accounts, you should be able to use $Credential as in my sample to access the \server\share. I don't have an AD environment to test with, so if that doesn't work, you can map a New-PSDrive to the share and use $Credential to authenticate. Or you can use the other options that those two links spell out.

    Give that a try.

    0 comments No comments

  2. js2010 186 Reputation points
    2021-02-17T23:41:10.033+00:00

    It should work without the quotes

    powershell.exe -executionpolicy bypass -file \\share\Install-MicrosoftTeams.ps1 -Source \\fsnl\gpodiverse\teams\
    

    Or

    powershell.exe -executionpolicy bypass -command \\share\Install-MicrosoftTeams.ps1 -Source \\fsnl\gpodiverse\teams\
    

  3. corne nietnodig 196 Reputation points
    2021-02-19T08:28:27.64+00:00

    I have tried several things now but none seem to work:

    enter-pssession laptop111
    c:\temp\teams-install.exe

    This gives me a blinking cursor but does nothing.

    Also tried:
    Invoke-command -computername lt111 -scriptblock {Start-Process 'c:\temp\teams-install.exe'}
    This gives no error but also does nothing

    When i give: -wait and after that: -asjob and then get-job it sais it failed

    0 comments No comments

  4. corne nietnodig 196 Reputation points
    2021-02-19T08:42:55.41+00:00

    It looks like the exe is not working when running-local on the machine. It copies the files to the local machine and starts the powershell commands but does not start the Teams install.

    This is the script, maybe you can help me what is wrong with it. I have made ps1 and createexefromps1 to make it an executable
    When i run this ps1 on the local machine it does install teams.

    Teams-install.ps1 (which i made a exe file):

    $path = "c:\Scripts"
    md $path
    copy \share*.* $path
    Powershell -executionpolicy bypass .\Install-MicrosoftTeams.ps1 -sourcepath "c:\scripts"
    cd\
    cd c:\scripts

    .\Install-MicrosoftTeams.ps1 -sourcepath "c:\scripts"

    0 comments No comments

  5. MotoX80 32,071 Reputation points
    2021-02-19T12:31:10.287+00:00

    Timeout. There is no need to convert your .ps1 into a .exe.

    Your have 2 main challenges.

    • The double hop authentication problem from workstation (where you are logged on) --> laptop (where the install runs) --> server (where the install source is located)
    • A non-interactive session.

    So let's first verify that we can access the file server from the laptop. Load this script into Powershell_ise and modify the first 4 lines with your values. Run it and verify that all machines can see each other.

    $target = "laptop111"                            # the machine we want to install the software on
    $share = "\\someserver\someshare"      # the file server where to software is stored
    $User = ".\admin"                              # this should be a domain account that has admin rights on the target PC and read access to the file share. 
    $PWord = ConvertTo-SecureString -String "admin" -AsPlainText -Force         # the account's password
    $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord
    $script = 
     {
        "This script is running on {0}" -f $env:COMPUTERNAME
        "**** Mapping a drive to the server where all of the install files and script are stored.."
        # Note the $using statement. This allows the remotely executed script to reference variables in the main script 
        new-psdrive -Name 'B' -PSProvider FileSystem -Root $using:share -Credential $using:credential 
        ""
        "**** Get a count of files/folders to verify that we can access the drive.."
        (get-childitem  b:\ ).count
        ""
        "Now run the install script." 
        # Note that this is being executed in an unattended session. This must be a completely silent install.
        # You cannot run a GUI that prompts the user for input. There is no user to see the prompt. 
        # B:\Install-MicrosoftTeams.ps1 -Source B:\teams\                              # point to correct files/folders and uncomment to run the install 
        ""
        "**** Remove the drive."
        Remove-PSDrive -Name 'B'   
     }
     Invoke-Command -ComputerName  $target -credential $Credential -ScriptBlock $script 
    

    If that works, then you just need to plug the name of the script that does the install and any parameters it requires.

    As the comments note, you are performing an unattended install. There is no user who can "click ok to continue".

    Update: you should investigate the script parameters and the parameters passed to any .exe. Since this is unattended, you will want to find something that enables logging so that your script can read the log and display it back to your Powershell session. That should answer the question of "I launched the program but it didn't do anything".

    0 comments No comments