只打印列表中的最后一个服务器,我想要所有服务器

时间:2017-07-06 21:26:00

标签: powershell citrix

这只会打印列表中的最后一个服务器,我希望获取所有服务器并打印到屏幕

$machines = (Get-BrokerMachine -AdminAddress $adminaddress -DesktopGroupName $deliverygroup | Select-Object DNSname).DNSname

foreach($machine in $machines){

$machinelist = Get-BrokerMachine -HostedMachineName $machine
    if($machinelist.InMaintenanceMode -eq $true){
        $status = "$machine is in maintenance mode"
        }else  {
        $status = "$machine is not in maintenance mode"
        }

}
Write-Host $status

3 个答案:

答案 0 :(得分:2)

$Status不断被列表中的当前计算机覆盖。

您正在寻找:

$Status+=

相反:

$Status=

您还要明确声明$Status在开头就是一个数组,如下所示:

$Status=@()

或者当您创建变量并在开头省略该行时。

[array]$Status +=

否则,您将获得一起运行的结果,因为它将被视为[String]

答案 1 :(得分:2)

这是一种类似PowerShell的方法(未经测试):

Get-BrokerMachine -AdminAddress $adminaddress -DesktopGroupName $deliverygroup | ForEach-Object {
  $machineName = $_.DNSName
  [PSCustomObject] @{
    "MachineName"     = $machineName
    "MaintenanceMode" = (Get-BrokerMachine -HostedMachineName $machine).InMaintenanceMode
  }
} | Export-Csv "C:\whatever\results.csv" -NoTypeInformation

答案 2 :(得分:0)

另一种时髦模式:

function get-BrokerMachineMode
{
    param (
        [Parameter(Mandatory = $true)]
        [string[]]$machines
    )
    begin
    {
        $ErrorActionPreference = 'Stop'
        Add-Type -Language CSharp @"
public class BrokenBroker {
        qpublic System.String MachineName;
        public System.String MaintenanceMode;

public  BrokenBroker (string MachineName, string MaintenanceMode)
    {
        this.MachineName  = MachineName;
        this.MaintenanceMode = IsInMaintenanceMode;
    }
}
"@

        $status = @()
        Write-Verbose "Created objects..."
    }
    process
    {
        try
        {
            $machines = (Get-BrokerMachine -AdminAddress $adminaddress `
                                           -DesktopGroupName $deliverygroup | Select-Object DNSname).DNSname

            foreach ($machine in $machines)
            {
                Write-Verbose "Checking machine: $machine"

                $machinelist = Get-BrokerMachine -HostedMachineName $machine

                if ($machinelist.InMaintenanceMode -eq $true)
                {
                    $status += New-Object BrokenBroker($machine, $true)
                }
                else
                {
                    $status += New-Object BrokenBroker($machine, $false)
                }

            }
        }
        catch
        {
            Write-Error $error[0].Exception.Message
        }
        $status
    }
    end
    {
        Write-Verbose "Done"
    }

}

这是您必须加载的功能,然后您只需使用以下命令启动它:

$computers = get-content = {PATH TO TXT FILE}
$list = get-BrokerMachineMode -machines $computers -Verbose