header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Gridview Help
Last Post 20 Sep 2010 01:55 PM by George Howarth. 1 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
arusUser is Offline
New Member
New Member
Posts:1
Avatar

--
20 Sep 2010 04:22 AM
    Hi

    I am just starting out with Powershell and although have managed to write my 1st script to do what I want I am having trouble outputting to Gridview.

    It checks the presence of certain services on various servers from a txt file and then if they are not running starts them.

    Ideally I would like to get the output to a gridview but so far all my attempts are giving me a seperate gridview window for each service each time it loops through the script.

    Any ideas or pointers to where I sould be adding | out-gridview or any extra commands I am missing to get a list of computer names and status of the services in the 1 window. 

    If that is not possible getting the output to an XML file would be great but also have been unable to get that to work.  It just keeps on coming up blank.

    Thanks in advance

    # Setup trap to catch exceptions
    trap [Exception]
    {
    	write-error $("TRAPPED: " + $_.Exception.Message); 
    }
    
    $logfile = "d:\citrixout.log"
    
    cls
    
    # read computers from text file
    $computers = Get-Content D:\Computers.txt;
    $start = $True;
    
    #Services to check
    $serviceArray = 'CitrixXTEServer', 'CitrixWMIService', 'Citrix 64-bit Virtual Memory Optimization', 'Citrix Encryption Service', 'Citrix Encryption Service', 'Citrix SMA Service', 'Citrix Virtual Memory Optimization', 'CitrixHealthMon', 'IMAService';
    
    
    foreach($computer in $computers)
    {
    	Write-Host "Checking $computer"-foregroundcolor Black -backgroundcolor Blue | Out-File $logfile -Append -noclobber;
    	$objWMIService = Get-WmiObject -Class win32_service -computer $computer
    	
    	foreach($service in $objWMIService)
    	{	
    		# Check each service specicfied in the $serviceArray
    		foreach($srv in $serviceArray)
    		{
    			if($service.name -eq $srv)
    			{
    					Write-Host $testing "$srv is present on $computer." -foregroundcolor Green;
    						if($service.state -eq "running") 
    				 					
    								
    				{
    					Write-Host "$srv is running on $computer" -foregroundcolor Green;
    				}
    				else
    				{
    					Write-Host "$srv is not running on $computer" -foregroundcolor yellow;
    					# If $start is true the script will attempt to start the service if it is stopped
    					if($start -eq $true)
    					{
    						# Attempt to start the current service on the current computer
    						$serviceInstance = (Get-WmiObject -computer $computer Win32_Service -Filter "Name='$srv'");
    						$name = $serviceInstance.Name;
    						Write-Host "Attempting to start $name  on $computer." -foregroundcolor red
    						$serviceInstance.StartService() | Out-Null;
    						# Refresh the object instance so we get new data
    						$serviceInstance = (Get-WmiObject -computer $computer Win32_Service -Filter "Name='$srv'");
    						$state = $serviceInstance.State;
    						Write-Host "$name is ""$state"" on  $computer." -foregroundcolor Cyan;		
    					}
    				}
    			}
    		}
    	}
    }
    George HowarthUser is Offline
    Basic Member
    Basic Member
    Posts:360
    Avatar

    --
    20 Sep 2010 01:55 PM

    I'm assuming that you're only interested in the services that are declared in array. If so, try this, you just need to edit the last line to output an XML file:

    trap [Exception]
    {
     Write-Error -Message ("TRAPPED: " + $_.Exception.Message)
    }

    $logfile = "D:\citrixout.log"

    Clear-Host

    $computers = Get-Content -Path "D:\Computers.txt"
    $start = $true

    $outputServices = @()

    $serviceNames = @(
        'CitrixXTEServer',
        'CitrixWMIService',
        'Citrix 64-bit Virtual Memory Optimization',
        'Citrix Encryption Service',
        'Citrix Encryption Service',
        'Citrix SMA Service',
        'Citrix Virtual Memory Optimization',
        'CitrixHealthMon',
        'IMAService'
    )

    foreach ($computer in $computers)
    {
     Write-Host "Checking $computer" -ForegroundColor Black -BackgroundColor Blue | Out-File -FilePath $logfile -Append -NoClobber
     $allServices = Get-Service -ComputerName $computer
     
     foreach ($service in $allServices)
     {
      foreach ($serviceName in $serviceNames)
      {
       if ($service.Name -eq $serviceName)
       {
        Write-Host $testing "$serviceName is present on $computer." -ForegroundColor Green
                   
        if ($service.State -eq "Running")
        {
         Write-Host "$serviceName is running on $computer" -ForegroundColor Green
        }
        else
        {
         Write-Host "$serviceName is not running on $computer" -ForegroundColor Yellow
                       
         if ($start -eq $true)
         {
                            Write-Host "Attempting to start $serviceName on $computer." -ForegroundColor Red
                            Start-Service -Name $serviceName
                            $s = Get-Service -Name $serviceName
          Write-Host '$($s.Name) is "$($s.State)" on $computer.' -ForegroundColor Cyan
         }
        }
                   
                    $outputServices += Get-Service -Name $serviceName
       }
      }
     }
    }

    $outputServices | Out-GridView
    (ConvertTo-XML -InputObject $outputServices -As Document).Save("Your_File_Name.xml")

    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer