locked
Unable to run a .cmd with script block RRS feed

  • Question

  • Hi I am trying download a file to a server then run that file using the following script

    $creds = Get-Credential -Credential "mycreds"
    $computername = Get-Content "C:\scripts\CCS\servers.txt"
    $sourcefile0 = "\\servername\ccs\ccs11.5.2-windows-agent.zip"
    $counter = 0
    $counter1 = 0

    foreach ($computer in $computername)
        { 
        $counter++
        Write-Progress -Activity "Processing Servers" -CurrentOperation $computer -PercentComplete (($counter / $computername.count) * 100)
        Start-Sleep -Milliseconds 200
        $destinationFolder = "\\$computer\C$\temp\"
       
        if (!(Test-Path -path $destinationFolder))
        {
           New-Item $destinationFolder -Type Directory
        }
        Copy-Item -Path $sourcefile0 -Destination $destinationFolder
        }
    foreach ($computer in $computername)
        {
        $counter1++
        Write-Progress -Activity "Unzipping File" -CurrentOperation $computer -PercentComplete (($counter1 / $computername.count) * 100)
        Start-Sleep -Milliseconds 200
        Invoke-Command -Computername $computer -Authentication Default -Credential $creds -ScriptBlock {Expand-Archive -LiteralPath C:\temp\ccs11.5.2-windows-agent.zip -DestinationPath c:\temp}
        Invoke-Command -ComputerName $computer -Authentication Default -Credential $creds  -ScriptBlock {Start-Process SilInstCCS110v3.5.cmd -ArgumentList argument1 -Verb runAs} 
        }

    EVERYTHING with the exception of the actually running of the command is working. I am able to download the file to all the servers and subsequently unzip it however when this command is ran 

     Invoke-Command -ComputerName $computer -Authentication Default -Credential $creds  -ScriptBlock {Start-Process SilInstCCS110v3.5.cmd -ArgumentList argument1 -Verb runAs} 

    I dont see any failures and it just closes.  Any help is appreciated.


    • Edited by Datatic Thursday, June 6, 2019 11:27 PM security
    Thursday, June 6, 2019 11:15 PM

Answers

  • You need to start simple and learn how the commands work.  Avoid any fancy constructs and additions that will only confuse you.

    Here is a starter.

    $creds = Get-Credential -Credential mycreds
    $sourcefile0 = '\\servername\ccs\ccs11.5.2-windows-agent.zip'
    $computername = Get-Content 'C:\scripts\CCS\servers.txt' foreach($computer in $computername){ Copy-Item -Path $sourcefile0 -Destination "\\$computer\C$\temp" Invoke-Command -Computername $computer -Authentication Default -Credential $creds -ScriptBlock { Expand-Archive 'C:\temp\ccs11.5.2-windows-agent.zip' -DestinationPath c:\temp Start-Process SilInstCCS110v3.5.cmd -ArgumentList 11.5.1.6 } }


    \_(ツ)_/



    • Edited by jrv Thursday, June 6, 2019 11:31 PM
    • Marked as answer by Datatic Friday, June 7, 2019 3:44 AM
    Thursday, June 6, 2019 11:29 PM

All replies

  • You are not trying to "download".  You are "uploading" files.  Which is it that you are trying to do?

    \_(ツ)_/

    Thursday, June 6, 2019 11:25 PM
  • I should probably mention when I try to run the command 

    "Start-Process SilInstCCS110v3.5.cmd -ArgumentList argument -Verb runAs"

    On a local server it will launch cmd prompt I will see it "try" to start the install then CMD will close and nothing is installed. 

    • Edited by Datatic Thursday, June 6, 2019 11:27 PM security
    Thursday, June 6, 2019 11:26 PM
  • You need to start simple and learn how the commands work.  Avoid any fancy constructs and additions that will only confuse you.

    Here is a starter.

    $creds = Get-Credential -Credential mycreds
    $sourcefile0 = '\\servername\ccs\ccs11.5.2-windows-agent.zip'
    $computername = Get-Content 'C:\scripts\CCS\servers.txt' foreach($computer in $computername){ Copy-Item -Path $sourcefile0 -Destination "\\$computer\C$\temp" Invoke-Command -Computername $computer -Authentication Default -Credential $creds -ScriptBlock { Expand-Archive 'C:\temp\ccs11.5.2-windows-agent.zip' -DestinationPath c:\temp Start-Process SilInstCCS110v3.5.cmd -ArgumentList 11.5.1.6 } }


    \_(ツ)_/



    • Edited by jrv Thursday, June 6, 2019 11:31 PM
    • Marked as answer by Datatic Friday, June 7, 2019 3:44 AM
    Thursday, June 6, 2019 11:29 PM
  • I should probably mention when I try to run the command 

    "Start-Process SilInstCCS110v3.5.cmd -ArgumentList argument -Verb runAs"

    On a local server it will launch cmd prompt I will see it "try" to start the install then CMD will close and nothing is installed. 

    We cannot help you with external third party batch file.  Contact the vendor or creator of the file and find out why it doesn't work.

    You cannot use "RunAs" in a remote session.


    \_(ツ)_/

    Thursday, June 6, 2019 11:35 PM
  • apologies...I was more concerned with the script than grammar.. &_&
    Friday, June 7, 2019 3:20 AM
  • sorry whats fancy?  I am learning scripting yes but am I supposed to somehow stay in my lane?  How do I learn if I don't reach beyond my grasp? I am after all asking for help a little less condescension would be appreciated.

    In any case I appreciate the response... I was unaware that I didn't have to repeat the Invoke-Command

    Additionally I had assumed that since I was using the invoke-Command it was running the commands on the remote server as if it was a local session.  SO my thought was I needed to force the local session to run as administrator.  Is that not the case?  Or is it redundant since I already used my admin credentials to authenticate?

    Either way the same result is happening, even with the streamlined changes you've suggested.  I really do appreciate pertinent feedback.  I think I'll look into the vendors cmd as you previously suggested.

    Friday, June 7, 2019 3:42 AM
  • What is the "same result"?  Please take some time to ask with all useful information.

    What is the error condition of the remote command?

    When remoting you are NOT running locally.  Only local processes can use "RunAs". 

    What is the result you are expecting?  The commands do not specify any output.

    The code specifies what looks like n IP address.  You cannot run any commands that attach to a third computer.


    \_(ツ)_/

    Friday, June 7, 2019 4:25 AM
  • apologies...I was more concerned with the script than grammar.. &_&

    What grammar? "download" and "upload" have nothing to do with grammar. They are fundamentally different actions,

    Would "I am going to throw a ball" and "I am going to eat a ball" to be only grammar differences?    No.  They comp[lately change the action and therefore mean to totally different things.


    \_(ツ)_/

    Friday, June 7, 2019 4:31 AM