0

私はこのスクリプトを使ってすべてのローカル管理者のパスワードをリセットしようとしていますが、サーバーリストにサーバーが1つしかないとスクリプトは完璧に動作します。エラーとして -ドメイン全体のローカル管理者パスワードの変更

サーバーx "ユーザー名が見つかりませんでした。 メンバ "SetInfo"の取得中に次の例外が発生しました: "ユーザーn が見つかりませんでした。 \ユーザー\ v7t7adm \デスクトップ\のlocaladmin.ps1:18文字:15 + $ user.SetInfo < < < <() + CategoryInfo:NotSpecified:(:) []、ExtendedTypeSystemExceptio N + FullyQualifiedErrorId:Cで CatchFromBaseGetMember

ここでは、私が作成したためにスクリプトがある -

$computers = Get-Content servers.txt 
"Name`tStatus" | Out-File -FilePath C:\users\v7t7adm\desktop\results.csv 
$user = "KArthur" 
$password = "Pol!sh90&8" 
foreach($server in $computers) 
{ 
try{ 
    $user = [adsi]"WinNT://$server/$user,user" 
    $user.SetPassword($password) 
    "$server`tSuccess" 
    "$server`tSuccess" | Out-File -FilePath C:\users\v7t7adm\desktop\results.csv -Append 
    } 
    catch{ 
     "$server`t" + $_.Exception.Message.ToString().Split(":")[1].Replace("`n","") 
     "$server`t" + $_.Exception.Message.ToString().Split(":")[1].Replace("`n","") | Out-File -FilePath C:\users\v7t7adm\desktop\results.csv -Append 

    } 

$user.SetInfo() 

} 
+2

にスクリプトを変更するには、スクリプトにプレーンテキストのパスワードをハードコードに非常に悪い考えです。 (全員が見るためにインターネットに投稿するのも良いことではありません) –

答えて

1

$user最初にユーザー名を含む文字列です。ループでは、ADSIオブジェクトで上書きします。 "WinNT://$server/$user,user"文字列で再び使用されますが、失敗します。

ループの$userに別の名前を使用するだけで問題ありません。

編集:

$computers = Get-Content servers.txt 
"Name`tStatus" | Out-File -FilePath C:\users\v7t7adm\desktop\results.csv 
$user = "KArthur" 
$password = "Pol!sh90&8" 

foreach($server in $computers) 
{ 
    try{ 
     $adsiUser = [adsi]"WinNT://$server/$user,user" 
     $adsiUser.SetPassword($password) 
     "$server`tSuccess" 
     "$server`tSuccess" | Out-File -FilePath C:\users\v7t7adm\desktop\results.csv -Append 
    } 
    catch{ 
      "$server`t" + $_.Exception.Message.ToString().Split(":")[1].Replace("`n","") 
      "$server`t" + $_.Exception.Message.ToString().Split(":")[1].Replace("`n","") | Out-File -FilePath C:\users\v7t7adm\desktop\results.csv -Append 
    } 

    $adsiUser.SetInfo() 
} 
+0

これはうまくいかず、私はループの中で$ userと$ passwordを宣言して動作しました。 –

関連する問題