Restart-computer command in start-job scriptblock always returns as state “blocked”. RRS feed

  • Question

  • If I run the code below then the created job “test3” always has a state “blocked”. It never executes. It has no additional error message. When I run the command in the start-job scriptblock separately, then it does work (restart-computer -computername $computer -credential $MySecureCreds -force).

    [xml]$XAML = @"
            Title="CSS Server Reboot" Height="450" Width="800">
            <ComboBox Name="Server_Combobox" SelectedValuePath="Content" HorizontalAlignment="Left" Margin="240,107,0,0" VerticalAlignment="Top" Width="120">
                 <ComboBoxItem Name="Server1"></ComboBoxItem>          
            <Label Name="Title_Label" Content="CSS – Server Reboot&#xD;&#xA;" HorizontalAlignment="Left" Margin="240,41,0,0" VerticalAlignment="Top" Height="34" Width="284" FontSize="16"/>
            <Button Name="Reboot_Button" Content="Reboot" HorizontalAlignment="Left" Margin="449,107,0,0" VerticalAlignment="Top" Width="75"/>
            <TextBox Name="Reboot_Textbox" Grid.Column="1" HorizontalAlignment="Left" Height="173" Margin="81,180,0,0" TextWrapping="Wrap" VerticalScrollBarVisibility="Auto" Text="" VerticalAlignment="Top" Width="294"/>
    #Read XAML
    $reader=(New-Object System.Xml.XmlNodeReader $xaml) 
    try{$Form=[Windows.Markup.XamlReader]::Load( $reader )}
    catch{Write-Host "Unable to load Windows.Markup.XamlReader"; exit}
    # Store Form Objects In PowerShell
    $xaml.SelectNodes("//*[@Name]") | ForEach-Object {Set-Variable -Name ($_.Name) -Value $Form.FindName($_.Name)}
    # Secure credential creation
    $Username = "XXX"
    $Password = "XXX"
    $pass = ConvertTo-SecureString -AsPlainText $Password -Force
    $SecureString = $pass
    $MySecureCreds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Username,$pass 
    # Use the combobox
    $timer = new-object System.Windows.Threading.DispatcherTimer
    $Server_Combobox.add_SelectionChanged( { 
        param($sender, $args)
        $Script:computer = $sender.SelectedItem.Content
    } )
    #Assign reboot event to button
    $sb = [scriptblock]::Create("restart-computer -computername $computer -credential $MySecureCreds -force")
    Start-Job -Name "test3" -ScriptBlock $sb
    $Script:jobtest = Get-Job -Name "test3"
    $Reboot_Textbox.AddText("The restart procedure is starting.`n")
    $timer.Interval = [TimeSpan]"0:0:5.00"
    if($jobtest.State -ne "Running"){
    $Reboot_Textbox.AddText("The restart failed, please contact IT Ops !!!`n")
    $Reboot_Textbox.AddText("The error message is:`n")
    }elseif($jobtest.State -eq "Completed"){
    $Reboot_Textbox.AddText("The restart procedure completed successfully.")
    Wait-Job -Name test3 | Remove-Job
    $Reboot_Textbox.AddText("The restart procedure is ")
    $Form.ShowDialog() | out-null

    Tuesday, August 7, 2018 2:12 PM

All replies

  • You cannot run the job on two threads.

    The code is interleaved.  The timer will not work correctly.

    The timer cannot use Wait-Job.

    The timer should test for "Complete" or "Failed"  and process the job  accordingly.

    The timer must be stopped while processing a tick and restarted at the end of processing or you will have re-entry issues.


    Tuesday, August 7, 2018 2:50 PM
  • I see no reason for using WPF.  It is a bit difficult for non-programmers to work with.  WinForms is much simpler and has a timer.  It also does not create threading issues.


    Tuesday, August 7, 2018 2:51 PM