gpt-4· OpenAI
PRISM Alignment Dataset
I need a PowerShell script to extract all servers from my domain with the operating…
- User
I need a PowerShell script to extract all servers from my domain with the operating system version, IP address, Name, location and comments listed in the extract CSV file emailed to me on a weekly schedule
- Assistant
Here's a basic idea:
# Script $Output = @() $Servers = Get-ADComputer -Filter {OperatingSystem -Like "*Server*"} foreach ($server in $Servers) { $props = [ordered] @{ "Name" = $server.Name; "OS" = $server.OperatingSystem; "IP" = Test-Connection -ComputerName $server.Name -Count 1 | Select -ExpandProperty IPV4Address; #Replace 'location' and 'comments' with actual AD properties "Location" = $server.location; "Comments" = $server.comments; } $Output += New-Object -TypeName PSObject -Property $props; } $Output | Export-Csv -Path .\Output.csv -NoTypeInformation; #Email $smtpServer = "Your.SMTP.Server" $smtpPort = "587" $mailTo = "Your.email@domain.com" $mailFrom = "NoReply@domain.com" $subject = "Server Info" $attachment = ".\Output.csv" $body = "Attached is the requested information." Send-MailMessage -SmtpServer $smtpServer -Port $smtpPort -To $mailTo -From $mail - User
can you also add in the CPU and RAM values to the output please?
- Assistant
Sure, you'd need to add a couple of lines to extract CPU and RAM information:
#.. previous code $wmi = Get-WmiObject -ComputerName $server.Name -Class Win32_ComputerSystem $cpu = Get-WmiObject -ComputerName $server.Name -Class Win32_Processor $props += @{ "RAM (GB)" = $wmi.TotalPhysicalMemory / 1GB -as [int] "CPU" = $cpu.Name } #.. rest of the codeThis will add the server's total physical memory (converted to GB) and CPU name to your CSV output.
- User
I also need the script CC to my manager and my Cloud Administrator can you add in the CC field please?
- Assistant
Sure, you'd just need to add the
Ccparameter to theSend-MailMessagecmdlet. Here's how you can do it.$mailCC1 = "manager.email@domain.com" $mailCC2 = "cloudadmin.email@domain.com" #... Send-MailMessage -SmtpServer $smtpServer -Port $smtpPort -To $mailTo -Cc $mailCC1, $mailCC2 -From $mailFrom -Subject $subject -Body $body -Attachments $attachmentThis will CC the email to your manager and your Cloud Administrator.