0

Creating a script to restart devices on a specific network and add some notification or iteration variable that lets me know they have been restarted and powershell is available to be leveraged. However, I can't even get the most basic of workflow to run as I expect it to.

I am getting no error messages, but at the same time nothing is happening so I have no way to even begin to understand why it isn't working. Any insight is greatly appreciated.

Import-Module PSWorkflow

#Get list of workstation IP Addresses on the 87 VLAN
[System.Collections.ArrayList]$NetScan = get-content -path C:\temp\NetScan.log

Workflow Restart87Network{
    
    
    Foreach -parallel ($ComputerIP in $NetScan)
    {
        Restart-Computer -PSComputerName $ComputerIP -force -wait -for powershell;
        $ComputerIP
    }
    
    
}

Restart87Network

Short and sweet just for testing to see if it is working. When it runs it doesn't restart the computers that are in the Array. Doesn't throw any error messages, doesn't restart the devices in the array, and the $ComputerIP does not list the IP addresses that are held in that variable.

Maybe I am doing something wrong, or just overlooking something, but everything else I test works as I expect it to except this.

Workflows are definitely working because putting '$var + 1' in the workflow and calling $Var outside of it I get the expected result and other basic commands are working as well. I know I can just use foreach without a workflow and have all the devices restarted, but I want to monitor each one to see when powershell is back up so that I can continue executing a script after. I am currently on Powershell 5.1 so I believe my parallel options are limited.

2
  • "and the $ComputerIP does not list the IP addresses that are held in that variable." - how do you know that it ever reaches that statement and isn't just stuck waiting forever for the remove computer to restart? FWIW the parameter name is -ComputerName, not -PSComputerName, maybe try fixing that. Commented Jul 13, 2023 at 17:24
  • When used in a workflow -computername does not work, it will only accept -pscomputername for whatever reason. The workflow executes and appears to be complete. When it is complete it should output whatever resides in the $ComputerIP but it does not. If I were to do a workflow with just $ComputerIP in there it would list the values within. For some reason it is hanging up on the restart command as you mention, but there is no troubleshooting I can do as no error is presented. Commented Jul 13, 2023 at 18:03

1 Answer 1

0

The workflow can't "see" the NetScan variable from the calling scope.

Parameterize the workflow and pass the list as an argument:

Import-Module PSWorkflow

#Get list of workstation IP Addresses on the 87 VLAN
$NetScan = Get-Content -Path C:\temp\NetScan.log

Workflow Restart-87Network {
    param([string[]]$TargetComputer)

    foreach -parallel ($Computer in $TargetComputer) {
        # attempt to restart
        try {
            Restart-Computer -ComputerName $Computer -Force -Wait -For Powershell -ErrorAction Stop
            $success = $true
            $message = 'Reboot complete'
        }
        catch {
            $success = $false
            $message = "$_"
        }
        # output results 
        [PSCustomObject]@{
            Target  = $Computer
            Success = $success
            Message = $message
        }
    }
}

Restart-87Network -TargetComputer $NetScan

I took the liberty of adding some error handling to your workflow

Sign up to request clarification or add additional context in comments.

3 Comments

I will have to see if this works tomorrow. I really appreciate the responses! Out of curiosity, is it because the Foreach is only looking at the scope within the workflow for the $NetScan variable, and it cannot see it as it is outside of the workflow? Because if I were to simply place $NetScan in the workflow it will output the array.
@ThePostMan That's right, only variables declared/assigned within the workflow body can be resolved from inside it. This differs from regular script functions which can access the calling scope.
That's working! Thank you so much! However, do you happen to have any insight as to why my powerhsell will not accept the -computername parameter for Restart-Computer? When I try to run using it, it says it could not find the computername parameter, and in Remote Connectivity it is handled by the PSComputerName Parameter. When using PSComputerName as the parameter it works as expected. I am on v5.1 if that makes any difference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.