2017-08-01 12 views
1

私はActive Directoryに対する猶予を行うためにpowershellスクリプトを記述しました。 私は面白いエラーを取得しています。 ここにスクリプトがあります。PowershellスクリプトがForEach-Objectを有効なコマンドレットとして認識しない

#imports the module active directory if it isn't there. 
function add-ADmodule() 
      { 
      $modules = Get-Module | Where-Object{$_.Name -like "*ActiveDirectory*"} 

      if($modules -eq $null) 
       { 
       Import-Module ActiveDirectory 
       } 
      } 

#import the data file 
$user_csv = import-csv C:\temp\users.csv 

#makes the ammendments to the AD object 
function ammend-ADUsers($user_csv) 
    {#this is the loop to make ammendments to each object 
     $users_csv|ForEach-Object` 
       { 
       #assigns each user's AD object to a variable 
       $user_object = get-aduser -filter * ` 
              -Properties mail |` 
           Where-Object{$_.mail -like $_."Email Address"} 

       #ammends the ad object in the above variable 
       set-aduser -Identity $user_object ` 
          -OfficePhone $_."Office Number" ` 
          -MobilePhone $_."Mobile Number" ` 
          -StreetAddress $_."Street" ` 
          -City $_."City" ` 
          -PostalCode $_."PostCode"  
       } 
    } 


#this is the main part of the code where it gets executed 

add-ADmodule 
Write-Verbose "Active Directory Module Added" 

ammend-ADUsers($user_csv) 

これはエラーです。

PS C:\Users\admin> C:\Scripts\ammend-aduser.ps1 
ForEach-Object : The term 'ForEach-Object' is not recognized as the name of a 
cmdlet, function, script file, or operable program. Check the spelling of the 
name, or if a path was included, verify that the path is correct and try again. 
At C:\Scripts\ammend-aduser.ps1:18 char:20 
+   $users_csv|ForEach-Object` 
+     ~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : ObjectNotFound: (ForEach-Object:String) [], Com 
    mandNotFoundException 
    + FullyQualifiedErrorId : CommandNotFoundException 

このエラーの原因や原因を確認できません。

+1

はこのために気をつけて - 'ammend-ADUsers($ user_csv)' - それは良いのPowerShellコマンドレットの引数構文ではありません、あなたは 'ammend-ADUsers $ user_csv'を使用する必要があります。あなたのコードは動作しますが、他の言語と同じようには動作しません。 'ammend-ADUsers($ user_csv、$ param2)'のような2つのパラメータに使用しようとすると、ブレークし、両方を最初のパラメータへの配列として渡します代わりに。 – TessellatingHeckler

答えて

1

コマンドレットやバッククォート文字の間にスペースを入れていないので、あなたの問題はあるが、バッククォートを使用してだけではなく、同じ行に開く中括弧{を保持しない方が良いでしょう:

$users_csv|ForEach-Object { 

また、パイプ文字の後ろにバックティックは必要ありません。書式を改善するためにバックティックスの代わりにスプラットを使用することを検討することもできます(バッククッキーは見やすく、不適切に使いやすいため一般的には使用しないでください)。私は、以下の改正を提案:

$users_csv | ForEach-Object { 
     #assigns each user's AD object to a variable 
     $user_object = Get-ADUser -filter * -Properties mail | 
         Where-Object{$_.mail -like $_."Email Address"} 

     $Props = @{ 
      Identity = $user_object 
      OfficePhone = $_."Office Number" 
      MobilePhone = $_."Mobile Number" 
      StreetAddress = $_."Street" 
      City = $_."City" 
      PostalCode = $_."PostCode"  
     } 
     #ammends the ad object in the above variable 
     Set-ADUser @Props 
    } 
+0

このマークをありがとうございました。私はスプラッティングに精通していない、あなたはそれのための良いガイドの方向に私を指摘することはできますか? – DarkOverNerd

+0

確かに、これを確認してください:https://technet.microsoft.com/en-us/library/gg675931.aspx上記のように、必要なパラメータと設定のハッシュテーブルを作成し、そのハッシュテーブルを '$'ではなく '@ '文字でコマンドレットに送信するケースです。 –

関連する問題