Send Variable to 2nd script an Start the second Script

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 5 months ago.

  • Author
    Posts
  • #5620

    by AndreSch at 2013-03-04 01:39:11

    Hi Guys

    in the last weeks i wrote some small Powershellscripts. Now i have a small Problem or better i need a small kick in the right direction. 😉

    In my case i have a script called "first.ps1"

    $test = "Hello World"

    Now i want to trigger from the first script a second script with the folowing code.It named "second.ps1"

    Write-Host $test

    I´ve tested a little bitte with "invoke-expression and invoke-command" but i don´t find the right way.

    Can anyone help me and is it a large amount of code or only 2 lines are needed to get the scripts

    Thank you for help.

    AndreSch

    Sorry for my bad English but i hope you´re understanding my problem 🙂

    by DexterPOSH at 2013-03-04 02:25:52

    I am just copying the below from the help for Invoke-Command.
    You can use the logic shown
    Hope this helps

    -------------------------- EXAMPLE 9 --------------------------

    C:\PS>$MWFO-LOg = Microsoft-Windows-Forwarding/Operational

    C:\PS> invoke-command -computername server01 -scriptblock {param($log, $num) get-eventlog -logname $log -newest $num} -ArgumentList $MWFO-log, 10

    Description
    -----------
    This example shows how to include the values of local variables in a command run on a remote computer.

    The first command saves the name of the Microsoft-Windows-Forwarding/Operational event log in the $MWFO-Log variabl
    e.

    The second command uses the Invoke-Command cmdlet to run a Get-EventLog command on the Server01 remote computer tha
    t gets the 10 newest events from the Microsoft-Windows-Forwarding/Operational event log on Server01.

    This command uses the "param" keyword to create two variables, $log and $num, that are used as placeholders in the
    Get-EventLog command. These placeholders have arbitrary names that do not need to match the names of the local vari
    ables that supply their values.

    The values of the ArgumentList parameter demonstrate the two different ways to specify values in the argument list.
    The value of the $log placeholder is the $MFWO-Log variable, which is defined in the first command. The value of t
    he $num variable is 10.

    Before the command is sent to the remote computer, the variables are replaced with the specified values.

    by AndreSch at 2013-03-04 02:59:38

    Hello,

    thanks for posting. I´ve tested it but there is no way for me to call the second script. I´ve tried it but i thing the "script block" contains only specified values. But in my case i would only call a second script with the values from the first Script.
    Somthing like this and not like in the thread opener posted.


    $var = Read-host "Please Enter you Serial Number:"
    Invoke-Command -Computer localhost {c:\scripts\enteredkey.ps1 -$var}

    In this case the Script enteredkey.ps1 will start with the "$var" variable like this.

    Write-Host "you´ve entered the following key: " $var

    I think it´s simple but i´dont know how to run the second script from the first script.
    I´ve wrote some script to get list items from a sharepoint list an write them into an Excelsheet in predifined fields but this ist , i think, a simple script. But how we say in our Country: Oh dear. I´dont see the forrest because there are so many tree´s 😉

    by DexterPOSH at 2013-03-04 03:12:14

    Hi ,
    I tried this and the following works for me, I have two files 1.ps1 and 2.ps1

    Content of 1.ps1
    $var = Read-host "Please Enter you Serial Number:"
    Invoke-Command -scriptblock {param($var) c:\temp\2.ps1 } -argumentlist $var

    content of 2.ps1
    write-host " You have entered $var"

    So when I do the following on the console
    PS C:\TEMP> .\1.ps1
    Please Enter you Serial Number:: 12345
    You have entered 12345
    PS C:\TEMP>

    Hope this helps

    by AndreSch at 2013-03-04 05:00:03

    Hi DexterPOSH,

    Thanks a lot. It works fine. Many Thanks.
    But i´ve got another Question.
    The first Scripts ends only when the second script is finished. So there are dependencies between the both scripts.
    Is it possible to end the first script and if the first is finished the second script run with the Variable from the first.
    In my case i would need the first script as a trigger for the second.
    Here is my little workflow. 😉

    1. Start the first Script
    2. Send varibale to second Script
    3. End First Script
    4. Start second Script

    Greetz

    AndreSch

    by DexterPOSH at 2013-03-05 08:56:18

    Hi Andersch,

    One way I can think of doing this is :

    1. Start the first Script
    2. Send the variable using Invoke-Command to the second script and use -AsJob parameter to start it as a job.
    3. Suspend the above job
    4. Resume with the first script and before exiting the script. Resume the above suspended job

    Does this makes sense ?

    ~Regards~
    Dexter

    by AndreSch at 2013-03-13 01:03:47

    Hi DexterPOSH

    thx for the answer. I will test in an send you a Feedback.
    But atm there is no time for me. SO full of work 😉

    Greetz AndreSch

You must be logged in to reply to this topic.