2017-01-23 1 views
-1

2 개의 함수를 실행하려면 첫 번째 함수 인 "ad"가 사용자 목록을 실행하기 위해 스크립트를 실행하는 동안 csv 형식을 사용하여 출력 함수에서 적절한 출력을 업데이트해야합니다. 출력이 비어 있습니다.Powershell Function in function

$out = "F:\inactive.csv" 

function ad() { 
    $users = Get-Content "C:\dis1.txt" 
    foreach ($user in $users) { 
     try { 
      $memberof = Get-ADUser $user -Properties * -ErrorAction Stop | Select-Object samaccountname, memberof 
      foreach ($groups in $memberof) { 
       if ($group=$groups.memberof -like "*mobile*") { 
        foreach ($name in $group) { 
         $gpnames += "$((Get-ADGroup $name).samaccountname);" 
         $gpname= "$($memberof.samaccountname),Exist,$($gpnames)" 
        } 
        Write-Output $gpname 
       } else { 
        Write-Output "$($memberof.samaccountname),Exist" 
       } 
      } 
     } catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException] { 
      $usrname = "$($user),NotExist" 
      Write-Output $usrname 
     } 
    } 
} 

function output { 
    param($usrname,$memberof,$gpnames,$ad) 
    New-Object PSObject -Property @{ 
     UserAccount = $memberof.samaccountname 
     NotExist = $usrname 
     GroupNames = $gpnames 
    } 

    $gpnames = $memberof = $usrname = $null 
    output | Select-Object useraccount, notexist, groupnames | Export-Csv $out 
} return output 
+3

죄송합니다, 그것은 무엇을 분명하지 않다 요 너는 묻고있다. 어떤 어려움을 겪으십니까? – JPBlanc

+0

두 개의 함수를 실행해야합니다. 하나는 추출을위한 것이고 다른 하나는 CSV 형식의 출력을위한 것입니다. –

+1

무엇이 오류입니까? 당신이 바로 잡으려고하는 행동은 무엇입니까? – sodawillow

답변

0

귀하의 접근 방식이 잘못되었습니다. 당신이 절대적으로 (알 수없는 어떤 이유)에 Export-Csv 포장 출력을위한 별도의 기능을해야하는 경우

function ad { 
    Get-Content 'C:\dis1.txt' | ForEach-Object { 
    $user = Get-ADUser -Filter "SamAccountName -eq '$_'" -Properties samaccountname, memberof 
    if ($user) { 
     $user | Select-Object SamAccountName, 
     @{n='UserExists';e={$true}}, 
     @{n='Groups';e={ 
      ($_.MemberOf | 
      Where-Object { $_ -like "*mobile*" } | 
      Get-ADGroup | 
      Select-Object -Expand SamAccountName) -join ',' 
     }} 
    } else { 
     New-Object -Type PSObject -Property { 
     SamAccountName = $_ 
     UserExists  = $false 
     Groups   = $null 
     } 
    } 
    } 
} 

ad | Export-Csv 'C:\path\to\output.csv' -NoType 

: 함수 객체의 목록을 반환 adExport-Csv에 다음 파이프 이러한 개체는 CSV 출력 파일을 만들 수 있습니다 이 같은 기능 :

function output($filename) { 
    $Input | Export-Csv $filename -NoType 
} 

및 파이프로 두 가지 기능을 연결합니다

ad | output 'C:\path\to\output.csv' 
+0

감사합니다. @Ansgar Wiechers. –

+0

@ Ansgar Wiecher - 도와 주셔서 감사합니다. 그 작품은 예외입니다. 다른 방법을 사용하여 CSV 형식으로 파일을 내보내려는 생각으로 PowerShell 스크립트를 배우기 시작했습니다. $ input 변수에 대한 쿼리가 있는데,이게 뭐야? –

+0

['Get-Help about_Automatic_Variables'] (https://msdn.microsoft.com/powershell/reference/3.0/Microsoft.PowerShell.Core/about/about_Automatic_Variables) –