Jump to content

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


Recommended Posts

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
Link to comment
Share on other sites


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

Link to comment
Share on other sites

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)
}

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...