Powershell: Export Installed Software to Excel


image

I created the next script to inventory the installed software on my servers. The only thing you need to create is a text file with all your servers in it and save it to Servers.txt.

#Create a new Excel object using COM
$Excel = New-Object -ComObject Excel.Application
$Excel.visible = $True
$Excel.SheetsInNewWorkbook = @(get-content "C:\Scripts\PS\Servers.txt").count

#Counter variable for rows
$i = 1

#Read thru the contents of the Servers.txt file
foreach ($server in get-content "C:\Scripts\PS\Servers.txt")
{
    $Excel = $Excel.Workbooks.Add()
    $Sheet = $Excel.Worksheets.Item($i++)
    $Sheet.Name = $server

    $intRow = 1

    # Send a ping to verify if the Server is online or not.
    $ping = Get-WmiObject `
    -query "SELECT * FROM Win32_PingStatus WHERE Address = '$server'"
       if ($Ping.StatusCode -eq 0) {

         #Create column headers
         $Sheet.Cells.Item($intRow,1) = "NAME:"
         $Sheet.Cells.Item($intRow,2) = $server.ToUpper()
         $Sheet.Cells.Item($intRow,1).Font.Bold = $True
         $Sheet.Cells.Item($intRow,2).Font.Bold = $True

         $intRow++

         $Sheet.Cells.Item($intRow,1) = "APPLICATION"
         $Sheet.Cells.Item($intRow,2) = "VERSION"

             #Format the column headers
             for ($col = 1; $col –le 2; $col++)
             {
                  $Sheet.Cells.Item($intRow,$col).Font.Bold = $True
                  $Sheet.Cells.Item($intRow,$col).Interior.ColorIndex = 48
                  $Sheet.Cells.Item($intRow,$col).Font.ColorIndex = 34
             }

             $intRow++

             $software = Get-WmiObject `
             -ComputerName $server -Class Win32_Product | Sort-Object Name 

             #Formatting using Excel

             foreach ($objItem in $software){
                $Sheet.Cells.Item($intRow, 1) = $objItem.Name
                $Sheet.Cells.Item($intRow, 2) = $objItem.Version

                   $intRow ++
             }

        $Sheet.UsedRange.EntireColumn.AutoFit()
    }
}

Clear

The following Excel file will be created. For each server in Servers.txt, there will be added a worksheet with the name of the server.

image

Advertisement

How To: Enable Remote WMI support in ISA 2004


 image

Ik wilde via een script WMI aanroepen op een van de ISA 2004 Servers maar dat werd uiteraard geblokkeerd. Na wat zoeken op Google, kwam ik de onderstaande oplossing tegen. Deze oplossing werkt perfect.

1.First you need to make explicict range form dcom high ports you can use via in the registry (see http://support.microsoft.com/?kbid=154596)
HKEY_LOCAL_MACHINE\Software\Microsoft\Rpc\Internet 
Edit the Ports multi-string to your liking. I use 5000-5100, this should be fine amount for a non application server.(see kb above)
Ports 5000-5100 (multi-string)

2. create two basic custom protocols for SMB and dcom,
cust_smb
445 tcp outbound
445 udp send
(no related application filters ticked!)

cust_dcom
135 tcp outbound
5000-5100 tcp outbound
(no related application filters ticked!)

3. create the rule, allow, source = trusted admin/monitor box(es), destination localhost, protocols: cust_smb, cust_dcom, all users

4. Edit the System policy
Untick the ‘enable’ for Microsoct Management Console, you don’t need it now because we have created a better rule for our trusted box(es) ( note having this ticked will create a hidden rule that can break wmi scripts and alike).
Untick the ‘force strict rpc compliance’ option for Active Dicrectory
Click ok, apply new configuration, restart the isa server

Je kunt ook de onderstaande reg file gebruiken i.p.v. stap 1 uit te voeren.

RPC_Ports.reg:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Rpc\Internet]
"Ports"=hex(7):35,00,30,00,30,00,30,00,2d,00,35,00,31,00,30,00,30,00,00,00,00,\
  00
"PortsInternetAvailable"="Y"
"UseInternetPorts"="Y"

Bron: http://forums.isaserver.org/m_410001100/mpage_1/key_/tm.htm#2002017878

Powershell: Check if KB patch is installed


Ik wilde weten of een bepaald KB nummer was geïnstalleerd op mijn VM’s. Dit wilde ik natuurlijk niet met het handje nakijken en heb hiervoor een script bij elkaar geraapt.

Het onderstaande script kun je gebruiken in een VMware VI omgeving.

#Add-PSSnapIn VMware.VimAutomation.Core

# Connect to vCenter
$VC = Connect-VIServer (Read-Host "Enter vCenter server")

# Enter the KB#
$HotFixID = (Read-Host "Enter KB#")
Write-Host

# Get all VM’s with powerstate = PoweredOn
$VMS = Get-VM | Where {$_.PowerState -eq "PoweredOn"} | Sort Name

ForEach ($VM in $VMS)

{
# Process only Windows Server VM’s
if ($VM.Guest.OSFullName -match "Microsoft Windows Server*")
  { 
 
# Get all the info using WMI
  $results = get-wmiobject -class "Win32_QuickFixEngineering" -namespace "root\CIMV2" -ComputerName $VM 
  
    # Loop through $results and look for a match then output to screen
    foreach ($objItem in $results)
            {
                # Do the match
                if ($objItem.HotFixID -match $HotFixID)
                    {
                    write-host $objItem.CSName
                    write-host "Hotfix "$HotFixID" installed"
                    write-host
                    }
            } 
   }
}
# Disconnect from vCenter
Disconnect-VIServer -Confirm:$False

 

Zodra je het script start, word er gevraagd naar de vCenter server en het KB nummer wat je wilt nakijken.

image

De volgende output wordt gegenereerd:

image

 

Je kunt het onderstaande script gebruiken in een omgeving zonder VI3:

# Get content
$computers = get-content c:\computers.txt

# Get all the info using WMI
$results = get-wmiobject -class “Win32_QuickFixEngineering” -namespace “root\CIMV2″ -computername $computers

# Loop through $results and look for a match then output to screen
foreach ($objItem in $results)
{
    if ($objItem.HotFixID -match “KB932168″)
    {
        write-host $objItem.CSName
        write-host “Hotfix KB932168 installed”
        write-host
    }
}

 

Source: http://techittome.wordpress.com/2007/04/12/windows-powershell-script-to-check-for-specific-hotfix/

Citrix: Script for logging off disconnected users


Ik kwam het volgende script tegen in een post van Joe Shonk op het forum van Brianmadden.com.

On Error Resume Next

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
Const cActive = 0
Const cDisconnected = 4
Const strComputer = “.”

Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\Citrix”)
Set colItems = objWMIService.ExecQuery(“Select * from Metaframe_Session Where sessionstate = ” & cDisconnected)

For Each objItem In colItems
if (objItem.SessionID > 0) and (objItem.SessionID < 65530) then
objItem.Logoff
end if
Next

Set objWMIService = Nothing

Dit script zorgt er voor dat alle Sessions die nog op Disconnected staan, netjes worden afgesloten. Dit script is handig om te draaien voor dat een Citrix server opnieuw wordt opgestart.

Citrix: How to use WMI Providers


In deze post schrijf ik mijn eerste ervaring met WMI. De tool waar ik gebruik van maakte was ‘wbemtest.exe’. Deze start je als volgt op: Start – Run – wbemtest.exe

Het onderstaande scherm verschijnt.

image

Druk op connect en vul bij de Namespace het volgende in: ‘root\citrix’ en druk op connect.

image

Nu kun je via ‘Query..’ een test uitvoeren. Bijvoorbeeld: ‘Select * from Metaframe_Session’.

image

De volgende output wordt gegenereerd. Dit zijn de active sessions op deze Terminal / Citrix server.

image

Het kan ook voorkomen dat je een foutmelding krijgt bij het connecten naar de ‘root\citrix’ Namespace.  Deze foutmelding treed op als je de WMI Providers niet hebt geïnstalleerd tijdens de installatie van Citrix Presentation Server (XenApp).

image

Dit kun je oplossen door in je service window de WMI Providers als nog te installeren. Dit kan alleen als er geen gebruikers ingelogt zijn.

image

Meer informatie vind je hier: CTX108365