May 042013
 

I was having a conversation with a colleague the other day about the many different ways to use Powershell and get-wmiobject to quickly tap into any WMI Class without having to source the work to vbscript. Gwmi gives you the opportunity to specify parameters and allow one to quickly attach to a remote PC or server and gather information about any class. Here is an example which will allow you to step through a given class on a remote computer and display the results to console.

  • Lets step through first and then provide the command line:
    • Open up command line > start > run > type in “cmd” and click on “OK”
    • type in “Powershell” and press enter
    • type in “gwmi -computer remoteHostname” without the quotes and press enter. The -computer parameter will allow us to specify the host we want to attach to.
    • You will now be prompted to enter a class. For this example type in “win32_computersystem” and press enter
    • Assuming the account you are using has rights to the remote computer/server, you will be displayed with information pertaining to the Win32_computersystem class (domain, manufacturer, model, name, primary owner name, total physical memory).
    • Now if you dont want to step through the code, simply key in the following command at the PS> prompt.
      • gwmi -class Win32_ComputerSystem -computer remotehostname
      • gwmi -class Win32_ComputerSystem -computer remotehostname -credential domain\LanID <– this will allow you to specify a specific user account to authenticate against.

Combining Powershell and WMI can be very useful. Add a dash of a hashtable and loop though a series of hosts and you have a recipe for a great looking script.

Aug 222012
 

Reading text files is pretty straightforward with Get-Content:

PS C:\> get-content boot.ini
 [boot loader]
 timeout=15
 default=multi(0)disk(0)rdisk(0)partition(2)\WINDOWS
 [operating systems]
 multi(0)disk(0)rdisk(0)partition(2)\WINDOWS="Microsoft Windows XP
 Professional"
 /fastdetect /NoExecute=OptIn
 multi(0)disk(0)rdisk(0)partition(1)\WINDOWS="Windows Server 2003,
 Enterprise" /
 noexecute=optout /fastdetect
 C:\CMDCONS\BOOTSECT.DAT="Microsoft Windows Recovery Console" /cmdcons
 PS C:\

Continue reading “using powershell to read text files” »

 Posted by at 10:21 pm
Aug 222012
 

PowerShell offers an intriguing option for getting Web content to a PowerShell session. By using a combination of the .NET Web client object and PowerShell’s native support for XML, it is a snap to pull data from a Web site and display it in a PowerShell console. As an example, the following script queries Microsoft’s basic RSS feed for security bulletins and displays bulletin information in a color coded console:

#GetSecurityRSS.ps1
 #Query Microsoft's Basic Security Feed for latest bulletins
 #Critical bulletins will be displayed in Red
 #Important bulletins will be display in Yellow
 #Everything else will be displayed in Green

 [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Web")
 $webclient = new-object System.Net.WebClient
 $url="http://www.microsoft.com/technet/security/bulletin/secrss.aspx"
 ## Get the web page into a single string
 $data =[xml]$webclient.downloadstring($url)

 if ($data -ne $Null) {
 Write-Host -backgroundcolor Yellow -foregroundcolor blue `
 $data.rss.channel.Title
 Write-Host "Last Updated" $data.rss.channel.LastBuildDate `n
 $i=0
 do {
 write-Host -foregroundcolor White `
 $data.rss.channel.item[$i].Title
 #color code description based on severity
 if ($data.rss.channel.item[$i].Description `
 -Like "*Rating:Critical*") {
 $color="Red"
 }
 elseif ($data.rss.channel.item[$i].Description `
 -Like "*Rating:Important*"){
 $color="Yellow"
 }
 else {
 $color="Green"
 }
 Write-Host -foregroundcolor $color `
 $data.rss.channel.item[$i].Description `n
 $i++
 }
 until ($i -gt ($data.rss.channel.item).count)
 }
 else {
 Write-Host -foregroundcolor Red "Could not get " $url
 }
 Posted by at 9:54 pm

Optimized by SEO Ultimate