循环到脚本

时间:2017-04-13 07:38:56

标签: powershell

我在使用循环向对象添加somthing时遇到问题。

这部分有效:

# Get Backup Jobs
$jobs = Get-VBRJob | ?{$_.JobType -eq "Backup"}
foreach ($job in $jobs) {
$jobOptions = New-Object PSObject
$jobOptions | Add-Member -MemberType NoteProperty -Name "JobName" -value $job.name
$jobOptions | Add-Member -MemberType NoteProperty -Name "Enabled" -value $job.isscheduleenabled

输出:

Jobname,Enabled,...    
Job1,True,...

我想要的是:

VMName,Jobname,Enabled

VM1,Job1,True,...

VM2,Job1,True,...

VM3,Job2,True,...

我试过了:

# Get Backup Jobs
$jobs = Get-VBRJob | ?{$_.JobType -eq "Backup"}
foreach ($job in $jobs) {

$vmnames = ($job.GetObjectsInJob()).name
$jobOptions = New-Object PSObject
$jobOptions | Add-Member -MemberType NoteProperty -Name "VMName" -value 
$vmnames

$jobOptions | Add-Member -MemberType NoteProperty -Name "JobName" -value 
$job.name
$jobOptions | Add-Member -MemberType NoteProperty -Name "Enabled" -value   
$job.isscheduleenabled

这会创建:

VMName,Jobname,Enabled

VM1 VM2,Job1,True,...

我需要改变什么?

4 个答案:

答案 0 :(得分:2)

您可以使用计算属性

$jobs = Get-VBRJob | 
    Where-Object JobType -eq "Backup" |
    Select-Object @{l="VMName"; e={($_.GetObjectsInJob()).name}}, 
    @{l="JobName"; e={$_.name}}, 
    @{l="Enabled"; e={$_.isscheduleenabled}}

注意:我认为$job.GetObjectsInJob().name会返回VMName。

答案 1 :(得分:0)

我会像这样遍历虚拟机,并使用+=添加到$jobOptions

# Get Backup Jobs
$jobs = Get-VBRJob | ?{$_.JobType -eq "Backup"}

foreach ($job in $jobs) {

    $vmnames = ($job.GetObjectsInJob()).name

    # change starts here; additional loop over VMs
    foreach ($vm in $vmnames){

        [array]$jobOptions += New-Object PSObject -Property @{
            "VMName"  = $vm
            "JobName" = $job.name
            "Enabled" = $job.isscheduleenabled
        }

    }
}

免责声明:我没有在PowerShell中使用过工作,但是不知道这会如何影响循环逻辑。

答案 2 :(得分:0)

Martins回答产生这个输出。

    VMName                                        JobName               Enabled
    ------                                        -------               -------
S01                                           Backup Job Server1       True
S02                                           Backup Job Server2       True
{C1, C2, C3, C4, C5, C6}                     Backup Job Clients       True

gms0ulman answer产生此输出。

VMName                                   JobName               Enabled
------                                   -------               -------
S01                                 Backup Job Server1       True
S02                                 Backup Job Server2       True
C1 C2 C3 C4 C5 C6                   Backup Job Clients       True

答案 3 :(得分:0)

在内部循环中,我会尝试循环遍历对象而不是循环遍历名称。这些方面的东西:

# Get Backup Jobs
$jobs = Get-VBRJob | ?{$_.JobType -eq "Backup"}

foreach ($job in $jobs) {
    # change starts here; additional loop over VMs
    foreach ($vm in $job.GetObjectsInJob()){

        [array]$jobOptions += New-Object PSObject -Property @{
            "VMName"  = $vm.name
            "JobName" = $job.name
            "Enabled" = $job.isscheduleenabled
        }

    }
}

我从gms0ulman的回复中借用了大部分代码。