Install Updates/Upgrades using PowerShell

Welcome Forums General PowerShell Q&A Install Updates/Upgrades using PowerShell

Viewing 8 reply threads
  • Author
    Posts
    • #277302
      Participant
      Topics: 8
      Replies: 23
      Points: 104
      Rank: Participant

      Hi All,

      I am new to powershell but learning more everyday it seem.  I have been given a task to figure out how to perform software updates/upgrades using Powershell.

      I have come up with a script but it is not working properly and need some help figuring out what is wrong with the script so I can get it to work or coming up with a script that will work.

      Below is the script I am currently trying…
      —————————————————————————————

      “””Welcome to Orlando’s application deployment interface. Please select from the folowing

      applications to deploy or uninstall.

      1. VLC Media Player 3.0.11
      2. Any App 2
      3. More coming soon
      4. ….
      “””

      $selection = Read-Host -Prompt “Please enter your selection: ”
      $targetComputer = Read-Host -Prompt “Enter target computer: ”
      $destination = “\\$targetComputer\c$\temp\”

      if(!(Test-Path -Path $destination )){
      New-Item -ItemType directory -Path $destination
      }

      if ($selection = 1) {

      $alphaSelection = Read-Host -Prompt “Press I to install or X to uninstall”
      $file = ‘\\server\filer7\Software\VLC Media Player\VLC 3.0.11\Install.vbs’
      }

      Copy-Item -Path $file -Destination “\\$targetComputer\c$\temp\Install.vbs”

      #Wait-Event -Timeout 30

      if ($alphaSelection = “I”){

      Invoke-Command -ComputerName $targetComputer -ScriptBlock {cscript.exe c:\temp\Install.vbs $Using:LocalPath}
      }

      else {
      Invoke-Command -ComputerName $targetComputer -ScriptBlock {cscript.exe c:\temp\Install.vbs $Using:LocalPath}

      }

      <hr />

      When I run it and input the selections required the files get copied to the target computer as expected but the vbs scripts never run or I am getting the following error…
      ==========================================================

      PS C:\> D:\Installmsi.ps1
      “Welcome to Orlando’s application deployment interface. Please select from the folowing
      applications to deploy or uninstall.

      1. VLC Media Player 3.0.11
      2. Any App 2
      3. More coming soon
      4. ….

      Please enter your selection: : 1
      Enter target computer: : amhraowintest16

      Directory: \\amhraowintest16\c$

      Mode LastWriteTime Length Name
      —- ————- —— —-
      d—– 12/7/2020 10:52 AM temp
      Press I to install or X to uninstall: I
      Invoke-Command : The value of the using variable ‘$using:LocalPath’ cannot be retrieved because it has not been set in the
      local session.
      At D:\Installmsi.ps1:32 char:5
      + Invoke-Command -ComputerName $targetComputer -ScriptBlock {cscrip …
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : InvalidOperation: (:) [Invoke-Command], RuntimeException
      + FullyQualifiedErrorId : UsingVariableIsUndefined,Microsoft.PowerShell.Commands.InvokeCommandCommand

      ==============================================================

       

       

    • #277305
      Participant
      Topics: 5
      Replies: 252
      Points: 998
      Helping Hand
      Rank: Major Contributor

      The error message is telling you the $localpath is empty.  Looking at your code, you never set that variable to anything.  What is it supposed to be?

      • #277386
        Participant
        Topics: 8
        Replies: 23
        Points: 104
        Rank: Participant

        Hi Mike,

        I think it might help if I explain what I am trying to accomplish before I answer that question.

        The goal here is to use an installer (install.vbs) to install an application from a network share on a remote computer or remote computers.

        So I believe the localPath should be the location that the content (e.g. install.vbs was copied to on the target machine).

        I am running the script from my workstation to go out to a network share copy the content to a target machine (amhraowintest16). and then run the vbs script on that target machine.

    • #277308
      Participant
      Topics: 0
      Replies: 9
      Points: 55
      Helping Hand
      Rank: Member

      This:

      Invoke-Command : The value of the using variable ‘$using:LocalPath’ cannot be retrieved because it has not been set in the
      local session.

      This means, that the variable Localpath is not set.

      Invoke-Command sends commands to a Remote Computer. You want give the remote computer a variable called Localpath.

      But in the local computer (where you are starting the Script) is the variable localpath not known. So it can’t be given to the remote computer.

       

    • #277536
      Participant
      Topics: 5
      Replies: 252
      Points: 998
      Helping Hand
      Rank: Major Contributor

      Hi Mike,

      I think it might help if I explain what I am trying to accomplish before I answer that question.

      The goal here is to use an installer (install.vbs) to install an application from a network share on a remote computer or remote computers.

      So I believe the localPath should be the location that the content (e.g. install.vbs was copied to on the target machine).

      I am running the script from my workstation to go out to a network share copy the content to a target machine (amhraowintest16). and then run the vbs script on that target machine.

      The only file I see copied to the machine is install.vbs.  What other files are there?  My guess is it needs to point to some share location, but need to understand what argument Install.vbs is looking for.  Regardless you need to assign that location to LocalPath in order for it to work.  Also, the if and else statements are doing the exact same thing.  Not sure that is what you want right?

       

      • #277551
        Participant
        Topics: 8
        Replies: 23
        Points: 104
        Rank: Participant

        Mike,

        there are 5 other files that get copied to the target machine’s temp directory.

        I guess I would like to start with how exactly do I set the localpath?

        As for the if else I am not sure what they are doing 100% so if you have suggestions to make the code more efficient I am all ears.

        Thanks for the feedback.

        FYI… just to clear this up a bit I think I might have put the wrong code in original post.  This is the actual code I am using in its entirety.

        $VNC = ‘\\Servernamegoeshere\Filer7\Software\VLC Media Player\VLC 3.0.11\Install.vbs’
        $Computer = ‘Servername’
        $TMP = “\\$Computer\c$\TEMP”

        ## $Install = “\\$Computer\c$\TEMP\VLC 3.0.11\vlc-3.0.11-win64.exe /S”

        if (!(Test-Path $TMP)) {
        New-Item -Path $TMP -ItemType Directory
        }

        Copy-Item -LiteralPath (Split-Path $VNC -Parent) -Destination $TMP -Container -Recurse -Force -Verbose
        $LocalPath = Join-Path ‘$TMP\VLC3.0.11’ (Join-Path (Split-Path $VNC -Parent | Split-Path -Leaf) (Split-Path $VNC -Leaf))
        Invoke-Command -ScriptBlock {cscript.exe Install.vbs $Using:LocalPath} -Computer $Computer

        # Restart might be needed of remote host

         

        Below is the error after the files are copied to the remote computer…

        Microsoft (R) Windows Script Host Version 5.812
        Copyright (C) Microsoft Corporation. All rights reserved.

        Input Error: Can not find script file “C:\Users\myuserprofile\Documents\Install.vbs”.

         

        Based on the error it looks like to me that it is trying to find the Install.vbs on my local machine under my userprofile\documents directory.

        My question is where in the script does it tell it to launch the VBS script from my local machine and how can I tell it to launch it from the remote computer and correct path?

        • This reply was modified 1 month, 1 week ago by othompson09.
    • #277563
      Participant
      Topics: 5
      Replies: 252
      Points: 998
      Helping Hand
      Rank: Major Contributor

      Did you write this code?  The script block for your if statement is the exact same as the script block for your else statement.  I think you are missing lots of basic coding issues.  First, “=” is an assignment operator.  I think your if statement is testing for equality not assigning a value.  The comparison operator you need is “-eq”  Here are a couple resources I’d recommend to get started.  I’ll link to the online version, but all can be accessed via Get-Help.  If you use Get-Help recommend using the -showwindow switch so you get all the documentation in a seperate/searchable window.

      about_if

      about_comparison_operators

      about_assignment_operators (this will show you how to assign a value to a variable i.e. $localpath = “C:\Temp”

      For general PowerShell knowledge the book “Learn PowerShell in Month of Lunches” is kind of the gold standard.

      • #277695
        Participant
        Topics: 8
        Replies: 23
        Points: 104
        Rank: Participant

        Mike to answer your initial question, no I did not write the code I barrowed it and made modification where I thought I needed to for it to do what I wanted.

        Thanks for the links to resources this is what I truly need to learn more about PowerShell and to start getting to know it much better.  I only know enough to be dangerous at this point but am eager to learn once pointed in the right direction.

         

        My understanding of the If statement below

        if (!(Test-Path $TMP)) {
        New-Item -Path $TMP -ItemType Directory
        }

        was that we would be testing for the path defined in the $tmp variable and if it did not exist it would be created per the variable.

        I was not expecting that part to make any assignment.

        Was that also a mistake?

        • This reply was modified 1 month, 1 week ago by othompson09.
    • #277698
      Participant
      Topics: 5
      Replies: 252
      Points: 998
      Helping Hand
      Rank: Major Contributor

      No worries.  I teach PowerShell in my normal job and I tell my students that they should at least know what the script they are running is doing by being able to look at the code and explain it.  Some may disagree with me on this point, but when you are running scripts across a operational network using administrator privileges, it is as you point out “dangerous” to not understand how the script works.  Hopefully, you have a test environment or sandbox to try stuff out before operational employment.

      • #277716
        Participant
        Topics: 8
        Replies: 23
        Points: 104
        Rank: Participant

        Yes we do have a test environment that I use for testing and for the most part I (thought) I understood what the script was doing.  I just was not 100% sure of how it was doing it.

        However I agree with your stand on knowing what a script is suppose to be doing before running it.

    • #277707
      Participant
      Topics: 10
      Replies: 201
      Points: 987
      Helping Hand
      Rank: Major Contributor

      You are not providing the literal path to the VBS file for cscript.exe so it is defaulting to the Desktop. That is my take on this. No way for me to test.

    • #277710
      Participant
      Topics: 8
      Replies: 23
      Points: 104
      Rank: Participant

      I made some tweaks to the script based on additional ways to do what I was trying to do and now I am getting a very different error. It makes me feel like I am getting closer but I am not good enough at Powershell “Yet” to understand if I am making progress or going backward.

      Here is the new code and below that the new error.

      NEWCODE

      ========================================

      $Username = ‘AdminAccount’
      $Password = ‘ADMINPassword’
      $pass = ConvertTo-SecureString -AsPlainText $Password -Force
      $Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass
      $VNC = ‘\\Servershare\Filer7\Software\VLC Media Player\VLC3.0.11\Install.vbs’
      $Computer = ‘Targetcomputer’
      $TMP = “\\$Computer\c$\TEMP”

      if (!(Test-Path $TMP)) {
      New-Item -Path $TMP -ItemType Directory
      }

      Copy-Item -LiteralPath (Split-Path $VNC -Parent) -Destination $TMP -Container -Recurse -Force -Verbose

      try {
      Invoke-Command -ComputerName “$Computer” -Credential $Cred -ScriptBlock -ErrorAction Stop {
      Start-Process “C:\Temps\VLC3.0.11\Install.bat”
      }
      } catch {
      Write-Host “error”
      }

       

      NEWERROR

      =========================================

      error

      PS C:\Users\raf03576> $Error
      Invoke-Command : Missing an argument for parameter ‘ScriptBlock’. Specify a parameter of type
      ‘System.Management.Automation.ScriptBlock’ and try again.
      At C:\Users\raf03576\Documents\InstallVLC3.0_V3.ps1:16 char:64
      + … mand -ComputerName “$Computer” -Credential $Cred -ScriptBlock -ErrorA …
      + ~~~~~~~~~~~~
      + CategoryInfo : InvalidArgument: (:) [Invoke-Command], ParameterBindingException
      + FullyQualifiedErrorId : MissingArgument,Microsoft.PowerShell.Commands.InvokeCommandCommand

    • #277719
      Participant
      Topics: 10
      Replies: 201
      Points: 987
      Helping Hand
      Rank: Major Contributor

      Your positional parameters are a tad off, try this:

      Invoke-Command -ComputerName $Computer -Credential $Cred -ScriptBlock {Start-Process C:\Temps\VLC3.0.11\Install.bat} -ErrorAction Stop

       

Viewing 8 reply threads
  • You must be logged in to reply to this topic.