Wednesday, December 17, 2014

Parsing Log File with PowerShell

Interesting article

http://matthewyarlett.blogspot.co.uk/2014/11/quick-and-dirty-parsing-logs-with.html

here is the code for reference

# Set the path to the log files            
$path = "C:\Temp\Logs"
# Get a collection of all the log files (anything ending in .log)
$files = Get-ChildItem -Path $path -Filter "*.log"
# Pipe the collection of log files to the ForEach-Object cmdlet
# (the alias of ForEach-Object is %)
$files | %{
# Call the OpenText method, to return a System.IO.StreamReader object
$file = $_.OpenText();
# Record the current line number (to use in the console output)
$lineNum = 1;
Write-Host "Checking file"$_.Name -f Yellow;
# Use the EndOfStream method (which returns true when you have reach the end
# of the file), read each line of the file.
while($file.EndOfStream -ne $true)
{
# Read the next line in the file
$line = $file.ReadLine();
if($line -ne $null)
{
# Use the String ToLower and Contains methods to check for occurances
# of the strings (or values) you need to check the file for
# In this example, I'm looking for any instances of the text "error" or "exception"
if($line.ToLower().Contains("error") -or $line.ToLower().Contains("exception"))
{
# If the current lines contains a match, write the line number
# and line text out to the console
Write-Host "Line: $lineNum " -NoNewline -ForegroundColor Green;
Write-Host $line -f Red;
}
}
# Increment the line number
$lineNum++;
}
}

Wednesday, July 2, 2014

ISE–PowerShell Integrated Scripting Environment

Here is some fun with the Windows PowerShell Integrated Scripting Environment or ISE

Start ISE

simply type ise

Start ISE as administrator

start-process PowerShell_ISE –verb RunAs

Customize ISE for SharePoint

http://thecloudengineer.blogspot.com/2011/10/create-ise-enabled-for-sharepoint.html

Configuring the PowerShell ISE for use with Git and GitHub


http://mikefrobbins.com/2016/02/09/configuring-the-powershell-ise-for-use-with-git-and-github/

Tuesday, December 3, 2013

Remove PowerShell Object

 

I was working with SharePoint and found an object that I needed to delete

image

NOTE that lega… is the friendly name.  Well not really.  On close examination appears that the name was corrupt.  The name was lega … with more info.

image

So using a where-object I gathered a couple of objects and saved as an array.

image

Then deleted the first object in the array using the name property.

image

HTH

Wednesday, May 22, 2013

Hyper-V PowerShell


UPDATE … more from Bill Baer
http://blogs.technet.com/b/wbaer/archive/2014/03/21/quick-starting-demos-with-windows-powershell.aspx
Learning to use PowerShell to manage your Hyper-V environment?

Simple Management

First type Get-vm (note if module is not loaded type import-module hyperv)

Now I notice a column for state so I just want the running vms so
type Get-vm | where {$_.state –eq ‘running’} (older PS)
type Get-vm | state –eq ‘running’ (using new PS3.0)

How I need to shutdown the running vm’s so
type Get-vm | state –eq ‘running’ | stop-vm

Start VM’s

So you need to start a VM first get a list
get-vm

Then sort based on name using like or contains.  You can use -like for wildcards, –contain for exact matches, or –ccontain for case sensitive exact matching. 

For my scenario, it is best to use –like in my where cmdlet.
get-vm | where name –like ‘*10’ (PS3.0)

Now just start em up in order, DC, DB, and finally SP, but wait you need to wait before going to the next vm.

So I finally verify for the VM heartbeat before I start another VM.


Here are more optional views

Now lets connect.

Simple VM Connection

So you need to connect to your vm?  Just just get a list of vms you want to start.  Save as variable and then using vmconnect connect to all in one step.

However since this is an array we need to step through each element in our array

Have fun with your VM’s

Stop VM’s

So just as before when we started just get-vm and then pipe to vm-stop.  Note that I filtered out just the running vm’s

VM Snapshot

So you need to snapshot several VM at one time? 
No problem, just pipe your vm’s to Checkpoint-vm

Note above that I listed the VM’s and then showed the syntax to verify the snapshots.
Now lets give the snapshots a name just add –snapshotname ‘string’

Now that fun …

Simple Network Configuration

Now I need to connect all the vm’s to a common switch.  So let’s look at a new command to show vm network settings.  See here that all the vm’s are connected to Private.
type Get-vm | Get-VMNetworkAdapter

I need a list of Virtual Switches, here’s how.
type Get-VMSwitch

I need to connect all the VM’s to a Demo Switch, here’s how.
type Get-vm | Get-VMNetworkAdapter | Connect-VMNetworkAdapter –SwitchName ‘Demo’
Now displaying all VM’s attached to Demo
type Get-vm | Get-VMNetworkAdapter | where SwitchName –eq ‘Demo’

VM Settings Administration

Now you need to set a setting for all your vm’s at once, no problem.
First list your vm’s, then pipe that list to set-vm.  Here is a list of settings, I am going to set the startup memory on all my vm’s with sp in the name.

References:

Hyper-V: Scripts
http://social.technet.microsoft.com/wiki/contents/articles/176.hyper-v-scripts.aspx
Hyper-V Settings
http://technet.microsoft.com/en-us/library/hh848575(v=wps.620).aspx
Orderly Shutdown from Scripting Guy
http://blogs.technet.com/b/heyscriptingguy/archive/2013/02/21/use-powershell-to-perform-an-orderly-shut-down-of-a-hyper-v-server.aspx
Guide to Hyper-V PowerShell (Technet)
http://technet.microsoft.com/en-us/library/hh848559(v=wps.620).aspx
VM Report Building
http://blog.basefarm.com/blog/2013/01/25/basic-inventory-of-hyperv-virtual-machines-using-powershell/
Virtual Switch
http://technet.microsoft.com/en-us/library/jj679878.aspx
Matching
http://technet.microsoft.com/en-us/library/ee692798.aspx
Kewl Article on Top Ten List on Hyper-v cmdlets
http://www.altaro.com/hyper-v/10-awesome-hyper-v-cmdlets/

MCT Hints

Delete VMs and cleanup from class
http://cursurimicrosoft.com/index.php/home/delete-all-vm-from-the-previous-moc-course-using-powershell/