Write-Progress in my profile

Welcome Forums General PowerShell Q&A Write-Progress in my profile

  • This topic has 3 replies, 2 voices, and was last updated 1 month ago by
    Participant
    .
Viewing 3 reply threads
  • Author
    Posts
    • #233410
      Participant
      Topics: 34
      Replies: 110
      Points: 565
      Rank: Major Contributor

      Powershell 5.1
      Windows 10

      I setup a $profile and this is my code

      
      # directory where my scripts are stored
      
      $psdir="C:\Util"  
      
      Get-ChildItem "${psdir}\*.ps1" 
      
      Cd \util 
      
      get-service nscp
      
      get-service teamviewer
      
      $PSVersionTable.PSVersion
      
      Write-Host "Welcome to Tom's Custom PowerShell Environment" 
      

      All works fine

      Now I want to add this
      Import-Module VMware.PowerCLI So it loads every time I start powershell
      Since the import-module takes awhile to load I would like to put a progress bar on the screen

      I tried this

      For ($i=0; $i -le 100; $i++) {
          Import-Module VMware.PowerCLI 
          Write-Progress -Activity "ActivityString" -Status "StatusString" -PercentComplete $i -CurrentOperation "CurrentOperationString"
      }
      
      

      I do not see the bar

      It take this long to run
      Loading personal and system profiles took 34953ms.

      Any ideas

      Thank you

      Tom

      I changed this around

      For ($i=0; $i -le 100; $i++) {
          Write-Progress -Activity "Load VMware PowerCLI" -Status "Current Count: $i" -PercentComplete $i -CurrentOperation "Please wait ..."
          Import-Module VMware.PowerCLI
      }
      

      The process bar shows now but the counter stays at 0 and between the { } nothing should be something displayed.

      Thaks

      • This topic was modified 1 month ago by Tom Grassi. Reason: made a change
    • #233425
      Participant
      Topics: 5
      Replies: 2373
      Points: 6,011
      Helping Hand
      Rank: Community MVP

      Tom,

      just as an aside: properly installed modules do autoload if needed since Powershell version 3.0. And even if you want to make sure a script does not run without a desired module you could use the #requires statement at the beginning of your script.

      In your for loop the Import-Module command blocks anything else until it’s finished loading the module. Then it writes the Progress, continues with the next iteration of the loop and tries to import the module again. And again and again and again … and this 100 times. 😉

      You should have read the help for Import-Module completely. Then you’d know that you can use the parameter -Verbose to ask the cmdlet to show a progress. 😉

    • #233443
      Participant
      Topics: 34
      Replies: 110
      Points: 565
      Rank: Major Contributor

      Olaf

      #Requires is that not a comment line:

      The verbose option gives details that I am not interested in seeing

      This code is almost working as I would like

      For ($i=0; $i -le 100; $i++) {
      Write-Progress -Activity “Load VMware PowerCLI” -Status “Current Count: $i” -PercentComplete $i -CurrentOperation “Please wait …”
      Import-Module VMware.PowerCLI
      }

      Just does not count or show bar I have a screen shoot but can not paste in this message how can I add images to here also

    • #233455
      Participant
      Topics: 5
      Replies: 2373
      Points: 6,011
      Helping Hand
      Rank: Community MVP

      #Requires is that not a comment line:

      Why don’t you click on the link I posted and read the documentation?

      This code is almost working as I would like

      Tom … it cannot work this way at all …. never.

      You would need to offload the task of loading the module and parse the verbose output to get a progress bar. I’d recommend to save your energy for something really useful. 😉

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