2017-01-23 5 views
-1

2つの関数を実行したい、最初の関数 "ad"はユーザーからのリストを実行するためのもので、出力関数ではcsv形式を使用して適切な出力を更新する必要があります。出力は空です。Powershell関数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

2つの関数を実行する必要があります.1つは抽出用、もう1つは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 

:あなたの関数は、CSV出力ファイルを作成するために、Export-Csvにパイプ、その後、それらのオブジェクトをオブジェクトのリストを返すad持っています

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

とパイプとの二つの機能を接続します:

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

あなたの助けに感謝@Ansgar Wiechers。 –

+0

@ Ansgar Wiecher - ありがとう、助けてくれてありがとうございます。 PowerShellスクリプトの学習を始め、さまざまな方法でファイルをCSV形式で書き出すことを考えました。 私は$ input変数について質問しましたが、これはすべてについてです。 –

+0

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