PowerCLI

 View Only
Expand all | Collapse all

Working script but need help tweaking

  • 1.  Working script but need help tweaking

    Posted Jan 22, 2012 03:06 PM

    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.



  • 2.  RE: Working script but need help tweaking

    Posted Jan 23, 2012 01:13 PM

    The Access denied message you see for Get-WmiObject can have several reasons.

    Which OS do you have on the servers ? W2K8 or W2K8R2 ?

    You could try using the Credential parameter on the Get-WmiObject cmdlets. You will have to provide an account of course.

    For the last patch value you can perhaps better do

    $row.LastPatchApplied = (Get-WmiObject Win32_QuickFixEngineering | ? { $_.InstalledOn } | Sort-Object -Property InstalledO | Select -Last 1).HotfixId
    


  • 3.  RE: Working script but need help tweaking

    Posted Jan 23, 2012 01:39 PM

    That looks much more readable! Thank you. We have a mixed enviroment of W2k3 w2k8 and w2k8r2. Now I am passing the cred file to the vm but how do I pass it to the WMI? I attempted this but it failed also. I am confused if I run the command from the CLI it works with no error.

    Is there a way to get the date of the last patch applied? 



  • 4.  RE: Working script but need help tweaking

    Posted Jan 23, 2012 02:00 PM

    The credentials are passed with the Credential parameter of the Get-WmiObject cmdlet.

    Get-WmiObject -Credential $cred ...

    The InstalledOn property seems to a know problem.

    Try bypassing it as follows

    $lastPatch = Get-WMIObject Win32_QuickFixEngineering -ComputerName $row.Name |
    where {($_.psBase.properties["InstalledOn"].Value).Length -gt 0} | 
    Sort-Object -Property {$_.psbase.Properties["InstalledOn"].Value} |
    Select -Last 1 

    $row
    .LastPatchApplied = $lastPatch.psbase.Properties["InstalledOn"].Value


  • 5.  RE: Working script but need help tweaking

    Posted Jan 23, 2012 02:30 PM

    Hmm, So I tried the -Credential $cred and get the same error.

    I assume I can use the $cred from earlier in the script right?

    Example

    $row.LastReboot = [System.Management.ManagementDateTimeConverter]::ToDateTime((Get-WmiObject -Credential $cred win32_operatingsystem -cn $row.Name).lastbootuptime)

    Produces error

    Get-WmiObject : Cannot process argument transformation on parameter 'Credential
    '. userName
    At E:\Powershell\Servers_for_Stage_Patching.ps1:27 char:106
    +     $row.LastReboot = [System.Management.ManagementDateTimeConverter]::ToDate
    Time((Get-WmiObject -Credential <<<<  $cred win32_operatingsystem -cn $row.Name
    ).lastbootuptime)
        + CategoryInfo          : InvalidData: (:) [Get-WmiObject], ParameterBindi
       n...mationException
        + FullyQualifiedErrorId : ParameterArgumentTransformationError,Microsoft.P
       owerShell.Commands.GetWmiObjectCommand

    Along with this error no DATA is retrieved. If I do not put in the -Credential $cred I get the same error but only once and Data is retrieved. Its driving me mad! :smileyhappy: Thanks for all your help thus far LucD.



  • 6.  RE: Working script but need help tweaking

    Posted Jan 23, 2012 02:47 PM

    That depends, the $cred from earlier in the script is used to connect to your vCenter server.

    If that same account also has the authority to query the WMI on remote machines, the $cred should be ok.

    I also saw that same "Access denied" on some machines due to UAC being active.

    Try disabling UAC on one of the servers that gives an "Access denied" error and see if the problem is then gone.



  • 7.  RE: Working script but need help tweaking

    Posted Jan 23, 2012 03:03 PM

    UAC is turned off on all of the w2k8 boxes.

    Could this be why?

    Get-WmiObject : Cannot process argument transformation on parameter 'Credential
    '. userName

    Seems that its not passing my cred file.

    Would I have to pass the value as I do for connecting to the VIserver such as $cred.User ? If so how do I pass the Password?



  • 8.  RE: Working script but need help tweaking

    Posted Jan 23, 2012 04:43 PM

    So this is interesting If I create this section all on its own no failures. So its something that is causing the error within that loop I suspect.

    # Set Username and Password
    $user = Get-Content e:\Powershell\User.txt
    $password = Get-Content e:\Powershell\pass.txt | ConvertTo-SecureString
    $cred1 = New-Object System.Management.Automation.PsCredential($user,$password)

    [System.Management.ManagementDateTimeConverter]::ToDateTime((Get-WmiObject -Credential $cred1 win32_operatingsystem -cn Server).lastbootuptime) | out-File "E:\Powershell\test.txt"




  • 9.  RE: Working script but need help tweaking

    Posted Jan 23, 2012 04:54 PM

    That seems to indicate that the saved credential, that you load into $cred, does not work for the Get-WmiObject Credential parameter.



  • 10.  RE: Working script but need help tweaking

    Posted Jan 23, 2012 05:14 PM

    We found the culprit. Its one server out of 62 that is giving the error. Hence why the script does not crap out fully. 



  • 11.  RE: Working script but need help tweaking

    Posted Jan 23, 2012 05:40 PM

    Great. Did the InstalledOn work the way I mentioned earlier ?



  • 12.  RE: Working script but need help tweaking

    Posted Jan 23, 2012 05:59 PM

    Not entirely. It does return a date but its several months before the last patch was installed.

    Here is what we are running

    # Create Server List for Patching
    $report = @()
    Get-Folder Folder, Folder1, Folder2, Folder3 | Get-VM | %{
          $vm = $_ | Get-View
          $row = "" | Select Name, State, Status, Host, CPU_Allocated, RAM_Allocated, LastReboot, LastPatchApplied, LastPatchDate, Rebooted, Issues  
          $row.Name = $_.Name
          $row.State = $_.PowerState
          $row.Status = $vm.Summary.OverallStatus
          $row.Host = $_.VMHost
          $row.CPU_Allocated = $_.numcpu
          $row.RAM_Allocated = $_.memorymb
          $row.LastReboot = [System.Management.ManagementDateTimeConverter]::ToDateTime((Get-WmiObject win32_operatingsystem -cn

    $row.Name).lastbootuptime)
    $row.LastPatchApplied = (Get-WmiObject Win32_QuickFixEngineering | ? { $_.InstalledOn } | Sort-Object -Property InstalledOn | Select -Last 1).HotfixId
    $lastPatch = Get-WMIObject Win32_QuickFixEngineering -ComputerName $row.Name |where {($_.psBase.properties["InstalledOn"].Value).Length -gt 0} | Sort-Object -Property {$_.psbase.Properties["InstalledOn"].Value} | Select -Last 1
    $row.LastPatchDate = $lastPatch.psbase.Properties["InstalledOn"].Value
            $row.Rebooted = "YES/NO"
    $row.Issues = "YES/NO"
          $report += $row
    }

    # End the Session on the Server

    And here is how it comes out.

    NameStateStatusHostCPU_AllocatedRAM_AllocatedLastRebootLastPatchAppliedLastPatchDateRebootedIssues
    ServerPoweredOngreenHost440961/5/2012 19:51KB25396358/9/2010YES/NOYES/NO
    ServerPoweredOngreenHost220481/5/2012 19:53KB25396358/9/2010YES/NOYES/NO
    ServerPoweredOngreenHost220481/5/2012 19:09KB25396359/29/2010YES/NOYES/NO
    ServerPoweredOngreenHost220481/5/2012 19:03KB25396359/28/2011YES/NOYES/NO
    ServerPoweredOngreenHost220481/5/2012 18:50KB25396355/20/2010YES/NOYES/NO
    ServerPoweredOngreenHost220481/5/2012 19:09KB25396359/29/2010YES/NOYES/NO
    ServerPoweredOngreenHost220481/5/2012 19:22KB25396359/28/2011YES/NOYES/NO
    ServerPoweredOngreenHost220481/5/2012 19:03KB25396359/28/2011YES/NOYES/NO
    ServerPoweredOngreenHost220481/5/2012 18:56KB25396359/29/2010YES/NOYES/NO
    ServerPoweredOngreenHost440961/5/2012 18:57KB25396359/29/2010YES/NOYES/NO
    ServerPoweredOngreenHost440961/5/2012 18:55KB25396359/29/2010YES/NOYES/NO
    ServerPoweredOngreenHost220481/5/2012 18:49KB25396359/28/2011YES/NOYES/NO
    ServerPoweredOngreenHost440961/5/2012 19:03KB25396359/28/2011YES/NOYES/NO
    ServerPoweredOngreenHost220481/5/2012 19:02KB25396359/29/2010YES/NOYES/NO
    ServerPoweredOngreenHost440961/5/2012 18:56KB25396359/29/2010YES/NOYES/NO
    ServerPoweredOngreenHost220481/5/2012 19:23KB25396359/29/2010YES/NOYES/NO
    ServerPoweredOngreenHost220481/5/2012 19:09KB25396359/29/2010YES/NOYES/NO
    ServerPoweredOngreenHost220481/10/2012 12:53KB25396355/20/2010YES/NOYES/NO
    ServerPoweredOngreenHost220481/5/2012 18:55KB25396359/29/2010YES/NOYES/NO
    ServerPoweredOngreenHost481921/5/2012 18:57KB25396359/29/2010YES/NOYES/NO
    ServerPoweredOngreenHost240961/5/2012 18:53KB25396359/28/2011YES/NOYES/NO

    So now they are showing all the same patch (which is wrong) and a very old date stamp. Ideas?

    No more errors! Just incorrect Data.

    Message was edited by: XataDale



  • 13.  RE: Working script but need help tweaking
    Best Answer

    Posted Jan 23, 2012 07:11 PM

    I dug a bit deeper, and it seems that the InstalledOn property is different on a Windows 2008 host compared to a Windows 2003 or Windows 2008 R2 host.

    This led me to the following code.

    # Create Server List for Patching
    $report = @() foreach($vm in Get-VM){     $row = "" | Select Name, State, Status, Host, CPU_Allocated, RAM_Allocated, LastReboot, LastPatchApplied, LastPatchDate, Rebooted, Issues     $row.Name = $vm.Name     $row.State = $vm.PowerState     $row.Status = $vm.ExtensionData.Summary.OverallStatus     $row.Host = $vm.VMHost     $row.CPU_Allocated = $vm.numcpu     $row.RAM_Allocated = $vm.memorymb     $row.LastReboot = [System.Management.ManagementDateTimeConverter]::ToDateTime((Get-WmiObject win32_operatingsystem -cn $row.Name).lastbootuptime)     $lastPatch = Get-WMIObject Win32_QuickFixEngineering -ComputerName $row.Name |         where {($_.psBase.properties["InstalledOn"].Value).Length -gt 0} |         Sort-Object -Property {             if($vm.Guest.GuestId -eq "WinLonghornGuest"){                 $_.InstalledOn             }else{                 [datetime]($_.psbase.Properties["InstalledOn"].Value)             }         } |         Select -Last 1
        $row.LastPatchApplied = $lastPatch.HotfixId     if($vm.Guest.GuestId -eq "WinLonghornGuest"){         $row.LastPatchDate = $lastPatch.InstalledOn     }     else{         $row.LastPatchDate = $lastPatch.psbase.Properties["InstalledOn"].Value     }     $row.Rebooted = "YES/NO"
        $row.Issues = "YES/NO"
        $report += $row
    }
    $report
    # End the Session on the Server

    If it is a Windows 2008 server, the date is there in clear text, for Windows 2003 and Windows 2008 R2 we need to use the PSBase trick.

    On the Sort-Object I use a codeblock to define the sorting value, and in there I can test what OS we are dealing with.

    In a similar way I fill in the $row.LastPatchDate property.



  • 14.  RE: Working script but need help tweaking

    Posted Jan 23, 2012 07:31 PM

    That is perfect!!! Thank you so much for your help. Really apperciated. +++++++++++++Rep 



  • 15.  RE: Working script but need help tweaking

    Posted Jan 23, 2012 10:39 PM

    Last issue we are having is powered down VMs cause the script to error on unable to make RPC call as it should I tried this

    # Create Server List for Patching
    $report = @()
    Get-Folder Folder, Folder1, Folder2, Folder3 | Get-VM | %{
        $row = "" | Select Name, State, Status, Host, CPU_Allocated, RAM_Allocated, LastReboot, LastPatchApplied, LastPatchDate, Rebooted, Issues
        $row.Name = $_.Name
        $row.State = $_.PowerState
        $row.Status = $_.ExtensionData.Summary.OverallStatus
        $row.Host = $_.VMHost
        $row.CPU_Allocated = $_.numcpu
        $row.RAM_Allocated = $_.memorymb
    if($row.State = "PoweredOn"){
    $row.LastReboot = [System.Management.ManagementDateTimeConverter]::ToDateTime((Get-WmiObject win32_operatingsystem -cn $row.Name).lastbootuptime)
         $lastPatch = Get-WMIObject Win32_QuickFixEngineering -ComputerName $row.Name |
            where {($_.psBase.properties["InstalledOn"].Value).Length -gt 0} |
            Sort-Object -Property {
                if($vm.Guest.GuestId -eq "WinLonghornGuest"){
                    $_.InstalledOn
                }else{
                    [datetime]($_.psbase.Properties["InstalledOn"].Value)
                }
            } |
            Select -Last 1
        $row.LastPatchApplied = $lastPatch.HotfixId
        if($vm.Guest.GuestId -eq "WinLonghornGuest"){
            $row.LastPatchDate = $lastPatch.InstalledOn
        }
        else{
            $row.LastPatchDate = $lastPatch.psbase.Properties["InstalledOn"].Value
        }
    }
    else{
    $row.LastReboot = "SHUT DOWN"
    $row.LastPatchApplied = "SHUT DOWN"
    $row.LastPatchDate = "SHUT DOWN"
    }

        $row.Rebooted = "YES/NO"
        $row.Issues = "YES/NO"
        $report += $row
    }
    $report

    No Dice. I thought by making the If else statment it would work but it wont. Any ideas? Again thank you so much for the assistance.



  • 16.  RE: Working script but need help tweaking

    Posted Jan 24, 2012 04:29 AM

    Thanks for sharing the script XataDale,

    However my LastPatchDate sometimes cannot be converted into date time format but instead it looks something like "01cc8accc01c8dc0" ?



  • 17.  RE: Working script but need help tweaking

    Posted Jan 24, 2012 06:28 AM

    That looks like a timestamp coming from a Windows 2008 server.

    Is that correct ? Or is that VM running another OS version ?



  • 18.  RE: Working script but need help tweaking

    Posted Jan 24, 2012 04:04 PM

    I have one that is doing that too. The OS is identified in VM as winLongHornGuest which is Windows 2008 - Service Pack 2 produces the timestamp 01cc3c777514eda9

    Anyone have any idea?



  • 19.  RE: Working script but need help tweaking

    Posted Jan 25, 2012 06:38 AM

    I could reproduce the problem with a Windows 2008 64-bit VM.

    The script didn't test for that OS. The following updated version should do the trick

    # Create Server List for Patching 
    $report = @() $vms = Get-Folder Folder, Folder1, Folder2, Folder3 | Get-VM
    foreach
    ($vm in $vms){     $row = "" | Select Name, State, Status, Host, CPU_Allocated, RAM_Allocated, LastReboot, LastPatchApplied, LastPatchDate, Rebooted, Issues     $row.Name = $vm.Name     $row.State = $vm.PowerState     $row.Status = $vm.ExtensionData.Summary.OverallStatus     $row.Host = $vm.VMHost     $row.CPU_Allocated = $vm.numcpu     $row.RAM_Allocated = $vm.memorymb     if($row.State -eq "PoweredOn"){         $row.LastReboot = [System.Management.ManagementDateTimeConverter]::ToDateTime((Get-WmiObject win32_operatingsystem -cn $row.Name).lastbootuptime)         $lastPatch = Get-WMIObject Win32_QuickFixEngineering -ComputerName $row.Name |         where {($_.psBase.properties["InstalledOn"].Value).Length -gt 0} |         Sort-Object -Property {             if("winNetStandardGuest","WinLonghornGuest","winLonghorn64Guest" -contains $vm.Guest.GuestId){                 $_.InstalledOn             }else{                 [datetime]($_.psbase.Properties["InstalledOn"].Value)             }         } | Select -Last 1
           
    if("winNetStandardGuest","WinLonghornGuest","winLonghorn64Guest" -contains $vm.Guest.GuestId){             $row.LastPatchDate = $lastPatch.InstalledOn         }         else{             $row.LastPatchDate = $lastPatch.psbase.Properties["InstalledOn"].Value         }         $row.LastPatchApplied = $lastPatch.HotfixId     }     $row.Rebooted = "YES/NO"
       
    $row.Issues = "YES/NO"
       
    $report += $row
    }
    $report
    # End the Session on the Server


  • 20.  RE: Working script but need help tweaking

    Posted Jan 30, 2012 01:32 PM

    Ok LucD how do you get the nice code output? :smileylaugh:



  • 21.  RE: Working script but need help tweaking

    Posted Jan 30, 2012 01:39 PM


  • 22.  RE: Working script but need help tweaking

    Posted Jan 24, 2012 06:27 AM

    That test should

    if($row.State -eq "PoweredOn"){


  • 23.  RE: Working script but need help tweaking

    Posted Jan 24, 2012 12:41 PM

    Gah! Of all things ok. That was easy thanks. Everything is happy all the logic is there. If anyone wants the full working script post here and I will upload it after I take out all our Companies info.



  • 24.  RE: Working script but need help tweaking

    Posted Jan 24, 2012 11:50 PM

    Yes please share it here mate :-)

    Thanks.



  • 25.  RE: Working script but need help tweaking

    Posted Jan 30, 2012 01:29 PM

    @AlbertWt Sorry I was out for a few days. Here is the completed working script

    #Call the Powershell snapin this is used instead of running powershell and then executing the script
    #Comment out if testing from an open PowerCLI window
    add-pssnapin VMware.VimAutomation.Core

    #Ask for user email so the entire team does not get blasted with information
    $email = Read-Host "Please type your email address, then press enter"
    Write-host "   "


    #Ask for .ad Credentials we need this to query the WMI of the servers.
    Read-Host "Once you hit enter a box will ask for your credentials. Supply your Domain account and password"
    $cred = Get-Credential

    # Setup Connection to VIServer
    connect-VIServer -Server SOMESERVER

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

    # Create Server List for Patching
    $report = @()
    Get-Folder Folder, Folder1, Folder2, Folder3 | Get-VM | %{
        $row = "" | Select Name, State, Status, Host, CPU_Allocated, RAM_Allocated, OperatingSystem, LastReboot, LastPatchApplied, LastPatchDate, Rebooted, Issues
        $row.Name = $_.Name
        $row.State = $_.PowerState
        $row.Status = $_.ExtensionData.Summary.OverallStatus
        $row.Host = $_.VMHost
        $row.CPU_Allocated = $_.numcpu
        $row.RAM_Allocated = $_.memorymb
        $row.OperatingSystem = $_.Guest.GuestId

    #We want to exclude any Linux enviroments list can be found here http://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.vm.GuestOsDescriptor.GuestOsIdentifier.html
        if($row.OperatingSystem -eq "sles11_64Guest","Linux2","Linux3"){
    $row.LastReboot = "This is a Linux "
    $row.LastPatchApplied = "Server. No Patches "
    $row.LastPatchDate = "are aviliable."
    $row.Rebooted = "NO CHECK"
    $row.Issues = "NO CHECK"
        }else{
    if($row.Name -eq [System.Net.Dns]::GetHostName()){
      $row.LastReboot = "You are currently "
      $row.LastPatchApplied = "logged into this "
      $row.LastPatchDate = "server. WMI not possible"
      $row.Rebooted = "NO CHECK"
      $row.Issues = "NO CHECK"
    }else{
      if($row.State -eq "PoweredOn"){
       $row.LastReboot = [System.Management.ManagementDateTimeConverter]::ToDateTime((Get-WmiObject -credential $cred win32_operatingsystem -cn $row.Name).lastbootuptime)
           $lastPatch = Get-WMIObject -credential $cred Win32_QuickFixEngineering -cn $row.Name |
               where {($_.psBase.properties["InstalledOn"].Value).Length -gt 0} |
               Sort-Object -Property {
                    if("winNetStandardGuest","WinLonghornGuest","winLonghorn64Guest" -contains $_.Guest.GuestId){
                        $_.InstalledOn
                    }else{
                        [datetime]($_.psbase.Properties["InstalledOn"].Value)
                    }
               } |
               Select -Last 1
                   if("winNetStandardGuest","WinLonghornGuest","winLonghorn64Guest" -contains $_.Guest.GuestId){
                    $row.LastPatchDate = $lastPatch.InstalledOn
               }else{
                    $row.LastPatchDate = $lastPatch.psbase.Properties["InstalledOn"].Value
               }
              $row.LastPatchApplied = $lastPatch.HotfixId

      }else{
       $row.LastReboot = "SERVER OFFLINE"
       $row.LastPatchApplied = "SERVER OFFLINE"
       $row.LastPatchDate = "SERVER OFFLINE"
       $row.Rebooted = "SERVER OFFLINE"
       $row.Issues = "SERVER OFFLINE"
      }
    }
        }
        $row.Rebooted = ""
        $row.Issues = ""
        $report += $row
    }
    $report

    # 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.SOMESERVER.com"
    $port = 25
    $to      = "$email"
    $from    = "SOMESERVER@SOMEDOMAIN.com"
    $subject = "Whatever you want the subject line to read"
    $body = "Whatever you want the body to read" 
    $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)    
    }

    # Catch if we can not send email
    catch {      
         "Exception caught in CreateTestMessage1(): "
    }



  • 26.  RE: Working script but need help tweaking

    Posted Jan 23, 2012 04:44 PM

    No, I don't think the fact UAC is off will cause an "Access denied".

    There can be numerous reasons for the "Access denied".

    Check out Connecting to WMI on a Remote Computer and Securing a Remote WMI Connection.

    I'm not sure how you obtained the $cred for the connection to the vCeter server, but you could try to use another variable and fill in the user and password via the Get-Credential cmdlet.


    $credWMI = Get-Credential "User and password for the WMI connection" 
    Get-WmiObject
    -Credential $credWMI ...

    See if that works