良い一日の人々、
私は、自分の管理下にあるWindowsシステムでローカル管理者パスワードを管理するというありふれた管理タスクを自動化することを主な目的として、このスクリプト(元々はSitaram Pamarthiによって作成された)を適応させようとしています。これまでのところ、スクリプトは機能しています-パスワードを変更して電子メール出力を取得することができます(テキスト形式ではありますが)-しかし、まだ完全には機能していません。
これは私のスクリプトからの抜粋です:
完全なスクリプトは、完全なコンテキストでここから入手できます。
$Computers = Get-Content -Path $InputFile
#Now let's loop through each computer and change the specified account password.
ForEach ($Computer in $Computers) {
$Computer = $Computer.ToUpper()
$IsOnline = "OFFLINE"
$Status = "SUCCESS"
Write-Verbose "Working on $Computer"
If((Test-Connection -ComputerName $Computer -count 1 -ErrorAction 0)) {
$IsOnline = "ONLINE"
Write-Verbose "`t$Computer is Online"
} Else { Write-Verbose "`t$Computer is OFFLINE" }
Try {
$Account = [ADSI]("WinNT://$Computer/$TargetAccount,user")
$Account.psbase.invoke("setpassword",$Pwd1_Text)
Write-Verbose "`tPassword Change completed successfully"
}
Catch {
$Status = "FAILED"
Write-Verbose "`tFailed to Change the password for $TargetAccount on $Computer. Error: $_"
$StatsError = "$_"
}
$Obj = New-Object -TypeName PSObject -Property @{
Date = "$(Get-Date)"
ComputerName = $Computer
IsOnline = $IsOnline
PasswordChangeStatus = $Status
DetailedStatus = $StatsError
}
$Obj | Format-Table ComputerName, Date, IsOnline, PasswordChangeStatus, DetailedStatus -AutoSize
$Obj | Select-Object "ComputerName", "Date", "IsOnline", "PasswordChangeStatus", "DetailedStatus" | Export-Csv -Append -Path ".\output.csv" -NoTypeInformation
If($Status -eq "FAILED" -or $IsOnline -eq "OFFLINE") {
$Stream.Writeline("$Computer `t $IsOnline `t $Status")
}
}
#Finally, let's close the Stream Writer, write the stream to the text file
#and notify the user of the location of the FailedComputers log file
$Stream.Close()
Write-Host "`n`nFailed computers list is saved to $FailedComputers"
$ScriptEndDate = $(Get-Date)
#Send an email log of what was done
$MsgBody = @"
Dear IT Admins,
Please be advised that the local admin account password for [ $TargetAccount ] has been changed
successfully on the following hosts:
Action Initiated by: `t$([System.Security.Principal.WindowsIdentity]::GetCurrent().Name)
Script Started: `t$ScriptStartDate
Script Ended: `t$ScriptEndDate
"@
Send-MailMessage -Body $MsgBody -Verbose
# Finally, let's clear the default parameters we set earlier
$PSDefaultParameterValues.Clear()
#END SCRIPT
スクリプトは現在、次のようにPowerShellコンソールに出力します。
ComputerName Date IsOnline PasswordChangeStatus DetailedStatus
------------ ---- -------- -------------------- --------------
HOME2 02/10/2019 06:18:20 ONLINE FAILED Exception calling "Invoke" with "2" argument(s): "Access is denied....
ComputerName Date IsOnline PasswordChangeStatus DetailedStatus
------------ ---- -------- -------------------- --------------
HOME3 02/10/2019 06:18:22 ONLINE FAILED Exception calling "Invoke" with "2" argument(s): "Access is denied....
ComputerName Date IsOnline PasswordChangeStatus DetailedStatus
------------ ---- -------- -------------------- --------------
HOME4 02/10/2019 06:18:29 OFFLINE FAILED Exception calling "Invoke" with "2" argument(s): "The network path was not found....
だから私の質問-電子メールの本文に含めることができるように、スクリプトを次のように(オンラインホストとオフラインホストの両方に単一のヘッダー行を使用して)統合するにはどうすればよいですか?
ComputerName Date IsOnline PasswordChangeStatus DetailedStatus
------------ ---- -------- -------------------- --------------
HOME2 02/10/2019 06:18:20 ONLINE FAILED Exception calling "Invoke" with "2" argument(s): "Access is denied....
HOME3 02/10/2019 06:18:22 ONLINE FAILED Exception calling "Invoke" with "2" argument(s): "Access is denied....
HOME4 02/10/2019 06:18:29 OFFLINE FAILED Exception calling "Invoke" with "2" argument(s): "The network path was not found....
なんらかの方法でコンソール出力をテーブルに変換してから、電子メールメッセージにスタイルを適用するために、Send-MailMessageコマンドレットに渡すことができるヘッダー変数にCSSを含める必要があると思います。しかし、私はこれを達成する方法をまだ理解していないので、このプラットフォームからの助けをいただければ幸いです。
皆さん、ありがとうございました!
ゲームに少し遅れますPSObjects
が、すべてを収集/保存してArrayList
から、を適用する必要がありますFormat-Table
。この例は期待どおりに機能します。コンピューターがオンラインであるかどうかをテストし、パスワード変更コードにコメントを付けています。
$Computers = @('SERVERA', 'SERVERB')
[System.Collections.ArrayList] $res = New-Object -TypeName "System.Collections.ArrayList"
#Now let's loop through each computer and change the specified account password.
ForEach ($Computer in $Computers) {
$Computer = $Computer.ToUpper()
$IsOnline = "OFFLINE"
$Status = "SUCCESS"
Write-Verbose "Working on $Computer"
If((Test-Connection -ComputerName $Computer -count 1 -ErrorAction 0)) {
$IsOnline = "ONLINE"
Write-Verbose "`t$Computer is Online"
} Else { Write-Verbose "`t$Computer is OFFLINE" }
<#
Try {
$Account = [ADSI]("WinNT://$Computer/$TargetAccount,user")
$Account.psbase.invoke("setpassword",$Pwd1_Text)
Write-Verbose "`tPassword Change completed successfully"
}
Catch {
$Status = "FAILED"
Write-Verbose "`tFailed to Change the password for $TargetAccount on $Computer. Error: $_"
$StatsError = "$_"
}
#>
$Obj = New-Object -TypeName PSObject -Property @{
Date = "$(Get-Date)"
ComputerName = $Computer
IsOnline = $IsOnline
PasswordChangeStatus = $Status
DetailedStatus = $StatsError
}
$res.Add($Obj)
#$Obj | Format-Table ComputerName, Date, IsOnline, PasswordChangeStatus, DetailedStatus -AutoSize
#$Obj | Select-Object "ComputerName", "Date", "IsOnline", "PasswordChangeStatus", "DetailedStatus" | Export-Csv -Append -Path ".\output.csv" -NoTypeInformation
If($Status -eq "FAILED" -or $IsOnline -eq "OFFLINE") {
$Stream.Writeline("$Computer `t $IsOnline `t $Status")
}
}
$res.ToArray() | Format-Table ComputerName, Date, IsOnline, PasswordChangeStatus, DetailedStatus -AutoSize
#This will output 1 file, with all the computers in it, with only 1 set of headers.
$res.ToArray() | Select-Object "ComputerName", "Date", "IsOnline", "PasswordChangeStatus", "DetailedStatus" | Export-Csv -Append -Path ".\output.csv" -NoTypeInformation
このような結果が得られます
ComputerName Date IsOnline PasswordChangeStatus DetailedStatus
------------ ---- -------- -------------------- --------------
SERVERA 02/11/2019 11:05:57 ONLINE SUCCESS
SERVERB 02/11/2019 11:05:57 ONLINE SUCCESS
Format-Table
コマンドレットを実行し続けるため、コンソールは同じオブジェクトタイプを出力することを認識していないと思います。したがって、複数回呼び出しているため、出力テーブルがコンソールに対して明示的に解析されていることを確認する必要があります。配列に物があり、Format-Table
1回だけ呼び出すと、次のように期待される結果が得られます。
私もExport-Csv
あなたのforeachから移動し、それも期待どおりに機能することに注意してください。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加