Solved

Modification of powershell script

Posted on 2014-03-26
2
495 Views
Last Modified: 2014-03-31
This is one really great script, extremely useful. I'm just getting into power shell and loving it.  I would love to increase the usefulness of this script to add the following features:

Retrieve Hot fixes installed on local or remote machine indicated:  Get-Hotfix - Description Update or Get-HotFix –ComputerName localhost
Identification of Processes using the most memory: ps | sort -p ws | select -last 5

System Information ie. OS info, Hard Drive Space/ Free space available, memory.  

This is really a great. Unless there is another one out there that includes this information already.  Would love to have this incorporated
NetworkTools.txt
0
Comment
Question by:cgooden01
2 Comments
 
LVL 10

Accepted Solution

by:
JoeKlimis earned 500 total points
ID: 39963018
Hi


I think this covers most of what  you required.


# Edit This item to change the DropDown Values
[array]$DropDownArray = "Disc Space","Computer Stats", "ping", "nslookup", "BIOS", "Services", "Programs", "RemoteUninstall", "RemoteInstall", "MapNetworkDrive","Hotfix-Updates","Hotfixs","Top 5 Memory"
function Get-ComputerStats 
{
  param(
    [Parameter(Mandatory=$true, Position=0, 
               ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
    [ValidateNotNull()]
    [string[]]$ComputerName
  )

  process {
    foreach ($c in $ComputerName) 
	{
        $avg = Get-WmiObject win32_processor -computername $c | 
                   Measure-Object -property LoadPercentage -Average | 
                   Foreach {$_.Average}
        $mem = Get-WmiObject win32_operatingsystem -ComputerName $c |
                   Foreach {"{0:N2}" -f ((($_.TotalVisibleMemorySize - $_.FreePhysicalMemory)*100)/ $_.TotalVisibleMemorySize)}
		$hardware = $b =  Get-WmiObject -class Win32_ComputerSystem -ComputerName $c |
						Select-Object -Property Manufacturer,Model,
                              @{n='Memory(GB)';e={$_.TotalPhysicalMemory / 1GB -as [int]}},
                              @{n='Architecture';e={$_.SystemType}},
                              @{n='Processors';e={$_.NumberOfProcessors}}

        new-object psobject -prop @{ # Work on PowerShell V2 and below
        # [pscustomobject] [ordered] @{ # Only if on PowerShell V3
			Manufacturer = $HARDWARE.Manufacturer
			Model = $hardware.model
			"Total Memory (Gb)"= $hardware."Memory(GB)"
			Architecture = $hardware.Architecture
			Processors = $hardware.Processors
            ComputerName = $c
            AverageCpu = $avg
            MemoryUsage = $mem

        }
    }
  }
}

# This Function Returns the Selected Value and their actions then Closes the Form
function Return-DropDown {
	$Choice = $DropDown.SelectedItem.ToString()
	$Address = $Address.Text
	#$Form.Close()
	
	
	switch ($choice) 
{
	Ping
		{
            write-host "PING $address"
            Test-Connection $address | Out-gridview
			break 
        }
	nslookup
{
            write-host "NSLOOKUP $address"
            nslookup $address | Out-gridview
            write-host
        }   
	Hotfix-Updates
		{ Get-HotFix -Description update -ComputerName $address| Out-gridview }
	Hotfixs
		{ Get-HotFix  -ComputerName $address| Out-gridview }
	"Top 5 Memory"
		{ 		ps -ComputerName . | sort -p ws | select -last 5 | Out-gridview }
	OSInfo 
		{	gwmi win32_operatingSystem | select name | Out-gridview 
		}
	"Disc Space"
	{ Get-WmiObject -Class win32_volume -cn $address |
       Select-Object @{LABEL='Comptuer';EXPRESSION={$address}},driveletter, label,@{LABEL='GBfreespace';EXPRESSION={"{0:N2}" -f ($_.freespace/1GB)}} | Out-gridview 
	
	}
	"Computer Stats"
	{
	 Get-ComputerStats $address | out-gridview
	}
	Bios
        {
            write-host "BIOS of $address"
            Get-WmiObject win32_bios -ComputerName $address | Out-gridview
            write-host
        }  
	Services
        {
            write-host "Services of $address"
            Get-WmiObject win32_service -ComputerName $address | Out-gridview
            write-host
        } 
	Programs
        {
            write-host "Programs installed on $address"
            Get-WmiObject win32_product -ComputerName $address | Out-gridview
            write-host
        }  
	RemoteInstall
      {	
				
           	$objForm = New-Object System.Windows.Forms.Form 
			$objForm.Text = "Data Entry Form"
			$objForm.Size = New-Object System.Drawing.Size(300,200) 
			$objForm.StartPosition = "CenterScreen"

			$objForm.KeyPreview = $True
			$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
   				 {$y=$objTextBox.Text;$objForm.Close()}})
			$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
  				  {$objForm.Close()}})

			$OKButton = New-Object System.Windows.Forms.Button
			$OKButton.Location = New-Object System.Drawing.Size(75,120)
			$OKButton.Size = New-Object System.Drawing.Size(75,23)
			$OKButton.Text = "OK"
			$OKButton.Add_Click({$location=$objTextBox.Text;$objForm.Close()})
			$objForm.Controls.Add($OKButton)

			$CancelButton = New-Object System.Windows.Forms.Button
			$CancelButton.Location = New-Object System.Drawing.Size(150,120)
			$CancelButton.Size = New-Object System.Drawing.Size(75,23)
			$CancelButton.Text = "Cancel"
			$CancelButton.Add_Click({$objForm.Close()})
			$objForm.Controls.Add($CancelButton)

			$objLabel = New-Object System.Windows.Forms.Label
			$objLabel.Location = New-Object System.Drawing.Size(10,20) 
			$objLabel.Size = New-Object System.Drawing.Size(280,20) 
			$objLabel.Text = "UNC path for the application to be installed:"
			$objForm.Controls.Add($objLabel) 
			
			$objLabel2 = New-Object System.Windows.Forms.Label
			$objLabel2.Location = New-Object System.Drawing.Size(10,50) 
			$objLabel2.Size = New-Object System.Drawing.Size(190,20) 
			$objLabel2.Text = "Eg: \\computername\c$\firefox.msi"
			$objForm.Controls.Add($objLabel2) 

			$objTextBox = New-Object System.Windows.Forms.TextBox 
			$objTextBox.Location = New-Object System.Drawing.Size(10,80) 
			$objTextBox.Size = New-Object System.Drawing.Size(260,20) 
			$objForm.Controls.Add($objTextBox) 

			$objForm.Topmost = $True

			$objForm.Add_Shown({$objForm.Activate()})
			[void] $objForm.ShowDialog()
			
			Try{
			$returnvalue = (Get-WmiObject -ComputerName $Address -List -ErrorAction SilentlyContinue | Where-Object -FilterScript {$_.Name -eq "win32_product"}).install($location) | Select-Object -Property returnvalue
				
				if($returnvalue.returnvalue -eq "0")
					{[Windows.Forms.MessageBox]::Show("Installation was successful!")}
				
				else
					{[Windows.Forms.MessageBox]::Show("Installation was not successful!")}
			}
			Catch{
				if($error[0] -match "The RPC server is unavailable" -or $error[0] -match "null-valued"){
					[Windows.Forms.MessageBox]::Show("Computer is unreachable, name is invalid or application does not exist.")}}
			
        }
	RemoteUninstall
      {
           $objForm = New-Object System.Windows.Forms.Form 
			$objForm.Text = "Data Entry Form"
			$objForm.Size = New-Object System.Drawing.Size(300,200) 
			$objForm.StartPosition = "CenterScreen"

			$objForm.KeyPreview = $True
			$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
   				 {$y=$objTextBox.Text;$objForm.Close()}})
			$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
  				  {$objForm.Close()}})

			$OKButton = New-Object System.Windows.Forms.Button
			$OKButton.Location = New-Object System.Drawing.Size(75,120)
			$OKButton.Size = New-Object System.Drawing.Size(75,23)
			$OKButton.Text = "OK"
			$OKButton.Add_Click({$location=$objTextBox.Text;$objForm.Close()})
			$objForm.Controls.Add($OKButton)

			$CancelButton = New-Object System.Windows.Forms.Button
			$CancelButton.Location = New-Object System.Drawing.Size(150,120)
			$CancelButton.Size = New-Object System.Drawing.Size(75,23)
			$CancelButton.Text = "Cancel"
			$CancelButton.Add_Click({$objForm.Close()})
			$objForm.Controls.Add($CancelButton)

			$objLabel = New-Object System.Windows.Forms.Label
			$objLabel.Location = New-Object System.Drawing.Size(10,20) 
			$objLabel.Size = New-Object System.Drawing.Size(280,20) 
			$objLabel.Text = "Name of the application to be uninstalled:"
			$objForm.Controls.Add($objLabel) 
			
			$objLabel2 = New-Object System.Windows.Forms.Label
			$objLabel2.Location = New-Object System.Drawing.Size(10,50) 
			$objLabel2.Size = New-Object System.Drawing.Size(190,20) 
			$objLabel2.Text = "Don't be too generic!"
			$objForm.Controls.Add($objLabel2) 

			$objTextBox = New-Object System.Windows.Forms.TextBox 
			$objTextBox.Location = New-Object System.Drawing.Size(10,80) 
			$objTextBox.Size = New-Object System.Drawing.Size(260,20) 
			$objForm.Controls.Add($objTextBox) 

			$objForm.Topmost = $True

			$objForm.Add_Shown({$objForm.Activate()})
			[void] $objForm.ShowDialog()
			
			Try{
			$app = Get-WmiObject win32_product -ComputerName $Address -ErrorAction SilentlyContinue | Where-Object {$_.name -match $location}
			$returnvalue = $app.uninstall() | Select-Object -Property returnvalue -ErrorAction SilentlyContinue
			
				if($returnvalue.returnvalue -eq "0")
					{[Windows.Forms.MessageBox]::Show("Installation was successful!")}
					
				else
					{[Windows.Forms.MessageBox]::Show("Installation was not successful!")}}
					
			Catch{
				if($error[0] -match "The RPC server is unavailable" -or $error[0] -match "null-valued"){
					[Windows.Forms.MessageBox]::Show("Computer is unreachable, name is invalid or application does not exist.")}}
        }   
	MapnetworkDrive
		{
			
			$objForm = New-Object System.Windows.Forms.Form 
			$objForm.Text = "Data Entry Form"
			$objForm.Size = New-Object System.Drawing.Size(300,200) 
			$objForm.StartPosition = "CenterScreen"

			$objForm.KeyPreview = $True
			$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
   				 {$y=$objTextBox.Text;$objForm.Close()}})
			$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
  				  {$objForm.Close()}})

			$OKButton = New-Object System.Windows.Forms.Button
			$OKButton.Location = New-Object System.Drawing.Size(75,120)
			$OKButton.Size = New-Object System.Drawing.Size(75,23)
			$OKButton.Text = "OK"
			$OKButton.Add_Click({$y=$objTextBox.Text;$objForm.Close()})
			$objForm.Controls.Add($OKButton)

			$CancelButton = New-Object System.Windows.Forms.Button
			$CancelButton.Location = New-Object System.Drawing.Size(150,120)
			$CancelButton.Size = New-Object System.Drawing.Size(75,23)
			$CancelButton.Text = "Cancel"
			$CancelButton.Add_Click({$objForm.Close()})
			$objForm.Controls.Add($CancelButton)

			$objLabel = New-Object System.Windows.Forms.Label
			$objLabel.Location = New-Object System.Drawing.Size(10,20) 
			$objLabel.Size = New-Object System.Drawing.Size(280,20) 
			$objLabel.Text = "Letter of new Network Drive:"
			$objForm.Controls.Add($objLabel) 

			$objTextBox = New-Object System.Windows.Forms.TextBox 
			$objTextBox.Location = New-Object System.Drawing.Size(10,40) 
			$objTextBox.Size = New-Object System.Drawing.Size(260,20) 
			$objForm.Controls.Add($objTextBox) 

			$objForm.Topmost = $True

			$objForm.Add_Shown({$objForm.Activate()})
			[void] $objForm.ShowDialog()
			
			$x = $y

			$objTextBox.clear()
			$objLabel.Text = "UNC:"
			$objForm.Controls.Add($objLabel)
			$objForm.Controls.Add($objTextBox) 
			$objForm.Add_Shown({$objForm.Activate()})
			[void] $objForm.ShowDialog()

			$map = New-Object -ComObject wscript.network
				if($x -match ":"){Try{$map.MapNetworkDrive($x,$y)}Catch{if($error[0] -match "The local device"){[Windows.Forms.MessageBox]::Show("The local device is already in use.")}else{[Windows.Forms.MessageBox]::Show("The network name cannot be found.")}}}
				else
				{Try{$x = $x+":"; $map.MapNetworkDrive($x,$y)}Catch{if($error[0] -match "The local device"){[Windows.Forms.MessageBox]::Show("The local device is already in use.")}else{[Windows.Forms.MessageBox]::Show("The network name cannot be found.")}}}

			}		
	default { throw "No matching environment for $choice on computer $address" }
}

  	

  

  


}

[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

$Form = New-Object System.Windows.Forms.Form

$Form.width = 300
$Form.height = 250
$Form.Text = "Network Tools"
$Form.maximumsize = New-Object System.Drawing.Size(300,250)
$Form.startposition = "centerscreen"
$Form.KeyPreview = $True
$Form.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
    {return-dropdown}})
$Form.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
    {$Form.Close()}})



$DropDown = new-object System.Windows.Forms.ComboBox
$DropDown.Location = new-object System.Drawing.Size(100,10)
$DropDown.Size = new-object System.Drawing.Size(130,30)

ForEach ($Item in $DropDownArray) {
	$DropDown.Items.Add($Item)
}

$Form.Controls.Add($DropDown)


$DropDownLabel = new-object System.Windows.Forms.Label
$DropDownLabel.Location = new-object System.Drawing.Size(10,10)
$DropDownLabel.size = new-object System.Drawing.Size(100,20)
$DropDownLabel.Text = "Command"
$Form.Controls.Add($DropDownLabel)

$Button = new-object System.Windows.Forms.Button
$Button.Location = new-object System.Drawing.Size(100,150)
$Button.Size = new-object System.Drawing.Size(100,20)
$Button.Text = "OK"
$Button.Add_Click({Return-DropDown})
$form.Controls.Add($Button)

$address = new-object System.Windows.Forms.TextBox
$address.Location = new-object System.Drawing.Size(100,100)
$address.Size = new-object System.Drawing.Size(100,20)

$Form.Controls.Add($address)

$addresslabel = new-object System.Windows.Forms.Label
$addresslabel.Location = new-object System.Drawing.Size(10,100)
$addresslabel.size = new-object System.Drawing.Size(100,20)
$addresslabel.Text = "Computer"
$Form.Controls.Add($addresslabel)

$authorlabel = new-object System.Windows.Forms.Label
$authorlabel.Location = new-object System.Drawing.Size(160,185)
$authorlabel.size = new-object System.Drawing.Size(200,15)
$authorlabel.Text = "Powered by F. Binotto."
$Form.Controls.Add($authorlabel)

$Form.Add_Shown({$Form.Activate()})
$Form.ShowDialog()

Open in new window

0
 

Author Comment

by:cgooden01
ID: 39966581
Great. That worked perfectly.  Thanks for the reply back.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Synchronize a new Active Directory domain with an existing Office 365 tenant
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
This tutorial will give a an overview on how to deploy remote agents in Backup Exec 2012 to new servers. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as connecting to a remote Back…
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…

774 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question