locked
need help with script RRS feed

  • Question

  • Hello,

    I am trying to get the hypervhostname and servername for a list of servers in a tabular format using powershell.

    When executed the script returns no errors but doesnt output or email me anything. Can some please shed some light

    where the script is wrong?

    Here is my script:

    $ErrorActionPreference = "Continue"; 
    $users = "dbateam@domain.com"
    $cc = "DBRequests@domain.com"
    $reportPath = "C:\disk\ListHostName\Output"; 
    $reportName = $datetime + "index.html"; 
    $HostNamesReport = $reportPath + $reportName 
    
     
    $i = 0; 
     
    	$computers = Get-Content "C:\disk\ListHostName\Servers\VirtualServers.txt"; 
    	$datetime = Get-Date -Format "MM-dd-yyyy_HHmmss"; 
     
    $titleDate = get-date -uformat "%m-%d-%Y - %A" 
    $header = " 
      <html> 
      <head> 
      <meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
      <title>List of Virtual SQL Servers and Their Hosts</title> 
      <STYLE TYPE='text/css'> 
      <!-- 
      td { 
       font-family: Tahoma; 
       font-size: 11px; 
       border- 
       border-right: 1px solid #999999; 
       border-bottom: 1px solid #999999; 
       border- 
       padding- 
       padding-right: 0px; 
       padding-bottom: 0px; 
       padding- 
      } 
      body { 
       margin- 
       margin- 
       margin-right: 0px; 
       margin-bottom: 10px; 
       table { 
       border: thin solid #000000; 
      } 
      --> 
      </style> 
      </head> 
      <body> 
      <table width='100%'> 
      <tr bgcolor='#CCCCCC'> 
      <td colspan='7' height='25' align='center'> 
      <font face='tahoma' color='#003399' size='4'><strong>HyperVHostName List of Virtual SQLServers for $titledate</strong></font> 
      </td> 
      </tr> 
      </table> 
    " 
     Add-Content $HostNamesReport $header 
     
    # Create and write Table header for report 
     $tableHeader = " 
     <table width='100%'><tbody> 
     <tr bgcolor=#CCCCCC> 
     <td width='10%' align='center'>VirtualHost</td> 
     <td width='5%' align='center'>HostName</td> 
      
     </tr> 
    " 
    Add-Content $HostNamesReport $tableHeader 
      
    # Start processing disk space reports against a list of servers 
      foreach($a in $computers) 
     {  
     $virtualhost = (Get-ItemProperty –path “HKLM:\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters”).PhysicalHostName
     $hostname = (Get-ItemProperty –path “HKLM:\SOFTWARE\Microsoft\Virtual Machine\Auto”).FullyQualifiedDomainName
       # Create table data rows  
        $dataRow = " 
      <tr> 
      <td width='10%'>$virtualhost</td> 
      <td width='5%' align='center'>$hostname</td> 
      </tr> 
    " 
    
    } 
     
     
    # Send Notification if alert $i is greater then 0 
    if ($i -gt 0) 
    { 
        foreach ($user in $users) 
    { 
            Write-Host "Sending Email notification to $user" 
    # SMTP PROPERTIES
    # SMTP Server settings that will allow the report to be emailed to you.
      $smtpServer = "EXCHANGE100" 
      $smtp = New-Object Net.Mail.SmtpClient($smtpServer) 
      $msg = New-Object Net.Mail.MailMessage 
      $msg.To.Add($user)
      $msg.CC.Add($cc)
            $msg.From = "dbateam@domain.com" 
      $msg.Subject = "$titledate is not responding. Please check" 
           
      $smtp.Send($msg) 
            $body = "Report completed."
    		}
    	}

    experts please share your valuable insights.

    Friday, May 26, 2017 6:15 PM

All replies

  • help Send-MailMessage -Full

    Follow the instructions.

    You are suing old and broken code copied from the Internet.

    You cannot get the remote registry using the reg provider.


    \_(ツ)_/


    • Edited by jrv Friday, May 26, 2017 6:22 PM
    Friday, May 26, 2017 6:20 PM
  • start with:

    Get-Vm -ComputerName $computer

    Help Get-Vm -full

    help ConvertTo-Html -full


    \_(ツ)_/

    • Proposed as answer by Hello_2018 Monday, June 5, 2017 2:07 AM
    Friday, May 26, 2017 6:25 PM
  • Hi,

    these codes is unreadable.

    it's not too complexity, you could write your own script, e.g.:

    $header = " 
      <html> 
      <head> 
      <meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
      <title>List of Virtual SQL Servers and Their Hosts</title> 
      <STYLE TYPE='text/css'> 
      <!-- 
      td { 
       font-family: Tahoma; 
       font-size: 11px; 
       border- 
       border-right: 1px solid #999999; 
       border-bottom: 1px solid #999999; 
       border- 
       padding- 
       padding-right: 0px; 
       padding-bottom: 0px; 
       padding- 
      } 
      body { 
       margin- 
       margin- 
       margin-right: 0px; 
       margin-bottom: 10px; 
       table { 
       border: thin solid #000000; 
      } 
      --> 
      </style> 
      </head> 
      <body> 
      <table width='100%'> 
      <tr bgcolor='#CCCCCC'> 
      <td colspan='7' height='25' align='center'> 
      <font face='tahoma' color='#003399' size='4'><strong>HyperVHostName List of Virtual SQLServers for $titledate</strong></font> 
      </td> 
      </tr> 
      </table> 
    " 
    $titleDate = get-date -uformat "%m-%d-%Y - %A" 
    $users = "dbateam@domain.com"
    $cc = "DBRequests@domain.com"
    $smtpServer = "EXCHANGE100" 
    $Subject = "$titledate is not responding. Please check"
    Get-Vm -ComputerName $computer | ConvertTo-Html | Out-File xx.html
    Send-MailMessage -From $users -to $users -Cc $cc -Subject $Subject -SmtpServer $smtpServer -BodyAsHtml -Body $header
    

    Besides, for sending mail message with table,you could refer to the following link:


    https://gallery.technet.microsoft.com/Script-to-generate-Memory-45079f02

    Best regards,

    Andy


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    • Proposed as answer by Hello_2018 Monday, June 5, 2017 2:07 AM
    Saturday, May 27, 2017 2:49 AM
  • Hi,
    Was your issue resolved? 
    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
    If no, please reply and tell us the current situation in order to provide further help.
    Best Regards,
    Andy

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Monday, June 5, 2017 2:07 AM