Powershell

Get Computer List from DC

Get-ADComputer -Filter * -SearchBase "CN=Workstations, DC=contoso, DC=com"

删除输入文件的双引号

... | ConvertTo-Csv -NoTypeInformation | % { $_ -replace '"' } | Out-File $csv

如果要删除首行

... | select -Skip 1 | Out-File $csv

完整实例

Get-ADComputer -Filter * -SearchBase "OU=cpg computers,dc=isnet,DC=corp,DC=org" -Properties * |Select name| ConvertTo-Csv -NoTypeInformation | % { $_ -replace '"' } | select -Skip 1 |Out-File C:\Script\PingEnduser\RemoteComputers.txt

PING IP and Send email

############ping tEST############### 
Get-Content C:\Script\PingEnduser\RemoteComputers.txt | ForEach-Object{
$pingstatus = ""
IF (Test-Connection -BufferSize 32 -Count 1 -ComputerName $_ -Quiet) {
        $pingstatus = "Online"
} Else {
        $pingstatus = "Offline"
}

New-Object -TypeName PSObject -Property @{
      Computer = $_
      Status = $pingstatus }
} | Export-Csv C:\Script\PingEnduser\PingStatus_$(get-date -f dd_MMM).csv -NoTypeInformation -Encoding UTF8

###########Email Sending Part######## 

###########Define Variables######## 
 
$fromaddress = "[email protected]" 
$toaddress = "[email protected]" 
$CCaddress = "[email protected]"
#$bccaddress = "[email protected]" 
#$CCaddress = "[email protected]" 
$Subject = "Offline check for Enduser" 
$body = "PING Test for all CPG laptop and Desktop after office hour."
#$body = get-content .\content.htm 
$attachment = "C:\Script\PingEnduser\PingStatus_$(get-date -f dd_MMM).csv" 
$smtpserver = "[email protected]" 
 
#################################### 
 
$message = new-object System.Net.Mail.MailMessage 
$message.From = $fromaddress 
$message.To.Add($toaddress)
$message.CC.Add($CCaddress) 
#$message.Bcc.Add($bccaddress) 
$message.IsBodyHtml = $True 
$message.Subject = $Subject 
$attach = new-object Net.Mail.Attachment($attachment)
$message.Attachments.Add($attach)
$message.body = $body 
$smtp = new-object Net.Mail.SmtpClient($smtpserver)
$smtp.Send($message)
 
#################################################################################

Leave a Reply

Your email address will not be published. Required fields are marked *