Running a script with parameters on remote computers

Welcome Forums General PowerShell Q&A Running a script with parameters on remote computers

Viewing 9 reply threads
  • Author
    Posts
    • #273451
      Participant
      Topics: 9
      Replies: 30
      Points: 129
      Rank: Participant

      Hi,

      I’m trying to run a PowerShell script along with parameters on remote computers using the below:

      This returns the error

      Invoke-Command : The value of the FilePath parameter must be a Windows PowerShell script file

      I’ve tried the following alternatively but my understanding is that this is used to pass local variables rather than the script parameters

      Is what I’m trying to do even possible?

      • This topic was modified 1 month, 3 weeks ago by jshizzle14.
      • This topic was modified 1 month, 3 weeks ago by jshizzle14.
    • #273484
      Participant
      Topics: 5
      Replies: 249
      Points: 985
      Helping Hand
      Rank: Major Contributor

      Unfortunately the -ArgumentList parameter of Invoke-Command takes an object collection so passing arguments using named parameters in that parameter is not possible.  You would have to pass them positionally in the order they appear in the script.  If FixUninstall is the first parameter you would just pass “$true” like this:

      Including -Whatif may be a challenge since it is a common parameter.  There are some ways to do this but it is a little tricky.  One way to do it is to modify your script to take a parameter to set the WhatIf switch.

    • #273789
      Participant
      Topics: 9
      Replies: 30
      Points: 129
      Rank: Participant

      This is a 3rd Party script I’ve found that addresses a vulnerability so wasn’t created by myself, however, does do what’s intended when run locally.

      The WhatIf parameter is actually a builtin switch as it goes so can be called.

      Many of the parameters are simply switches that don’t expect a value:

      I’ve tried the following but this gives me the error: “A positional parameter cannot be found that accepts argument ‘True'”

      What am I missing?

    • #274098
      Participant
      Topics: 17
      Replies: 1951
      Points: 3,986
      Helping Hand
      Rank: Community Hero

      Rather than using ArgumentList, try named parameters:

    • #274125
      Participant
      Topics: 5
      Replies: 249
      Points: 985
      Helping Hand
      Rank: Major Contributor

      Rather than using ArgumentList, try named parameters:

      Rob, I’m not aware of how to use named parameters with Invoke-Command.  -FilePath will only take a path to a script (no arguments after) and -ArgumentList will only take a collection of objects (no named parameters).

      I’m still working on a decent solution but this really shouldn’t be that hard one would think….

       

    • #274146
      Participant
      Topics: 5
      Replies: 249
      Points: 985
      Helping Hand
      Rank: Major Contributor

      Here is a solution.  Not very pretty and could probably be cleaned up some, but should work.  Leave your script (windows_path_enumerate.ps1)  as is, but call it with this script.  Modify $params for whatever arguments you want to pass.

       

       

    • #274401
      Participant
      Topics: 9
      Replies: 30
      Points: 129
      Rank: Participant

      Great stuff thanks Mike. Really appreciate your persistence. I’ll give this a test and let you know how I get on. I was going to run this via SCCM but my preference is to run it from a server and see the results in real time rather than having to check SCCM client logs etc.

    • #275271
      Participant
      Topics: 9
      Replies: 30
      Points: 129
      Rank: Participant

      I tested this on a single server and although it didn’t error, nothing came back as output. I’m interested to know why you’ve split the construction of the  $ScriptContents variable into three separate lines? What benefit does this provide over simply "$scriptContents = '$params = $using:params; .{Get-Content -Path "C:\Scripts\Windows_Path_Enumerate.ps1" -Raw } @params' ?

      I wouldn’t bust your balls trying to assist with this one as can at least push this out via SCCM but would have been nice to be able to run this and see the results via the local console as and when.

    • #275301
      Participant
      Topics: 5
      Replies: 249
      Points: 985
      Helping Hand
      Rank: Major Contributor

      Just easier to read in my opinion.

    • #275313
      Participant
      Topics: 9
      Replies: 30
      Points: 129
      Rank: Participant

      Ok, thanks for the update. Just wondering if I was missing something and it was required etc. Thanks again.

Viewing 9 reply threads
  • You must be logged in to reply to this topic.