在嵌套循环中传递变量

时间:2017-06-27 09:40:45

标签: powershell

我想弄清楚如何将if语句中的变量传递给从以下开始的嵌套if语句:

  

if($ server -eq $ env:COMPUTERNAME)

Foreach ($comp in $computer) {

    $server = split-FQDN $comp -part H
    $domain = Split-FQDN $comp -part D  

    #Set the domain based parameters

    If ($domain -eq '1.fqdn.com') {
        set-domparams $domain $userprompt $userpass
    }

    Elseif ($domain -eq '2.fqdn.com') {
        set-domparams $domain $userprompt $userpass
    }

    Elseif ($domain -eq '3.fqdn.com') {
        set-domparams $domain $userprompt $userpass
    } 

    ElseIf ($domain -eq '4.fqdn.com') {
        set-domparams $domain $userprompt $userpass
    } 

    ElseIf ($domain -eq '5.fqdn.com') {
        set-domparams $domain $userprompt $userpass
    } 

    ElseIf ($domain -eq '6.fqdn.com') {
        set-domparams $domain $userprompt $userpass
    } 

    If ($server -eq $env:COMPUTERNAME) {
        $server = $_
        #clear Kerberos ticket cache
        Invoke-Expression -Command:'cmd.exe /c klist purge' | Out-Null
        $buildlogsuccess = check-buildlog $domain
        $chocologstatus = Invoke-Command -ScriptBlock {check-choco}
        $KMSvalues = Invoke-Command -ScriptBlock {get-winlicense}
        $parentOU = get-ParentOU (Get-ADComputer -Server $dc -SearchBase $searchbase -Filter {name -eq $server} -Credential $fetchCreds) | select -expand parentou
        $SCCMcheck = Invoke-Command -ScriptBlock {get-sccmstatus} 
        $scomcheck = Invoke-Command -ScriptBlock {get-scomstatus} -argumentlist $scom
        $AV = Invoke-Command -ScriptBlock {get-avstatus}
        $wfirewall = Invoke-Command -ScriptBlock {(get-service MpsSvc).status}
        $net35 = Invoke-Command -ScriptBlock {(Get-WindowsFeature NET-Framework-Core).installed}
        $admins = Invoke-Command -ScriptBlock {check-admins} 
        $DomainComms = Invoke-Command -ScriptBlock {get-domaininfo}
        $bigfix =  Invoke-Command -ScriptBlock {get-bigfix}
    }

    Else {
        $server = $_
        #clear Kerberos ticket cache
        Invoke-Expression -Command:'cmd.exe /c klist purge' | Out-Null
        #start running functions against target server(s) to get required info
        $PSSession = New-PSSession -ComputerName $comp -Credential $fetchCreds -Name $server
        $buildlogsuccess = check-buildlog $domain
        $chocologstatus = Invoke-Command -Session $pssession -ScriptBlock ${function:check-choco}
        $KMSvalues = Invoke-Command -Session $PSSession -ScriptBlock ${Function:get-winlicense}
        $parentOU = get-ParentOU (Get-ADComputer -Server $dc -SearchBase $searchbase -Filter {name -eq $server} -Credential $fetchCreds) | select -expand parentou
        $SCCMcheck = Invoke-Command -Session $PSSession -ScriptBlock ${Function:get-sccmstatus} 
        $scomcheck = Invoke-Command -Session $PSSession -ScriptBlock ${function:get-scomstatus} -argumentlist $scom
        $AV = Invoke-Command -Session $PSSession -ScriptBlock ${Function:get-avstatus}
        $wfirewall = Invoke-Command -Session $PSSession -ScriptBlock {(get-service MpsSvc).status}
        $net35 = Invoke-Command -Session $PSSession -ScriptBlock {(Get-WindowsFeature NET-Framework-Core).installed}
        $admins = Invoke-Command -Session $PSSession -ScriptBlock ${Function:check-admins} 
        $DomainComms = Invoke-Command -Session $PSSession -ScriptBlock ${Function:get-domaininfo}
        $bigfix =  Invoke-Command -Session $PSSession -ScriptBlock ${Function:get-bigfix}
        #clean up the remote session
        Remove-PSSession -Name $server
    }

我已经使用了一些由加载函数传递的全局变量,但我试图避免对这段代码执行相同的操作并了解如何传递剩余的变量($ comp,$ computer和$域)正确到下一个内循环。

我尝试过使用$ server来完成以下工作但尚未取得任何成功:

  

$ server = $ _

2 个答案:

答案 0 :(得分:2)

这更多是code review意见而不是答案,但无论如何。

为什么不使用table driven编程,而不是长if...elseif个结构?如果有很多域并且需要不同的凭据,则维护if...elseif会很繁琐。将凭据存储在自定义Powershell对象和哈希表中的对象中。然后只能使用域名进行查找。像这样,

# Declare a hashtable and add custom credential objects
$creds=@{}
$creds.Add('foo.fqdn', $(New-Object –TypeName PSObject –Prop @{'Domain'='foo.fqdn'; 'User'='foo.user'; 'Pass'='foo.pass'}))
$creds.Add('bar.fqdn', $(New-Object –TypeName PSObject –Prop @{'Domain'='bar.fqdn'; 'User'='bar.user'; 'Pass'='bar.pass'}))
$creds.Add('zof.fqdn', $(New-Object –TypeName PSObject –Prop @{'Domain'='zof.fqdn'; 'User'='zof.user'; 'Pass'='zof.pass'}))

# Later when credentials are needed, check if the domain is present
# and get the values from the hashtable.
if($creds.ContainsKey($domain) {
  set-domparams $domain $creds[$domain].User $creds[$domain].Pass
} else {
  write-warning "Domain $domain not found!"
}

答案 1 :(得分:1)

我认为这是一个非问题,因为你的变量在它们被创建的循环/语句中是有效的。

Foreach ($comp in $computer) {

    $server = split-FQDN $comp -part H
    $domain = Split-FQDN $comp -part D

    if($true){
       # $comp, $server and $domain are all in scope
       Write-host "$comp $server $domain"
           if($true){
               # also true in nested if statements
               Write-host "$comp $server $domain"
           }
    }
    Else{
       # $comp, $server and $domain are all in scope
       Write-host "$comp $server $domain"
    }
}
# outside, $comp is not available, only $computer
# $server and $domain will only contain the values from the last run of the foreach loop.