Skip to content
TheChatArchiveThe Chat Archive
gpt-4· OpenAIHugging Face PRISM Alignment Dataset

I need a PowerShell script to extract all servers from my domain with the operating…

  1. 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

  2. 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
    
  3. User

    can you also add in the CPU and RAM values to the output please?

  4. 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 code
    

    This will add the server's total physical memory (converted to GB) and CPU name to your CSV output.

  5. User

    I also need the script CC to my manager and my Cloud Administrator can you add in the CC field please?

  6. Assistant

    Sure, you'd just need to add the Cc parameter to the Send-MailMessage cmdlet. 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 $attachment
    

    This will CC the email to your manager and your Cloud Administrator.