Executing CMD file remotely

Welcome Forums General PowerShell Q&A Executing CMD file remotely

Viewing 3 reply threads
  • Author
    Posts
    • #200246
      Participant
      Topics: 1
      Replies: 1
      Points: 34
      Rank: Member

      I am trying to find a way to execute a command, located on a network share, from a remote workstation.

      The scenario; I have a new VM server we will call newserverone.  If I log into newserverone, I can execute the cmd file located on a network share //storage/installs/monitor.cmd by simply opening up explorer and typing in //storage/installs/monitor.cmd.  What I would like to do is write a script that I could run on my workstation that would execute this in the same manner as it does via the gui.  I ran into similar problem with trying to run a msi file but I found a workaround for it.  (see below) The problem is I cannot copy everything from the network share for the monitor.cmd install.

      For the msi install;
      
      Copy-Item -Path "\\storage\AntiVirus\WebRoot-Installers\Windows\Webroot.msi" -Destination \\newserverone\c$\installs
      Invoke-Command -ComputerName newserverone -Credential domain\adminuser -ScriptBlock { Msiexec /i \\newserverone\C$\installs\Webroot.msi /log C:\MSIInstall.log }
      Start-Sleep -Seconds 33
      Remove-Item -Path \\newserverone\C$\installs\Webroot.msi
      

      If someone also has a better way to deal with this msi install, I would love to hear about that as well.  Any help is greatly appreciated.  Thank you in advance.

       

    • #200261
      Participant
      Topics: 11
      Replies: 1445
      Points: 1,784
      Helping Hand
      Rank: Community Hero

      You are invoking the command on the other system, so write the code as if you were sitting at the other system. You should be able to run what is in the scriptblock and install the software:

      
      $sb = {
          
          #Copy the file locally
          Copy-Item -Path "\\storage\AntiVirus\WebRoot-Installers\Windows\Webroot.msi" -Destination 'C:\Installs\AntiVirus'
      
          #Create a logfile path
          $logFile = 'C:\Installs\AntiVirus\webroot-{0}.log' -f (Get-Date -Format yyyyMMddTHHmmss)
          $MSIArguments = @(
              "/i"
              'C:\Installs\Antivirus\Webroot.msi'
              "/qn"
              "/norestart"
              "/L*v"
              $logFile
          )
      
          #Start the installer and wait until complete
          Start-Process -FilePath "msiexec.exe" -ArgumentList $MSIArguments -Wait -NoNewWindow 
      
          #Remove the directory
          Remove-Item -Path 'C:\Installs\AntiVirus' -Force
      
      }
      
      Invoke-Command -ComputerName newserverone -Credential domain\adminuser -ScriptBlock $sb
      
      

      These commands should be wrapped with try\catch to ensure step 1 completes before the step 2 and so on and so forth, but the above code should be close. There are many examples of installing software and MSI’s with Powershell that you can reference.

    • #200264
      Participant
      Topics: 1
      Replies: 1
      Points: 34
      Rank: Member

      Thank you for the response.  I need to clarify one thing about my question.  While I was able to make the msi install work properly by copying it to the new server but  I cannot get the “.cmd” installer to work properly.  It would be even better if I could install  from a network share instead of copying the entire software down to the server.  Unless that is my only option.

       

    • #200282
      Participant
      Topics: 11
      Replies: 1445
      Points: 1,784
      Helping Hand
      Rank: Community Hero
Viewing 3 reply threads
  • You must be logged in to reply to this topic.