HØLLØW Posted October 25, 2011 Share Posted October 25, 2011 (edited) 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 FullyAutomatedcluster2 True 1 False FullyAutomatedcluster3 False 1 False FullyAutomated123442 True 1 False FullyAutomatedserver23 True 0 False FullyAutomatedtest123 False 1 False FullyAutomatedabcd False 1 False FullyAutomatedI 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 October 25, 2011 by HØLLØW Link to comment Share on other sites More sharing options...
CoffeeFiend Posted October 25, 2011 Share Posted October 25, 2011 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 More sharing options...
HØLLØW Posted October 26, 2011 Author Share Posted October 26, 2011 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 More sharing options...
CoffeeFiend Posted October 26, 2011 Share Posted October 26, 2011 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 More sharing options...
HØLLØW Posted October 26, 2011 Author Share Posted October 26, 2011 (edited) Hi again :-)Thank you - Now I got it and it's working very well.Just one thing:I modified your script toselect -skip 4So 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).Countwrite-host $linecountThis 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 October 26, 2011 by HØLLØW Link to comment Share on other sites More sharing options...
CoffeeFiend Posted October 26, 2011 Share Posted October 26, 2011 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 toselect -skip 4That'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 More sharing options...
HØLLØW Posted October 26, 2011 Author Share Posted October 26, 2011 (edited) 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!?I'll test it and give you feedback.Thanks Edited October 27, 2011 by HØLLØW Link to comment Share on other sites More sharing options...
CoffeeFiend Posted October 26, 2011 Share Posted October 26, 2011 Yes, but what if there are more than one blank line at the end? (just kidding). I obviously missed that part, sorry about that. Link to comment Share on other sites More sharing options...
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now