none
How to create a script from command line commands.

    Question

  • I have these commands that work fine from the command line.

    I need to put these into a .ps1 script that will be located remotely.

    The local server that will call the remote script and has to pass a variable to the remote script. Multiple subdirectories are in the path, source contains many files in sub-directories that are not wanted on destination. The commands are abrivated here but the actual commands do work when ran from the PowerShell command line.

    Example local Variable = $test represents a new folder to be created selected locally

    as in $test might = 5.0 or 6.0 or 5.2.3 etc

    commands that need to be executed :

    #1 PS  $source = "D:\Bfolder\Leadfolder\data\programs\scratch\file.txt"; 
          $destination = Server_1\IT\Bfolder\LleadFolder\$test\scratch\file.txt                       #2 PS New-Item -ItemType File -Path $destination -Force
    #3 PS Copy-item $source $destination -Recurse

    these three commands will be reteated many times within the script but with a different PATH to $test and a specific file to be copied for each in command #1

    So what might the script.ps1 file look like and when running remotely how do I pass the variable?

    I tried this with no success:

    ${vars.getValue("powershell")} Invoke-Command -ComputerName Server_1 -scriptblock {param(${vars.getValue("destdir")})} & 'D:\PSscripts\script.ps1' ${vars.getValue("destdir")}

    where ${vars.getValue("powershell")} = path to powershell command (local variable format)

             ${vars.getValue("destdir")} = $test in example

    and the  script.ps1 file contain the three PS commands above.

    • Moved by Bill_Stewart Thursday, January 02, 2014 7:37 PM Abandoned
    Wednesday, November 06, 2013 10:04 PM

All replies

  • You do not need a script to do that.  Just type the command at a command prompt.  Putting a single command in  a file serves no purpose.

    If you are trying to ask how to execute a command on a remote server then you need to have remoteing installed and enabled.

    Look into how to do remoting in PowerShell.

    From the lines you have posted I can recommend that you attempt to learn the basics of PowerShell.

    TO remote commands you would do:

    Invoke-Command -Scriptblock { ... commands ...} -computer remotepc

    Type:

    HELP Invoke-Command -FULL


    ¯\_(ツ)_/¯

    Wednesday, November 06, 2013 10:27 PM
  • This won't be run from the command line the script.ps1 will be called from another application that will pass the variable to the script.

    thanks anyway the response was not very useful I have a copy of the PowerShell book I just was looking for some advice on how to have those commands inside a script.

    Wednesday, November 06, 2013 10:49 PM
  • It would have been help fulhad you mentioned that you wanted to call a script fromanother program.

    You can buid a PowerShell host  usimg the PowerSHell SDK.  this canbe sued to mke direc calls to the PowerSHell hosting process.

    Is it possible that you are really trying to ask "How do I pass variables from the command line into a script file?"

    At a command prompt type "powershell -?"  read the results carefully.  It will show you all of the options for strating a powershell session with commands and with arguments.

    If the file is stored on a remote share you will need to sign the script for security porposzes or, for two cammands, just call the commands directly.

    Here is the call:

    $myscript.ps1
    Param(
         $source
         $destination
    )
    
    New-Item -ItemType Directory -Path $destination -Force
    Copy-item $source $destination -Recurse
    

    I changed the New-Item to be "Directory" because you cannot recurse a file.

    This is called like this:

    powershell -file myfile.ps1 -source D:\Bfolder\Leadfolder\data\programs\scratch\file.txt -destination  Server_1\IT\Bfolder\LleadFolder\$test\scratch\file.txt 

    We just pass arguments on the commandline by name.  This is fundamental PowerShell usage.

    Of course you description of what you want to do is a bit vague so I still may not be understanding what you are asking.


    ¯\_(ツ)_/¯

    Wednesday, November 06, 2013 11:50 PM
  • It could be that you e just trying to copy a file.  File copies are just like any old DOS or CMD.EXE copy command

    copy <source file> <destination file>

    There is no need to create the target file first.  Just copy the file.

    This can be done very easily with this command:

    powershell -command Copy-Item D:\Bfolder\Leadfolder\data\programs\scratch\file.txt \\Server_1\IT\Bfolder\LleadFolder\$test\scratch

    You do not need to specify the destination file name in any Windows utility except if you wish to change the name.


    ¯\_(ツ)_/¯

    Wednesday, November 06, 2013 11:58 PM
  • You also need to know that you cannot pass native variables from one program to another in Windows through a command line. You can send strings or the string representations of a variables contents through a command line or a "shell" call'

    You cannot make shell execute calls directly to PowerShell scripts.  They do not auto-open.  That has been disabled in PowerShell for security reasons.

    If none of the above answers you question you will need to give a more detailed description. What program and how does the program activate external programs.


    ¯\_(ツ)_/¯

    Thursday, November 07, 2013 12:05 AM
  • ok let me try again.

    From Powershell I run the following commands and they do exactly what I want them to do. I need to do it this way as I need to create all the directories in the destination path but only add a file to the last directory. The directories do not exist on the destination. I do not want to copy any other files to the destination that may exist in the source PATH. I can invoke Powershell from QuickBuild (like invoking from the DOS cmd line I suppose).

    So this works. note the "test" directory show here will be a variable from QB. It is hard coded here to show what works from powershell.

    PS > $source = "D:\Build\Cdir\LeadPlus\Builds\trunk\Continuous\LPdir\scratch\file.jar
        $destination = "Server_1\it_cm\LPdir\scratch\fileRef\test\scratch\file.jar"
    PS > New-Item -ItemType File -Path $destination -Force
    PS > Copy-item $source $destination -Recurse

    I have 10 sets of these three commands to run each with different non existing destination directories and different files to be copied into the last directory in each path.


    • Edited by philpinkerton Thursday, November 07, 2013 12:34 AM changed DOC to DOS
    Thursday, November 07, 2013 12:33 AM
  • The $source and $destination values are files.  They are not directories. 

    The destination path is a relative path. It is relative to the home of PowerShell.

    Maybe you are sayng that this is a file?

    "Server_1\it_cm\LPdir\scratch\fileRef\test\scratch\file.jar"

    Why are you saying you are creating 10 directories?  It appears you are creating files.

    Your New-Item explicitly creates a file.  You then copy this:

    $source = "D:\Build\Cdir\LeadPlus\Builds\trunk\Continuous\LPdir\scratch\file.jar

    over the top of the file. Is that really what you are trying to do. I posted the info on how to call a script with arguments.  That is how you would do it.

      


    ¯\_(ツ)_/¯

    Thursday, November 07, 2013 1:04 AM