Network Tools v2


posted by Felipe
10-21-2010

Downloads: 1,810
File size: 11.1kB
Views: 5,646

Embed
Network Tools v2
  1. # Edit This item to change the DropDown Values 
  2. [array]$DropDownArray = "ping", "nslookup", "BIOS", "Services", "Programs", "RemoteUninstall", "RemoteInstall", "MapNetworkDrive" 
  3.  
  4. # This Function Returns the Selected Value and their actions then Closes the Form 
  5. function Return-DropDown
  6.     $Choice = $DropDown.SelectedItem.ToString() 
  7.     $Address = $Address.Text 
  8.     #$Form.Close() 
  9.         if ($choice -eq "ping")  
  10.         
  11.             write-host "PING $address" 
  12.             Test-Connection $address | Out-gridview 
  13.             write-host 
  14.         
  15.         elseif ($choice -eq "nslookup")  
  16.         
  17.             write-host "NSLOOKUP $address" 
  18.             nslookup $address | Out-gridview 
  19.             write-host 
  20.         }         
  21.         elseif ($choice -eq "BIOS")  
  22.         
  23.             write-host "BIOS of $address" 
  24.             Get-WmiObject win32_bios -ComputerName $address | Out-gridview 
  25.             write-host 
  26.         }    
  27.         elseif ($choice -eq "Services")  
  28.         
  29.             write-host "Services of $address" 
  30.             Get-WmiObject win32_service -ComputerName $address | Out-gridview 
  31.             write-host 
  32.         }    
  33.         elseif ($choice -eq "Programs")  
  34.         
  35.             write-host "Programs installed on $address" 
  36.             Get-WmiObject win32_product -ComputerName $address | Out-gridview 
  37.             write-host 
  38.         }         
  39.         elseif ($choice -eq "RemoteUninstall")  
  40.         
  41.            $objForm = New-Object System.Windows.Forms.Form  
  42.             $objForm.Text = "Data Entry Form" 
  43.             $objForm.Size = New-Object System.Drawing.Size(300,200)  
  44.             $objForm.StartPosition = "CenterScreen" 
  45.  
  46.             $objForm.KeyPreview = $True 
  47.             $objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")  
  48.                     {$y=$objTextBox.Text;$objForm.Close()}}) 
  49.             $objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")  
  50.                     {$objForm.Close()}}) 
  51.  
  52.             $OKButton = New-Object System.Windows.Forms.Button 
  53.             $OKButton.Location = New-Object System.Drawing.Size(75,120) 
  54.             $OKButton.Size = New-Object System.Drawing.Size(75,23) 
  55.             $OKButton.Text = "OK" 
  56.             $OKButton.Add_Click({$location=$objTextBox.Text;$objForm.Close()}) 
  57.             $objForm.Controls.Add($OKButton
  58.  
  59.             $CancelButton = New-Object System.Windows.Forms.Button 
  60.             $CancelButton.Location = New-Object System.Drawing.Size(150,120) 
  61.             $CancelButton.Size = New-Object System.Drawing.Size(75,23) 
  62.             $CancelButton.Text = "Cancel" 
  63.             $CancelButton.Add_Click({$objForm.Close()}) 
  64.             $objForm.Controls.Add($CancelButton
  65.  
  66.             $objLabel = New-Object System.Windows.Forms.Label 
  67.             $objLabel.Location = New-Object System.Drawing.Size(10,20)  
  68.             $objLabel.Size = New-Object System.Drawing.Size(280,20)  
  69.             $objLabel.Text = "Name of the application to be uninstalled:" 
  70.             $objForm.Controls.Add($objLabel)  
  71.              
  72.             $objLabel2 = New-Object System.Windows.Forms.Label 
  73.             $objLabel2.Location = New-Object System.Drawing.Size(10,50)  
  74.             $objLabel2.Size = New-Object System.Drawing.Size(190,20)  
  75.             $objLabel2.Text = "Don't be too generic!" 
  76.             $objForm.Controls.Add($objLabel2)  
  77.  
  78.             $objTextBox = New-Object System.Windows.Forms.TextBox  
  79.             $objTextBox.Location = New-Object System.Drawing.Size(10,80)  
  80.             $objTextBox.Size = New-Object System.Drawing.Size(260,20)  
  81.             $objForm.Controls.Add($objTextBox)  
  82.  
  83.             $objForm.Topmost = $True 
  84.  
  85.             $objForm.Add_Shown({$objForm.Activate()}) 
  86.             [void] $objForm.ShowDialog() 
  87.              
  88.             Try{ 
  89.             $app = Get-WmiObject win32_product -ComputerName $Address -ErrorAction SilentlyContinue | Where-Object {$_.name -match $location
  90.             $returnvalue = $app.uninstall() | Select-Object -Property returnvalue -ErrorAction SilentlyContinue 
  91.              
  92.                 if($returnvalue.returnvalue -eq "0"
  93.                     {[Windows.Forms.MessageBox]::Show("Installation was successful!")} 
  94.                      
  95.                 else 
  96.                     {[Windows.Forms.MessageBox]::Show("Installation was not successful!")}} 
  97.                      
  98.             Catch{ 
  99.                 if($error[0] -match "The RPC server is unavailable" -or $error[0] -match "null-valued"){ 
  100.                     [Windows.Forms.MessageBox]::Show("Computer is unreachable, name is invalid or application does not exist.")}} 
  101.         }    
  102.         elseif ($choice -eq "RemoteInstall")  
  103.         {     
  104.                  
  105.                $objForm = New-Object System.Windows.Forms.Form  
  106.             $objForm.Text = "Data Entry Form" 
  107.             $objForm.Size = New-Object System.Drawing.Size(300,200)  
  108.             $objForm.StartPosition = "CenterScreen" 
  109.  
  110.             $objForm.KeyPreview = $True 
  111.             $objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")  
  112.                     {$y=$objTextBox.Text;$objForm.Close()}}) 
  113.             $objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")  
  114.                     {$objForm.Close()}}) 
  115.  
  116.             $OKButton = New-Object System.Windows.Forms.Button 
  117.             $OKButton.Location = New-Object System.Drawing.Size(75,120) 
  118.             $OKButton.Size = New-Object System.Drawing.Size(75,23) 
  119.             $OKButton.Text = "OK" 
  120.             $OKButton.Add_Click({$location=$objTextBox.Text;$objForm.Close()}) 
  121.             $objForm.Controls.Add($OKButton
  122.  
  123.             $CancelButton = New-Object System.Windows.Forms.Button 
  124.             $CancelButton.Location = New-Object System.Drawing.Size(150,120) 
  125.             $CancelButton.Size = New-Object System.Drawing.Size(75,23) 
  126.             $CancelButton.Text = "Cancel" 
  127.             $CancelButton.Add_Click({$objForm.Close()}) 
  128.             $objForm.Controls.Add($CancelButton
  129.  
  130.             $objLabel = New-Object System.Windows.Forms.Label 
  131.             $objLabel.Location = New-Object System.Drawing.Size(10,20)  
  132.             $objLabel.Size = New-Object System.Drawing.Size(280,20)  
  133.             $objLabel.Text = "UNC path for the application to be installed:" 
  134.             $objForm.Controls.Add($objLabel)  
  135.              
  136.             $objLabel2 = New-Object System.Windows.Forms.Label 
  137.             $objLabel2.Location = New-Object System.Drawing.Size(10,50)  
  138.             $objLabel2.Size = New-Object System.Drawing.Size(190,20)  
  139.             $objLabel2.Text = "Eg: \\computername\c$\firefox.msi" 
  140.             $objForm.Controls.Add($objLabel2)  
  141.  
  142.             $objTextBox = New-Object System.Windows.Forms.TextBox  
  143.             $objTextBox.Location = New-Object System.Drawing.Size(10,80)  
  144.             $objTextBox.Size = New-Object System.Drawing.Size(260,20)  
  145.             $objForm.Controls.Add($objTextBox)  
  146.  
  147.             $objForm.Topmost = $True 
  148.  
  149.             $objForm.Add_Shown({$objForm.Activate()}) 
  150.             [void] $objForm.ShowDialog() 
  151.              
  152.             Try{ 
  153.             $returnvalue = (Get-WmiObject -ComputerName $Address -List -ErrorAction SilentlyContinue | Where-Object -FilterScript {$_.Name -eq "win32_product"}).install($location) | Select-Object -Property returnvalue 
  154.                  
  155.                 if($returnvalue.returnvalue -eq "0"
  156.                     {[Windows.Forms.MessageBox]::Show("Installation was successful!")} 
  157.                  
  158.                 else 
  159.                     {[Windows.Forms.MessageBox]::Show("Installation was not successful!")} 
  160.             
  161.             Catch{ 
  162.                 if($error[0] -match "The RPC server is unavailable" -or $error[0] -match "null-valued"){ 
  163.                     [Windows.Forms.MessageBox]::Show("Computer is unreachable, name is invalid or application does not exist.")}} 
  164.              
  165.         
  166.         elseif ($Choice -eq "MapNetworkDrive"
  167.         
  168.              
  169.             $objForm = New-Object System.Windows.Forms.Form  
  170.             $objForm.Text = "Data Entry Form" 
  171.             $objForm.Size = New-Object System.Drawing.Size(300,200)  
  172.             $objForm.StartPosition = "CenterScreen" 
  173.  
  174.             $objForm.KeyPreview = $True 
  175.             $objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")  
  176.                     {$y=$objTextBox.Text;$objForm.Close()}}) 
  177.             $objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")  
  178.                     {$objForm.Close()}}) 
  179.  
  180.             $OKButton = New-Object System.Windows.Forms.Button 
  181.             $OKButton.Location = New-Object System.Drawing.Size(75,120) 
  182.             $OKButton.Size = New-Object System.Drawing.Size(75,23) 
  183.             $OKButton.Text = "OK" 
  184.             $OKButton.Add_Click({$y=$objTextBox.Text;$objForm.Close()}) 
  185.             $objForm.Controls.Add($OKButton
  186.  
  187.             $CancelButton = New-Object System.Windows.Forms.Button 
  188.             $CancelButton.Location = New-Object System.Drawing.Size(150,120) 
  189.             $CancelButton.Size = New-Object System.Drawing.Size(75,23) 
  190.             $CancelButton.Text = "Cancel" 
  191.             $CancelButton.Add_Click({$objForm.Close()}) 
  192.             $objForm.Controls.Add($CancelButton
  193.  
  194.             $objLabel = New-Object System.Windows.Forms.Label 
  195.             $objLabel.Location = New-Object System.Drawing.Size(10,20)  
  196.             $objLabel.Size = New-Object System.Drawing.Size(280,20)  
  197.             $objLabel.Text = "Letter of new Network Drive:" 
  198.             $objForm.Controls.Add($objLabel)  
  199.  
  200.             $objTextBox = New-Object System.Windows.Forms.TextBox  
  201.             $objTextBox.Location = New-Object System.Drawing.Size(10,40)  
  202.             $objTextBox.Size = New-Object System.Drawing.Size(260,20)  
  203.             $objForm.Controls.Add($objTextBox)  
  204.  
  205.             $objForm.Topmost = $True 
  206.  
  207.             $objForm.Add_Shown({$objForm.Activate()}) 
  208.             [void] $objForm.ShowDialog() 
  209.              
  210.             $x = $y 
  211.  
  212.             $objTextBox.clear() 
  213.             $objLabel.Text = "UNC:" 
  214.             $objForm.Controls.Add($objLabel
  215.             $objForm.Controls.Add($objTextBox)  
  216.             $objForm.Add_Shown({$objForm.Activate()}) 
  217.             [void] $objForm.ShowDialog() 
  218.  
  219.             $map = New-Object -ComObject wscript.network 
  220.                 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.")}}} 
  221.                 else 
  222.                 {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.")}}} 
  223.  
  224.  
  225.             
  226.  
  227. [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms"
  228. [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing"
  229.  
  230. $Form = New-Object System.Windows.Forms.Form 
  231.  
  232. $Form.width = 300 
  233. $Form.height = 250 
  234. $Form.Text = "Network Tools" 
  235. $Form.maximumsize = New-Object System.Drawing.Size(300,250) 
  236. $Form.startposition = "centerscreen" 
  237. $Form.KeyPreview = $True 
  238. $Form.Add_KeyDown({if ($_.KeyCode -eq "Enter")  
  239.     {return-dropdown}}) 
  240. $Form.Add_KeyDown({if ($_.KeyCode -eq "Escape")  
  241.     {$Form.Close()}}) 
  242.  
  243.  
  244.  
  245. $DropDown = new-object System.Windows.Forms.ComboBox 
  246. $DropDown.Location = new-object System.Drawing.Size(100,10) 
  247. $DropDown.Size = new-object System.Drawing.Size(130,30) 
  248.  
  249. ForEach ($Item in $DropDownArray) { 
  250.     $DropDown.Items.Add($Item
  251.  
  252. $Form.Controls.Add($DropDown
  253.  
  254.  
  255. $DropDownLabel = new-object System.Windows.Forms.Label 
  256. $DropDownLabel.Location = new-object System.Drawing.Size(10,10) 
  257. $DropDownLabel.size = new-object System.Drawing.Size(100,20) 
  258. $DropDownLabel.Text = "Command" 
  259. $Form.Controls.Add($DropDownLabel
  260.  
  261. $Button = new-object System.Windows.Forms.Button 
  262. $Button.Location = new-object System.Drawing.Size(100,150) 
  263. $Button.Size = new-object System.Drawing.Size(100,20) 
  264. $Button.Text = "OK" 
  265. $Button.Add_Click({Return-DropDown}) 
  266. $form.Controls.Add($Button
  267.  
  268. $address = new-object System.Windows.Forms.TextBox 
  269. $address.Location = new-object System.Drawing.Size(100,100) 
  270. $address.Size = new-object System.Drawing.Size(100,20) 
  271.  
  272. $Form.Controls.Add($address
  273.  
  274. $addresslabel = new-object System.Windows.Forms.Label 
  275. $addresslabel.Location = new-object System.Drawing.Size(10,100) 
  276. $addresslabel.size = new-object System.Drawing.Size(100,20) 
  277. $addresslabel.Text = "Computer" 
  278. $Form.Controls.Add($addresslabel
  279.  
  280. $authorlabel = new-object System.Windows.Forms.Label 
  281. $authorlabel.Location = new-object System.Drawing.Size(160,185) 
  282. $authorlabel.size = new-object System.Drawing.Size(200,15) 
  283. $authorlabel.Text = "Powered by F. Binotto." 
  284. $Form.Controls.Add($authorlabel
  285.  
  286. $Form.Add_Shown({$Form.Activate()}) 
  287. $Form.ShowDialog() 

Now all integrated and with good error handling.

Ping

Nslookup

Get BIOS information

Get Services

Get programs installed

Perform a remote uninstall

Perform a remote install

Map a network drive

Comments

docman95 wrote re: Network Tools v2
on 11-06-2010 1:03 PM

très bon script

docman95 wrote re: Network Tools v2
on 11-15-2010 6:47 AM

Je me suis permis de modifier ce code en fonction de mes besoin et ajouter l'option de récupérer les membres du groupe admin local, du serveurs que l'on sélectionne.

Felipe wrote re: Network Tools v2
on 11-15-2010 1:10 PM

Sure you can!

Copyright 2012 PowerShell.com. All rights reserved.