Workflow: 重新启动电脑,等待PowerShell,执行命令

huangapple go评论69阅读模式
英文:

Workflow: Restart PC, wait for powershell, execute commands

问题

创建一个脚本来重新启动特定网络上的设备,并添加一些通知或迭代变量,以让我知道它们已经重新启动,并且可以利用PowerShell。然而,我甚至无法让最基本的工作流按照我期望的方式运行。

我没有收到任何错误消息,但与此同时什么也没有发生,所以我根本无法开始理解为什么它不起作用。非常感谢任何见解。

Import-Module PSWorkflow

# 获取87 VLAN上工作站IP地址的列表
[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

简洁明了,仅用于测试是否起作用。运行时没有重新启动数组中的计算机。没有抛出任何错误消息,也没有重新启动数组中的设备,$ComputerIP 不列出存储在该变量中的IP地址。

也许我做错了什么,或者只是忽略了某些东西,但我测试的其他一切都按照我期望的方式工作,除了这个。

工作流肯定是起作用的,因为在工作流中放入“'$var + 1'”并在外部调用$Var时,我得到了预期的结果,其他基本命令也正常工作。我知道我可以只使用foreach而不使用工作流并重新启动所有设备,但我想监视每个设备,以查看powershell何时恢复,以便我可以继续执行脚本。我目前使用的是PowerShell 5.1,因此我认为我的并行选项有限。

英文:

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.

答案1

得分: 0

工作流程无法从调用范围"看到" NetScan 变量。

将工作流程参数化并将列表作为参数传递:

Import-Module PSWorkflow

# 获取 87 VLAN 上的工作站 IP 地址列表
$NetScan = Get-Content -Path C:\temp\NetScan.log

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

    foreach -parallel ($Computer in $TargetComputer) {
        # 尝试重新启动
        try {
            Restart-Computer -ComputerName $Computer -Force -Wait -For Powershell -ErrorAction Stop
            $success = $true
            $message = '重新启动完成'
        }
        catch {
            $success = $false
            $message = "$_"
        }
        # 输出结果
        [PSCustomObject]@{
            Target  = $Computer
            Success = $success
            Message = $message
        }
    }
}

Restart-87Network -TargetComputer $NetScan

我自行添加了一些错误处理到你的工作流程。

英文:

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

huangapple
  • 本文由 发表于 2023年7月14日 01:12:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76681830.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定