locked
invoke-command with arguments RRS feed

  • Question

  • Hello again :)

    I have created a script that i would like to run remotely. As the script is interactive and that wouldn't work with the invoke-command, i decided to change it so it passes the commands as local parameters. For example, i want to pass a computer argument so in my script which will run remotely i have this:

    param(
        [Parameter(ParameterSetName='computername')]
        [string]$computername
    )


    so if i would run my script locally, i would say script.ps1 -computername test1

    how do i do the same with invoke-command? 

    I tried 

    Invoke-Command -ComputerName computer.domain.local -FilePath .\script.ps1 -computername test1 -Credential domain\user

    but obviously that won't work. i read quite some on the internet but i wasn't able to find that specific that thing.

    any ideas?

    Thanks in advance!

    Thursday, June 29, 2017 7:43 AM

Answers

  • This is how i solved it:

    $ScriptBlockContent =
                                {
                                    $name = $args[0]
                                    c:\full-path\N-V-M-B.ps1 -name $name
                                }
    Invoke-Command -Computer server.domain.local -ScriptBlock $ScriptBlockContent -ArgumentList $name -Credential (Get-Credential)

    • Marked as answer by VasileiosG Friday, June 30, 2017 9:07 AM
    Friday, June 30, 2017 9:07 AM

All replies

  • The "param" statement in a script block does not use Parameter tokens and does not receive parameters by name.

    To learn how to use the command see:

    help Invoke-Command -full

    The examples will show you how to do this.


    \_(ツ)_/

    Thursday, June 29, 2017 7:52 AM
  • thanks. I looked at this and i looked also at the more explanatory https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.core/invoke-command?f=255&MSPPError=-2147217396

    but i am still not getting it because i don't know what i am looking for

    Thursday, June 29, 2017 8:53 AM
  • Hi,

    Try this:

     Invoke-Command -ComputerName  rdweb -FilePath .\sample.ps1 -ArgumentList "<args>" -Credential(get-credential)
    

    Best regards,

    Andy


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

    Friday, June 30, 2017 4:56 AM
  • Do you mean you have a list of computers and you want to use invoke command run your script against the remote computers?

    Check out this link, it might give you some idea. It's about getting local admins on remote pc but you can tweak it and run a script instead of a command line. Get local admins on remote pc

    Something like this:

    $computers = Get-content "D:\computers.txt"
    
    
    ForEach ($Line In $computers)
    
    {
      #write-host $Line
     Invoke-command -ComputerName $line -ScriptBlock { d:\script.ps1}  
    
     }

    If you are the domain administrator (which I hope you are), no need to pass the credential  just run the PowerShell script in an elevated mode with the domain admin account.

    If you're not the domain administrator, please be gentle and ask your domain admin or else he/she might be pissed off if you're running scripts against remote computers.


    Every second counts..make use of it. Disclaimer: This posting is provided AS IS with no warranties or guarantees and confers no rights.

    Friday, June 30, 2017 6:15 AM
  • Hi Andy,

    thanks but that didn't work. This is exactly what i typed:

    Invoke-Command -ComputerName tt-00-main01.tt.local .\N-V-M-B.ps1 -ArgumentList "-vm_n $vm_n" -Credential (Get-Credential)

    did i do that wrong?

    Friday, June 30, 2017 7:29 AM
  • Hi cguan,

    no, that's not what i mean. I have a script which i want to run on a remote server that accept arguments. I want the arguments to passed from the local script to the remote script

    but thanks for the info!

    Friday, June 30, 2017 7:32 AM
  • The file specified must be on the local computer.  It cannot be a remote file.

    If you would actually read the help it would save you a lot of wasted time with guesses and bad advice like adding quotes to arguments.  The examples  are what you need to pay close attention to.


    \_(ツ)_/

    Friday, June 30, 2017 7:35 AM
  • Hi cguan,

    no, that's not what i mean. I have a script which i want to run on a remote server that accept arguments. I want the arguments to passed from the local script to the remote script

    but thanks for the info!

    Passed a variable from local to remote script, well I think it's possible but it would not be an easy way.

    If I were you I would save the value of the local script and let the remote script read that value.

    I guess you need to plan carefully on what your're trying to do.

    And just follow the KISS method, Keep it simple sys-admin.

     

    If you really want to do it the hard way, create a VB.NET DLL that will handle the passing of data between your scripts but I guess you really don't need that.


    Every second counts..make use of it. Disclaimer: This posting is provided AS IS with no warranties or guarantees and confers no rights.

    Friday, June 30, 2017 7:45 AM
  • @jrv

    I think you should change your tone a bit. Sometimes is better to say nothing rather than be mean.

    In addition, you made the assumption that I haven't read the help nor that I didn't know that the file needs to be on the local computer. For your information then, I read the help but that doesn't mean I understood what I read, doesn't it? If I had, I wouldn't ask. You think I have nothing better to do than posting on the internet?

    The file I am trying to run, is located both on my computer and remotely. When I run the command as suggested by Andy Invoke-Command -ComputerName tt-00-main01.tt.local .\N-V-M-B.ps1 -ArgumentList "-vm_n $vm_n" -Credential (Get-Credential), it seems that is working fine, except that it is not passing the variable:

    A positional parameter cannot be found that accepts argument '-vm_n test1'.
        + CategoryInfo          : InvalidArgument: (:) [], ParameterBindingException
        + FullyQualifiedErrorId : PositionalParameterNotFound
        + PSComputerName        : server.domain.local
     


    Friday, June 30, 2017 7:45 AM
  • Abdies directions won't work.  I asked you to read the help 23 hours ago and you ignored the advice.  Yes - that is a requirement for  a tech. This is a professional technical forum.  It is not a pre-school.  You are expected to put in the effort to read the documentation.   Professional techs do not need hand holding or being treated like fragile children.   If you want to learn PowerShell then you need to do some of the work.

    \_(ツ)_/


    • Edited by jrv Friday, June 30, 2017 7:51 AM
    Friday, June 30, 2017 7:47 AM
  • Hi cguan,

    no, that's not what i mean. I have a script which i want to run on a remote server that accept arguments. I want the arguments to passed from the local script to the remote script

    but thanks for the info!

    Passed a variable from local to remote script, well I think it's possible but it would not be an easy way.

    If I were you I would save the value of the local script and let the remote script read that value.

    I guess you need to plan carefully on what your're trying to do.

    And just follow the KISS method, Keep it simple sys-admin.

     

    If you really want to do it the hard way, create a VB.NET DLL that will handle the passing of data between your scripts but I guess you really don't need that.


    Every second counts..make use of it. Disclaimer: This posting is provided AS IS with no warranties or guarantees and confers no rights.


    It is exactly how the command works.  Read the help and you will see how.  We pass arguments to scripts and script blocks all of the time.  It is fundamental to PowerShell.

    \_(ツ)_/

    Friday, June 30, 2017 7:53 AM
  • This is how i solved it:

    $ScriptBlockContent =
                                {
                                    $name = $args[0]
                                    c:\full-path\N-V-M-B.ps1 -name $name
                                }
    Invoke-Command -Computer server.domain.local -ScriptBlock $ScriptBlockContent -ArgumentList $name -Credential (Get-Credential)

    • Marked as answer by VasileiosG Friday, June 30, 2017 9:07 AM
    Friday, June 30, 2017 9:07 AM
  • Hi cguan,

    no, that's not what i mean. I have a script which i want to run on a remote server that accept arguments. I want the arguments to passed from the local script to the remote script

    but thanks for the info!

    Passed a variable from local to remote script, well I think it's possible but it would not be an easy way.

    If I were you I would save the value of the local script and let the remote script read that value.

    I guess you need to plan carefully on what your're trying to do.

    And just follow the KISS method, Keep it simple sys-admin.

     

    If you really want to do it the hard way, create a VB.NET DLL that will handle the passing of data between your scripts but I guess you really don't need that.


    Every second counts..make use of it. Disclaimer: This posting is provided AS IS with no warranties or guarantees and confers no rights.


    It is exactly how the command works.  Read the help and you will see how.  We pass arguments to scripts and script blocks all of the time.  It is fundamental to PowerShell.

    \_(ツ)_/

    Thank you Sir JRV, much appreciated. You're a genius!!!

    Every second counts..make use of it. Disclaimer: This posting is provided AS IS with no warranties or guarantees and confers no rights.

    Friday, June 30, 2017 9:19 AM
  • This is how i solved it:

    $ScriptBlockContent =
                                {
                                    $name = $args[0]
                                    c:\full-path\N-V-M-B.ps1 -name $name
                                }
    Invoke-Command -Computer server.domain.local -ScriptBlock $ScriptBlockContent -ArgumentList $name -Credential (Get-Credential)

    Where is your remote script that you were discussing?


    Every second counts..make use of it. Disclaimer: This posting is provided AS IS with no warranties or guarantees and confers no rights.

    Friday, June 30, 2017 9:21 AM
  • Really not a genius.  I just know that learning the tools in our toolbox is critical to working in modern technology.  This is not new.  I have had many people working for me over the years and it is  requirement, always,  for a tech to, first, familiarize him or herself with the documentation for any component or system.  The best answer we can give is to look at the help and learn the basics.  Learning by asking questions is only useful once you know the basics.

    I read articles in industry business new where managers complain that they must hire H1B visa holders because there are no enough competent techs in the US yet we train more than enough.  Companies sift through hundreds of applications and test the candidates.  In last large company that I worked for tested with an online tool and I interviewed and challenged candidates to prove they could think.  Most candidates could not solve simple problems even when given the book.

    Trade schools and technical colleges teach old technology and do not teach how to think or teach the fundamentals other than a quick overview.  Most techs today do not know how a computer actually works on a technical level. Most newer techs today do not know how to read technical literature and fight to not have to read anything.  They fight to get all of the answers by trolling Internet forums.  They get answers but fail to learn the technology.

    Now most trade schools and tech colleges are teaching programming skills and PowerShell as well as JavaScript because techs need this to advance and to understand the technology.

    Companies are quickly moving t the cloud because it allows them to eliminate the lower level desktop and network techs and hire only degreed systems engineers and IT specialists that can understand and work with cloud based and hybrid systems.  Both require advanced skills with scripting.

    My intent is to badger the lazy ones to try harder because they will eventually discover that they need to learn more.

    I hope I am successful at least some of the time.


    \_(ツ)_/

    Friday, June 30, 2017 9:35 AM
  • This is how i solved it:

    $ScriptBlockContent =
                                {
                                    $name = $args[0]
                                    c:\full-path\N-V-M-B.ps1 -name $name
                                }
    Invoke-Command -Computer server.domain.local -ScriptBlock $ScriptBlockContent -ArgumentList $name -Credential (Get-Credential)

    Where is your remote script that you were discussing?


    Every second counts..make use of it. Disclaimer: This posting is provided AS IS with no warranties or guarantees and confers no rights.

    Arguments can easily passed to a file using positioning.  Once you learn PowerShell you will learn how PowerShell uses parameters and you will then learn what the help is trying to tell you when you pass arguments in an "Invoke".  The arguments are matched to the parameters by position of the parameter as defined by the Param statement.

    The help gives a very detailed discussion of using arguments with a Param statement.  Of course you have to read the help carefully to know this.

    To make help easier to use there are two help parameters.  "-online" and "-ShowWindow". 

    To learn how to use help just type the following:

    help help

    The help about help is extensive.  It is almost as extensive as the Unix "man man" command and, in fact, you can also type that in PowerShell.


    \_(ツ)_/

    Friday, June 30, 2017 9:44 AM
  • Really not a genius.  I just know that learning the tools in our toolbox is critical to working in modern technology.  This is not new.  I have had many people working for me over the years and it is  requirement, always,  for a tech to, first, familiarize him or herself with the documentation for any component or system.  The best answer we can give is to look at the help and learn the basics.  Learning by asking questions is only useful once you know the basics.

    I read articles in industry business new where managers complain that they must hire H1B visa holders because there are no enough competent techs in the US yet we train more than enough.  Companies sift through hundreds of applications and test the candidates.  In last large company that I worked for tested with an online tool and I interviewed and challenged candidates to prove they could think.  Most candidates could not solve simple problems even when given the book.

    Trade schools and technical colleges teach old technology and do not teach how to think or teach the fundamentals other than a quick overview.  Most techs today do not know how a computer actually works on a technical level. Most newer techs today do not know how to read technical literature and fight to not have to read anything.  They fight to get all of the answers by trolling Internet forums.  They get answers but fail to learn the technology.

    Now most trade schools and tech colleges are teaching programming skills and PowerShell as well as JavaScript because techs need this to advance and to understand the technology.

    Companies are quickly moving t the cloud because it allows them to eliminate the lower level desktop and network techs and hire only degreed systems engineers and IT specialists that can understand and work with cloud based and hybrid systems.  Both require advanced skills with scripting.

    My intent is to badger the lazy ones to try harder because they will eventually discover that they need to learn more.

    I hope I am successful at least some of the time.


    \_(ツ)_/

    I understand where you're coming, fundamentals is important on all aspects I think in every industry especially in IT.

    But sometimes in some companies where some managers who are not IT trained, what they want is a quick output. They don't wanna know how you do it, on how's it's gonna be done. They just want to see the final result.

    And some small companies, they could not afford a guy just for network, a guy just for scripting, a guy just for user support, a guy to administer windows and linux server. They want to save some money hire someone and expect to do all things, a one man army. 

    If the guy who will do it, will start from scratch and learn all the fundamentals: of network, scripting, active directory, Windows and Linux server fundamentals then I think all the black, and white hair will be a pure scalp at the end.


    Every second counts..make use of it. Disclaimer: This posting is provided AS IS with no warranties or guarantees and confers no rights.

    Friday, June 30, 2017 10:04 AM
  • I understand where you're coming, fundamentals is important on all aspects I think in every industry especially in IT.

    But sometimes in some companies where some managers who are not IT trained, what they want is a quick output. They don't wanna know how you do it, on how's it's gonna be done. They just want to see the final result.

    And some small companies, they could not afford a guy just for network, a guy just for scripting, a guy just for user support, a guy to administer windows and linux server. They want to save some money hire someone and expect to do all things, a one man army. 

    If the guy who will do it, will start from scratch and learn all the fundamentals: of network, scripting, active directory, Windows and Linux server fundamentals then I think all the black, and white hair will be a pure scalp at the end.


    In all cases this is no excuse.  A company hires a competent tech and ask them to do everything.  Learning how to learn allows us to match the pace.  I hired people because they proved a certain level of competence and could prove they could acquire new skills at a reasonable pace.  For very large tools and projects we would send techs to a school but the schools require independent work and an advanced skill with reading.  I only sked that techs could read and understand the documentation and knew how to find answers.  Collaboration was always encouraged.

    Once a tech settled in and we were sure they were what they sold themselves as I would fight to keep them up to date and work with them through any issues.   It is only the bassline that counts.  After that it is all about people and work ethic.

    Self teaching is one of the most important aspects of modern IT and technology.  Any manager who does not promote and allow time for techs to learn the newest systems is doing everyone a disservice.  A good tech is as valuable as a bad tech is a waste of time and money.  Bad techs are visible very quickly.  Good techs are easy to spot but hard to hire since they are in demand.

    Smaller companies end up with the cast offs although smaller companies are a very good place to learn if you spend time studying the technology and mastering what the company has.

    I was sent two a two week class at the old Digital Equipment Corporation (DEC) and studied the new VMS 5.x internals and API.  That is like learning the NT kernel in two weeks.   It can be done.  (VMS 5 is the model that NT 3.0 was based on.)  Since then I have trained in multiple OS internals including the NT and Win32 API and continue to study each new release as much as I can find time for.  I never did any of this to be able to build an OS but so I could understand better how to design systems and how to troubleshoot complex network and database issues.  This is the kind of direction that most good tech take although today the mix of studies s much different for those new to Windows and IT.

    There is no reason for a tech with an itchy manager to not spend the time learning the latest tools. Managers expect this of a professional.  No one should tell a tech to learn PowerShell today.  All techs must now become proficient in PowerShell. It is the baseline management tool for almost everything including devices and phones even on xNix platforms.  PowerShell is even used on the Mac now.

    The only way to become proficient in PowerShell is to sit down and study it until it makes complete sense.  It cannot be learned by osmosis or guesswork.  It cannot be learned by asking questions.  Questions are only useful once you know the basics well.


    \_(ツ)_/

    Friday, June 30, 2017 10:27 AM
  • In all cases this is no excuse.  A company hires a competent tech and ask them to do everything.  

    Wow, quite demanding. Ask them to do everything, I guess I couldn't agree more with you. That's the reality now, companies are asking Tech to do everything. A jack of all trades, an IT guy who's like a framework that is pluggable able to do all types of modules. 

    Even up to the point some companies ask to do job that is beyond someone's payroll grade.

    Anyway, work is work. Life is life, a limited edition module. 


    Every second counts..make use of it. Disclaimer: This posting is provided AS IS with no warranties or guarantees and confers no rights.


    • Edited by cguan Friday, June 30, 2017 10:39 AM edit
    Friday, June 30, 2017 10:38 AM
  • A competent tech can just change jobs.  A poor tech is usually stuck being a go-for.  Another reason to study and advance.  I recommend certification in Windows to as high a level as you can get.  Online training makes this easier then when I first did it. It is well worth the time and access to a much higher salary makes it worth the investment.


    \_(ツ)_/

    Friday, June 30, 2017 10:43 AM
  • I have it at the remote server on a specific path
    Friday, June 30, 2017 11:39 AM
  • I think you are looking for robots, not people. Yes i agree with you that people should learn, advance etc but obviously there is a limit. And i don't know you, i don't know what you do but from your attitude and your own words i can deduct that you are in a position of power since you are able to judge and say i hire you and i don't hire him. Sometimes when people reach that level, forget where they came from. They also forget that it's not only one thing you have to learn, powershell and that's it. I do way more stuff than just powershell, in which i agree i am a noob. I do networking from several different vendors, firewalls from several different vendors. I do support Windows, Linux, MacOS. I do security, I do ISO 27001. I do project management. And the list goes on and on. I also live in not my country (Greece) but in The Netherlands. I learn the local language. I am married with a woman who is not from my country or from the Netherlands, but from a 3rd country. I have a child and I have other responsibilities. I have also my morning job and my evening personal business. 

    Many things to learn and that's how life is. I am not saying all these for you to feel sorry for me, but rather for you to stop acting like an as*h*le. If you like to show off of how big @@ you have and how great you are, then I feel very sorry for you because obviously you have issues that you need to resolve with yourself and those who have depressed you in your life.

    This is a forum for techs indeed so lets concentrate on helping each other and if you need psychological support, feel free to send me a private message. I do that too for others.

    Friday, June 30, 2017 11:54 AM
  • I recommend that you learn to read documentation to save your self a lot of useless wasted time.

    Your personal and childish use of obscenities says it all.  Stubbornly refusing to read the documentation is also very counterproductive and un-professional.


    \_(ツ)_/

    Friday, June 30, 2017 8:11 PM
  • @jrv--please get off the "read the documentation" kick. The OP already stated he read it. That doesn't mean the doc is well-written, or that the examples jump out at him to explain the concept well. If you are a teacher, you need to have compassion for your learners and tact in communicating with them. We all started somewhere, and we all needed help growing into what we do. Let's also be amazed that the OP @VasileiosG has grown to cope with many things--including two more languages, new culture and new technology. Let's all be humble and recognize the good in others. We can help them. Eventually, they will get it and help still others. Life is too short for negativity.
    Tuesday, January 15, 2019 6:58 PM
  • ...and in the same breath, thank you @jrv for mentioning the Help Invoke-Command (e.g.). That is something I am going to explore now. :-)
    Tuesday, January 15, 2019 6:59 PM
  • ...and in the same breath, thank you @jrv for mentioning the Help Invoke-Command (e.g.). That is something I am going to explore now. :-)

    I reread documentation repeatedly because new issues uncover new implications of what the documentation says. It is normal and critical that all techs learn to review old things as new discoveries are nearly always found.

    Techs new to PS read the docs but lack sufficient experience to catch the subtle implications of the content.

    Yes some docs have issues but most (98%) are correct and useful.  The online versions are currently being updated and corrected continuously. There is a project that is permanently deployed to maintain the PS docs.

    It is also necessary to "update" the local versions to have the latest versions.

    Also, from the OPs questions it was clear that the docs had NOT been read carefully.  Skimming tech docs is not the same as "reading".


    \_(ツ)_/

    Tuesday, January 15, 2019 7:44 PM