Jump to content

Welcome to MSFN Forum
Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more. This message will be removed once you have signed in.
Login to Account Create an Account



Photo

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

- - - - -

  • Please log in to reply
7 replies to this topic

#1
HØLLØW

HØLLØW

    Member

  • Member
  • PipPip
  • 138 posts
  • Joined 01-March 07
  • OS:Windows 7 x64
  • Country: Country Flag
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

Edited by HØLLØW, 25 October 2011 - 02:58 PM.

Mit freundlichen Grüßen / Kind regards / Salutations distinguées / Atentamente / Distinti saluti

HØLLØW


How to remove advertisement from MSFN

#2
CoffeeFiend

CoffeeFiend

    Coffee Aficionado

  • Super Moderator
  • 5,399 posts
  • Joined 14-July 04
  • OS:Windows 7 x64
  • Country: Country Flag

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                                     

Coffee: \ˈkȯ-fē, ˈkä-\. noun. Heaven in a cup. Life's only treasure. The meaning of life. Kaffee ist wunderbar. C8H10N4O2 FTW.

#3
HØLLØW

HØLLØW

    Member

  • Member
  • PipPip
  • 138 posts
  • Joined 01-March 07
  • OS:Windows 7 x64
  • Country: Country Flag
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)
}

Mit freundlichen Grüßen / Kind regards / Salutations distinguées / Atentamente / Distinti saluti

HØLLØW

#4
CoffeeFiend

CoffeeFiend

    Coffee Aficionado

  • Super Moderator
  • 5,399 posts
  • Joined 14-July 04
  • OS:Windows 7 x64
  • Country: Country Flag

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

Nah. It was pretty well explained.

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)
Coffee: \ˈkȯ-fē, ˈkä-\. noun. Heaven in a cup. Life's only treasure. The meaning of life. Kaffee ist wunderbar. C8H10N4O2 FTW.

#5
HØLLØW

HØLLØW

    Member

  • Member
  • PipPip
  • 138 posts
  • Joined 01-March 07
  • OS:Windows 7 x64
  • Country: Country Flag
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!?

Edited by HØLLØW, 26 October 2011 - 06:49 AM.

Mit freundlichen Grüßen / Kind regards / Salutations distinguées / Atentamente / Distinti saluti

HØLLØW

#6
CoffeeFiend

CoffeeFiend

    Coffee Aficionado

  • Super Moderator
  • 5,399 posts
  • Joined 14-July 04
  • OS:Windows 7 x64
  • Country: Country Flag

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".

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.

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.
Coffee: \ˈkȯ-fē, ˈkä-\. noun. Heaven in a cup. Life's only treasure. The meaning of life. Kaffee ist wunderbar. C8H10N4O2 FTW.

#7
HØLLØW

HØLLØW

    Member

  • Member
  • PipPip
  • 138 posts
  • Joined 01-March 07
  • OS:Windows 7 x64
  • Country: Country Flag

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


As I said:

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

Edited by HØLLØW, 27 October 2011 - 04:08 AM.

Mit freundlichen Grüßen / Kind regards / Salutations distinguées / Atentamente / Distinti saluti

HØLLØW

#8
CoffeeFiend

CoffeeFiend

    Coffee Aficionado

  • Super Moderator
  • 5,399 posts
  • Joined 14-July 04
  • OS:Windows 7 x64
  • Country: Country Flag
Yes, but what if there are more than one blank line at the end? (just kidding). I obviously missed that part, sorry about that.
Coffee: \ˈkȯ-fē, ˈkä-\. noun. Heaven in a cup. Life's only treasure. The meaning of life. Kaffee ist wunderbar. C8H10N4O2 FTW.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users