MSFN Forum: [Powershell] Get lines of text-file and fill up a variable - MSFN Forum

Jump to content


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

[Powershell] Get lines of text-file and fill up a variable Rate Topic: -----

#1 User is offline   HØLLØW 

  • Member
  • PipPip
  • Group: Members
  • Posts: 135
  • Joined: 01-March 07
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 25 October 2011 - 02:55 PM

Hey Guys,

I'm working with Powershell for a few days now - It's totally new to me (I know you already heard that so many times before... :-)).


I already have the base of my script - But now I have to do the following:


I have a text-file / logfile that looks like this (as you can see - I'm using PowerCLI from VMware, but I think this is a general Powershell question :-)):

Name           				HAEnabled  HAFailover DrsEnabled DrsAutomationLevel  
                                      	Level                     				
----           				---------  ---------- ---------- ------------------ 
cluster1     				True   	1      	True   	FullyAutomated
cluster2     				True   	1      	False  	FullyAutomated
cluster3     				False  	1      	False  	FullyAutomated
123442     				True   	1      	False  	FullyAutomated
server23                  	True   	0      	False  	FullyAutomated
test123                    	False  	1      	False  	FullyAutomated
abcd     				False  	1      	False  	FullyAutomated



I think we have to cut off all the lines which are not necessary for the script?! - This will be the first 5 lines of the logfile and the stuff behind the cluster-names.

The next step will be to fill up a variable with the names of the clusters (in the first column) - I think this must be done in a loop?!
The script should be flexible so I will run a special command for each server / cluster:

function disableesxhostmonitoring {
	# Disable the Cluster-Option "Host Monitoring"
	$spec = New-Object VMware.Vim.ClusterConfigSpecEx
	$spec.dasConfig = New-Object VMware.Vim.ClusterDasConfigInfo
	$spec.dasConfig.hostMonitoring = "disabled"
	$_this = Get-Cluster $vmcluster | Get-View
	$_this.ReconfigureComputeResource_Task($spec, $true)
}




I hope someone can help.



Thanks

This post has been edited by HØLLØW: 25 October 2011 - 02:58 PM



#2 User is offline   CoffeeFiend 

  • Coffee Aficionado
  • Group: Super Moderator
  • Posts: 5,399
  • Joined: 14-July 04
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 25 October 2011 - 04:48 PM

View PostHØLLØW, on 25 October 2011 - 02:55 PM, said:

I'm working with Powershell for a few days now - It's totally new to me (I know you already heard that so many times before... :-)).

Been there before :) I also use PowerCLI, but we don't have clusters full of ESXi machines. And yes, it is indeed a rather general PowerShell question.

This should do the trick:
$servers = @()
foreach($server in gc clusters.txt | select -skip 3) {
$tmp = $server.split(" ",[system.StringSplitOptions]::RemoveEmptyEntries)
$tmpSvr = New-Object System.Object
$tmpSvr | Add-Member -membertype noteproperty -name Name -value $tmp[0]
$tmpSvr | Add-Member -membertype noteproperty -name HAEnabled -value $tmp[1]
$tmpSvr | Add-Member -membertype noteproperty -name HAFailover -value $tmp[2]
$tmpSvr | Add-Member -membertype noteproperty -name DrsEnabled -value $tmp[3]
$tmpSvr | Add-Member -membertype noteproperty -name DrsAutomationLevel -value $tmp[4]
$servers += $tmpSvr }


assuming your cluster list is called clusters.txt. This recreates an actual PowerShell object called $servers. You can use it like any other object now. For example, if I run $servers | format-table after that, I get:
Name                                                HAEnabled                                           HAFailover                                          DrsEnabled                                          DrsAutomationLevel                                 
----                                                ---------                                           ----------                                          ----------                                          ------------------                                 
cluster1                                            True                                                1                                                   True                                                FullyAutomated                                     
cluster2                                            True                                                1                                                   False                                               FullyAutomated                                     
cluster3                                            False                                               1                                                   False                                               FullyAutomated                                     
123442                                              True                                                1                                                   False                                               FullyAutomated                                     
server23                                            True                                                0                                                   False                                               FullyAutomated                                     
test123                                             False                                               1                                                   False                                               FullyAutomated                                     
abcd                                                False                                               1                                                   False                                               FullyAutomated                                     



#3 User is offline   HØLLØW 

  • Member
  • PipPip
  • Group: Members
  • Posts: 135
  • Joined: 01-March 07
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 26 October 2011 - 05:00 AM

Hi CoffeeFiend,

thanks for your reply - I'm not sure if I explained my problem detailed enough...


I need to fill a variable called "$vmcluster" with the names of the cluster - I think this should be done in a loop!?

I want to run my defined function for every cluster in the list:

function disableesxhostmonitoring {
        # Disable the Cluster-Option "Host Monitoring"
        $spec = New-Object VMware.Vim.ClusterConfigSpecEx
        $spec.dasConfig = New-Object VMware.Vim.ClusterDasConfigInfo
        $spec.dasConfig.hostMonitoring = "disabled"
        $_this = Get-Cluster $vmcluster | Get-View
        $_this.ReconfigureComputeResource_Task($spec, $true)
}



#4 User is offline   CoffeeFiend 

  • Coffee Aficionado
  • Group: Super Moderator
  • Posts: 5,399
  • Joined: 14-July 04
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 26 October 2011 - 05:25 AM

View PostHØLLØW, on 26 October 2011 - 05:00 AM, said:

thanks for your reply - I'm not sure if I explained my problem detailed enough...

Nah. It was pretty well explained.

View PostHØLLØW, on 26 October 2011 - 05:00 AM, said:

I need to fill a variable called "$vmcluster" with the names of the cluster - I think this should be done in a loop!?

I think you just didn't "get" how to use it yet then. Just use a foreach loop like this:

foreach ($clusterobject in $servers) { 
$vmcluster = $clusterobject.name 
$vmcluster
}


The advantage here is that you can not only access the name property of the cluster object ($cluster.name) but every other property that was read from the log file. Obviously, you'll be doing something else than writing it to the console, so replace the last line ($vmcluster by itself) with whatever you'd like to do with it (like calling disableesxhostmonitoring). Or if you're positive that you'll never need the other properties, then just create a list of names, and use it directly with a foreach loop (still very similar). Then again, I'd probably use a parameter on that function for the cluster name (much like you would in any other language)

#5 User is offline   HØLLØW 

  • Member
  • PipPip
  • Group: Members
  • Posts: 135
  • Joined: 01-March 07
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 26 October 2011 - 06:38 AM

Hi again :-)

Thank you - Now I got it and it's working very well.



Just one thing:

I modified your script to

select -skip 4


So I get a very clean list of just the cluster names - Is it possible to also skip the last line (because it's empty)?


EDIT:

Sorry,

I checked my script again and noticed that the last line is an empty line that Powershell doesn't count!?

I' running the following command to get the total line numbers:

$linecount = (Get-Content $getclusterlog_clean).Count
write-host $linecount



This gives me the number "63" - If I open "$getclusterlog_clean" there are 64 lines (the last one is empty as I said before).

So how can I remove this empty line at the end of my logfile? - I think there will be a solution to remove all empty lines without counting te total line numbers before!?

This post has been edited by HØLLØW: 26 October 2011 - 06:49 AM


#6 User is offline   CoffeeFiend 

  • Coffee Aficionado
  • Group: Super Moderator
  • Posts: 5,399
  • Joined: 14-July 04
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 26 October 2011 - 03:14 PM

View PostHØLLØW, on 26 October 2011 - 06:38 AM, said:

Thank you - Now I got it and it's working very well.

You're welcome :) And yes, it can take a while to "get it".

View PostHØLLØW, on 26 October 2011 - 06:38 AM, said:

Just one thing:

I modified your script to

select -skip 4

That's fine. I was using what you posted above. Line 1 started by "Name". Line 2 had just "Level" on it, line 3 was a bunch of dashes, and line 4 was where the data started. Perhaps your log file was slightly different.

View PostHØLLØW, on 26 October 2011 - 06:38 AM, said:

Is it possible to also skip the last line (because it's empty)?

Sure. But what about if there are 2 empty lines at the end? Or about a bug that somehow introduces a blank line in the middle of the file? It's easier and more reliable to just skip empty lines altogether. Just test $vmcluster's value before calling your method like such:
foreach ($clusterobject in $servers) { 
$vmcluster = $clusterobject.name 
if ($vmcluster) { $vmcluster }
}

i.e. if ($vmcluster) { your_code_here }, then your code won't be run if $vmcluster contains nothing.

#7 User is offline   HØLLØW 

  • Member
  • PipPip
  • Group: Members
  • Posts: 135
  • Joined: 01-March 07
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 26 October 2011 - 04:18 PM

Quote

Sure. But what about if there are 2 empty lines at the end?


As I said:

Quote

So how can I remove this empty line at the end of my logfile? - I think there will be a solution to remove all empty lines without counting te total line numbers before!?


:yes:


I'll test it and give you feedback.




Thanks

This post has been edited by HØLLØW: 27 October 2011 - 04:08 AM


#8 User is offline   CoffeeFiend 

  • Coffee Aficionado
  • Group: Super Moderator
  • Posts: 5,399
  • Joined: 14-July 04
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 26 October 2011 - 04:26 PM

Yes, but what if there are more than one blank line at the end? (just kidding). I obviously missed that part, sorry about that.

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2013 msfn.org
Privacy Policy