2017-06-27 3 views
1

私は文の場合、私は、ネストされたへの変数を渡す方法文がで始まる場合は動作するように抱き合わせている:渡す変数内部のネストされたループ

IF($サーバー-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、$ domain)を正しく内部の次のループに渡します。

私は次のことをやって動作するように$サーバーを取得しようとしたが、まだのよういずれかの成功を持っていなかった。

$サーバー= $ _

+4

私は内側のループが見えません。どういう意味ですか? '$ _'を使用する必要はありません(実際には、あなたが示していないいくつかの外部パイプライン関数やループがない限り、おそらく空です)。 'If​​'ステートメントに特定のスコープがない場合、使用している変数をそのまま使用してください。 –

答えて

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. 
+0

ありがとうございます。変数が正しく渡されていないように見えるようになった何か他のことが起こっているかどうかはわかりませんが、今は大丈夫です。 – J1raya

2

これは答えの代わりにcode reviewの意見ですが、とにかくです。

長いif...elseif構成の代わりに、table drivenプログラミングを使用してください。多くのドメインがあり、異なる資格情報が必要な場合は、if...elseifを維持するのは面倒です。カスタムPowershellオブジェクトとハッシュテーブルの証明書に資格情報を格納します。その後、ドメイン名のみでルックアップを行うことができます。そうですね、

# Declare a hashtable and add custom credential objects 
[email protected]{} 
$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!" 
} 
+0

おかげさまでvonPryz、それは大変感謝しています。 さまざまなドメインオブジェクトを生成し、値のハッシュテーブルからクエリを構築する、インポートされた関数の一部として、u/nとパスワードオブジェクトが作成および格納されていることを述べておく必要があります。 – J1raya

関連する問題