1

I'm tying to work out how I pass a variable from an if statement into a nested if statement starting at:

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
    }

I already have some global variables in use that are passed by a loaded function, but I'm trying to avoid do the same for this bit of code and understand how to pass my remaining variables ($comp, $computer and $domain) correctly to the next inner loop.

I've tried getting $server to work doing the following but haven't had any success as of yet:

$server = $_

J1raya
  • 320
  • 1
  • 6
  • 20
  • 4
    I don't see an inner loop, can you clarify what you mean? There's no need to use `$_` (in fact its probably empty, unless there's some outer pipeline function or loop you haven't shown). `If` statements don't have any specific scope, you should just continue to use the variables you've been using. – Mark Wragg Jun 27 '17 at 09:45

2 Answers2

2

This is more code review opinion instead of an answer, but anyway.

Instead of long if...elseif constructs, why not use table driven programming? If there are lots of domains and they require different credentials, maintaining the if...elseif is tedious. Store the credentials in custom Powershell objects and those in a hashtable. Then lookup can be done with the domain name only. Like so,

# 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!"
}
vonPryz
  • 22,996
  • 7
  • 54
  • 65
  • Thanks vonPryz, that's much appreciated & something that will come in handy. I should have mentioned that the u/n & password objects are created & stored as part of an imported function that generates various domain objects & constructs queries from a hash table of values. – J1raya Jun 27 '17 at 10:15
1

I think this is a non-question as your variables are valid within the loops/statements they are created.

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.
G42
  • 9,791
  • 2
  • 19
  • 34
  • Thanks all. Don't know if something else was going on that made it look like variables weren't being passed properly, but seem ok now. – J1raya Jun 27 '17 at 10:06