Quantcast
Channel: VMware Communities: Message List
Viewing all articles
Browse latest Browse all 293210

Working script but need help tweaking

$
0
0

add-pssnapin VMware.VimAutomation.Core

 

# Set Username and Password
$cred = Get-VICredentialStoreItem -file e:\powershell\cred2


# Setup Connection to VIServer
connect-VIServer -Server $cred.Host -User $cred.User -Password $cred.Password

 

# Set Date Time and Name of file
$date = get-date
$datefile = get-date -uformat '%m-%d-%Y'
$filename = "E:\Powershell\Stage_patching_list_" + $datefile + ".csv"

 

# Create Server List for Patching
$report = @()
Get-Folder Folder0, Folder1, Folder2, Folder3 | Get-VM | %{
      $vm = $_ | Get-View
      $row = "" | Select Name, State, Status, Host, CPU_Allocated, RAM_Allocated, LastReboot, LastPatchApplied, Rebooted, Issues   
      $row.Name = $_.Name
      $row.State = $_.PowerState
      $row.Status = $vm.Summary.OverallStatus
      $row.Host = $_.VMHost
$row.CPU_Allocated = $_.numcpu
$row.RAM_Allocated = $_.memorymb
$LastBoot=[System.Management.ManagementDateTimeConverter]::ToDateTime((Get-WmiObject win32_operatingsystem -ComputerName $row.Name).lastbootuptime)
$row.LastReboot = $LastBoot
$LastPatch = Get-WmiObject Win32_QuickFixEngineering -ComputerName $row.Name | ? { $_.InstalledOn } | sort installedon | select -Last 1 #| ft hotfixid, installedon
$row.LastPatchApplied = $LastPatch  
$row.Rebooted = "YES/NO"
$row.Issues = "YES/NO"
      $report += $row
}

 

# End the Session on the Server
disconnect-viserver -confirm:$false

 

# Creat the Coma Seperated File
$report | Export-Csv $filename -NoType -Force

 

# Create mail message  
$server = "mail.xata.com"
$port = 25
$to      = "someone@somewhere.com"
$from    = Server@somewhere.com
$subject = "Stage Patching Server list"
$body = "Please see attached file. TEXT TEXT TEXT TEXT" 
$message = New-Object system.net.mail.MailMessage $from, $to, $subject, $body

# Create SMTP client
$client = New-Object system.Net.Mail.SmtpClient $server, $port

# Try to send the message  
try {    
     $message.IsBodyHTML = $true   
     $attachment = new-object Net.Mail.Attachment($filename)    
     $message.attachments.add($attachment)   

# Send message    
     $client.Send($message)    
}

# Message failure catch
catch {      
     "Exception caught in CreateTestMessage1(): "
}

 

The script works when I run it manualy (comment out the snapin) but I see some authentication errors on the cli. Most notably the "Get-WmiObject : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESS DENIED))" error. However the CSV populates with all the information as if there was no error at all. We would like to have this running on a scheduler for our patch dates. I am just wondering if the errors that showup in powerCLI will prevent a scheduled task from completing? Also is there a way to correct these issues we are running the script as a user that has local admin rights on the servers the script checks. I have tried -authentication 6 -enableallprivaleges and a host of other googled answers. None of them have corrected the issue. However its baffling as it still returns the correct information.

 

Is there a better way to do this? Is there a cleaner way to code the script? Last question is there a way to change the output of "Get-WmiObject Win32_QuickFixEngineering -ComputerName $row.Name | ? { $_.InstalledOn } | sort installedon | select -Last 1 #| ft hotfixid, installedon" from "

\\Server\root\cimv2:Win32_QuickFixEngineering.HotFixID="KB2624667",ServicePackInEffect="SP3" to something a little more readable like "Last Patch = KB2624667"? We do not need the service pack information.

 

I have cobbled this together from many other scripts into a workable script that does what we want I am just wondering if there is a way to clean it up. Thank you for looking and taking the time to respond.


Viewing all articles
Browse latest Browse all 293210

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>