Write-Progress problems

This topic contains 6 replies, has 2 voices, and was last updated by Profile photo of Thompson Thompson 3 years, 2 months ago.

  • Author
    Posts
  • #14280
    Profile photo of Thompson
    Thompson
    Participant

    I am trying to display a progress bar with the write-progress cmdlet for a change in ip address settings.
    I have the interface
    $iface = Get-WMiObject Win32_NetworkAdapterConfiguration | Where{$_.IPEnabled -eq "TRUE"}

    and I have the changes
    $iface.enablestatic($ip,$subnet)
    $iface.setdnsserversearchorder($dns)
    $iface.setgateways($gate)
    $iface.SetDynamicDNSRegistration(“FALSE”)

    How can I use write-progress to display the time taken to change the ip and subnet mask?

    There are variables missing such as $ip etc.

  • #14283
    Profile photo of Thompson
    Thompson
    Participant

    [quote=14281]I'm not sure Write-Progress is the right tool here. These steps should complete very quickly, and the progress indicator would just flash by. For example:

    Write-Progress -Activity 'Configuring Network Adapter' -CurrentOperation 'Assigning IP and Subnet' -PercentComplete 0
    $iface.enablestatic($ip,$subnet)
    
    Write-Progress -Activity 'Configuring Network Adapter' -CurrentOperation 'Configuring DNS Servers' -PercentComplete 25
    $iface.setdnsserversearchorder($dns)
    
    Write-Progress -Activity 'Configuring Network Adapter' -CurrentOperation 'Assigning Default Gateway' -PercentComplete 50
    $iface.setgateways($gate)
    
    Write-Progress -Activity 'Configuring Network Adapter' -CurrentOperation 'Configuring DNS registration' -PercentComplete 75
    $iface.SetDynamicDNSRegistration(“FALSE”)
    
    Write-Progress -Activity 'Configuring Network Adapter' -Completed
    

    (On a side note, keep in mind that $iface might be an array, if you have multiple IP-enabled adapters on your system.)

    Are you looking for something more like verbose output of the exact amount of time each operation took? For that, the code might look something like this:

    $elapsed = Measure-Command { $iface.enablestatic($ip,$subnet) }
    Write-Verbose ('Time to assign IP / Subnet Mask: {0:F2}ms' -f $elapsed.TotalMilliseconds)
    
    # and so on
    

    [/quote]

    Thank you so much for the help! I believe I understand what you mean when you talk about multiple adapters being in use. I tried the write-progress lines that you showed and when run in ISE it asks me to supply values for the parameter "Status".

    Write-Progress : Cannot bind argument to parameter 'Status' because it is an empty string.
    At C:\TCPSettings.ps1:53 char:15
    + Write-Progress <<<< -Activity 'Configuring Network Adapter' -Completed + CategoryInfo : InvalidData: (:) [Write-Progress], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.WriteProgressCommand I have to use Write-Progress hence my push for it. I will also try the write-verbose though! Thank you again!

  • #14284
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Ah, looks like -Status was a mandatory parameter in PowerShell 2.0; I assume that's what you're running? You can add it to each of the calls to Write-Progress, if so:

    Write-Progress -Activity 'Configuring Network Adapter' -Status 'Processing' -CurrentOperation 'Assigning IP and Subnet' -PercentComplete 0
    $iface.enablestatic($ip,$subnet)
    
    Write-Progress -Activity 'Configuring Network Adapter' -Status 'Processing' -CurrentOperation 'Configuring DNS Servers' -PercentComplete 25
    $iface.setdnsserversearchorder($dns)
    
    Write-Progress -Activity 'Configuring Network Adapter' -Status 'Processing' -CurrentOperation 'Assigning Default Gateway' -PercentComplete 50
    $iface.setgateways($gate)
    
    Write-Progress -Activity 'Configuring Network Adapter' -Status 'Processing' -CurrentOperation 'Configuring DNS registration' -PercentComplete 75
    $iface.SetDynamicDNSRegistration(“FALSE”)
    
    Write-Progress -Activity 'Configuring Network Adapter' -Status 'Complete' -Completed
    
  • #14285
    Profile photo of Thompson
    Thompson
    Participant

    [quote=14284]Ah, looks like -Status was a mandatory parameter in PowerShell 2.0; I assume that's what you're running? You can add it to each of the calls to Write-Progress, if so:

    Write-Progress -Activity 'Configuring Network Adapter' -Status 'Processing' -CurrentOperation 'Assigning IP and Subnet' -PercentComplete 0
    $iface.enablestatic($ip,$subnet)
    
    Write-Progress -Activity 'Configuring Network Adapter' -Status 'Processing' -CurrentOperation 'Configuring DNS Servers' -PercentComplete 25
    $iface.setdnsserversearchorder($dns)
    
    Write-Progress -Activity 'Configuring Network Adapter' -Status 'Processing' -CurrentOperation 'Assigning Default Gateway' -PercentComplete 50
    $iface.setgateways($gate)
    
    Write-Progress -Activity 'Configuring Network Adapter' -Status 'Processing' -CurrentOperation 'Configuring DNS registration' -PercentComplete 75
    $iface.SetDynamicDNSRegistration(“FALSE”)
    
    Write-Progress -Activity 'Configuring Network Adapter' -Status 'Complete' -Completed
    

    [/quote]

    I am still getting the same prompt for status plus a new error. I did not think anything was wrong with the way I got the interface but this is an error meaning that the value cannot be used for write-progress?

    Write-Progress : Cannot bind parameter 'Id'. Cannot convert the "System.Management.ManagementBaseObject" value of type "System.Management.ManagementBaseObject#\__PARAMETERS" to type
    "System.Int32".
    At C:\Users\brant\Documents\TCPSettings.ps1:47 char:19
    + Write-Progress <<<< -Activity 'Configuring Network Adapter' -Status 'Processing' -CurrentOperation 'Assigning IP and Subnet' -PercentComplete 0 $iface.enablestatic($ip,$subnet ) + CategoryInfo : InvalidArgument: (:) [Write-Progress], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.WriteProgressCommand Write-Progress : Cannot bind parameter 'Id'. Cannot convert the "System.Management.ManagementBaseObject" value of type "System.Management.ManagementBaseObject#\__PARAMETERS" to type "System.Int32". At C:\Users\brant\Documents\TCPSettings.ps1:49 char:15 + Write-Progress <<<< -Activity 'Configuring Network Adapter' -Status 'Processing' -CurrentOperation 'Configuring DNS Servers' -PercentComplete 25 $iface.setdnsserversearchorder($dn s) + CategoryInfo : InvalidArgument: (:) [Write-Progress], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.WriteProgressCommand Write-Progress : Cannot bind parameter 'Id'. Cannot convert the "System.Management.ManagementBaseObject" value of type "System.Management.ManagementBaseObject#\__PARAMETERS" to type "System.Int32". At C:\Users\brant\Documents\TCPSettings.ps1:51 char:15 + Write-Progress <<<< -Activity 'Configuring Network Adapter' -Status 'Processing' -CurrentOperation 'Assigning Default Gateway' -PercentComplete 50 $iface.setgateways($gate) + CategoryInfo : InvalidArgument: (:) [Write-Progress], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.WriteProgressCommand Write-Progress : Cannot bind parameter 'Id'. Cannot convert the "System.Management.ManagementBaseObject" value of type "System.Management.ManagementBaseObject#\__PARAMETERS" to type "System.Int32". At C:\Users\brant\Documents\TCPSettings.ps1:53 char:15 + Write-Progress <<<< -Activity 'Configuring Network Adapter' -Status 'Processing' -CurrentOperation 'Configuring DNS registration' -PercentComplete 75 $iface.SetDynamicDNSRegistrat ion(“FALSE”) + CategoryInfo : InvalidArgument: (:) [Write-Progress], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.WriteProgressCommand

  • #14286
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    It looks like you've put multiple statements on the same line, somehow. There should be a line break between "-PercentComplete xx" and $iface.Whatever()

  • #14287
    Profile photo of Thompson
    Thompson
    Participant

    [quote=14286]It looks like you've put multiple statements on the same line, somehow. There should be a line break between “-PercentComplete xx” and $iface.Whatever()[/quote]

    Silly me hahaha Thank you very much! That works perfectly and write-verbose works as well!

  • #14281
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    I'm not sure Write-Progress is the right tool here. These steps should complete very quickly, and the progress indicator would just flash by. For example:

    Write-Progress -Activity 'Configuring Network Adapter' -CurrentOperation 'Assigning IP and Subnet' -PercentComplete 0
    $iface.enablestatic($ip,$subnet)
    
    Write-Progress -Activity 'Configuring Network Adapter' -CurrentOperation 'Configuring DNS Servers' -PercentComplete 25
    $iface.setdnsserversearchorder($dns)
    
    Write-Progress -Activity 'Configuring Network Adapter' -CurrentOperation 'Assigning Default Gateway' -PercentComplete 50
    $iface.setgateways($gate)
    
    Write-Progress -Activity 'Configuring Network Adapter' -CurrentOperation 'Configuring DNS registration' -PercentComplete 75
    $iface.SetDynamicDNSRegistration(“FALSE”)
    
    Write-Progress -Activity 'Configuring Network Adapter' -Completed
    

    (On a side note, keep in mind that $iface might be an array, if you have multiple IP-enabled adapters on your system.)

    Are you looking for something more like verbose output of the exact amount of time each operation took? For that, the code might look something like this:

    $elapsed = Measure-Command { $iface.enablestatic($ip,$subnet) }
    Write-Verbose ('Time to assign IP / Subnet Mask: {0:F2}ms' -f $elapsed.TotalMilliseconds)
    
    # and so on
    

You must be logged in to reply to this topic.