Microsoft Virtualization Discussions

Configuration info for a controller

callowaydx
8,387 Views

Full disclosure, I'm fairly new to PowerShell but a big fan. I'm trying to learn more and more so that I can leverage it for my organization.

I'm developing a script that logs into each controller and gathers information that I specify. It takes that informaiton and add/appends it to a file based on the controller name and date/time then attaches those files and sends an email.

The script works up until I check for the multistore license and start checking for different info. Any suggestions? Maybe I need to clean up my script, too. Any and all help would be appreciated.

1 ACCEPTED SOLUTION

bsti
8,387 Views

Here is your issue:

$NFSiSCSIVerify = Get-NaLicense -Name multistore

    if ($NFSViSCSIVerify.Licensed -eq "TRUE")

You have an extra V in your second line. 

Also, I'd avoid comparing a boolean value to a string. It works, but can be confusing.  I'd try something like this:

if ($NFSiSCSIVerify.Licensed )

or

if ($NFSiSCSIVerify.Licensed -eq $true )

View solution in original post

12 REPLIES 12

vinith
8,288 Views

I wen through your script, and i found that the credential parameter is missing for Connect-NaController, you will need to specify credentials as below, i have highlighted the text which needs to be appended in black.

ForEach ($Controller in $ControllerList)

{

    #Admin account logon

    $Password = ConvertTo-SecureString "password" -AsPlainText –Force

    $Cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "administrator",$Password

    Connect-NaController $Controller -Credential $cred

Also you can use custom hash tables to append all values to one place rather than using Add-Content to create reusable objects, please have a look at the link http://blogs.msdn.com/b/powershell/archive/2009/12/05/new-object-psobject-property-hashtable.aspx

callowaydx
8,288 Views

Thank you for your help. I've fixed the credential portion (thanks for the catch) and I'm looking into hast tables.

I'm having trouble with lines 102 - 126 of my script. The variable is not populating with info and I'm having difficulty getting the 'if' portion of my script to check if a value is true, the continue to run the script.

Thoughts?

#Check to see if the filer is licensed for multistore.

    #If it is, check for NFS/iSCSI related items.

    $NFSiSCSIVerify = Get-NaLicense -Name multistore

    if ($NFSViSCSIVerify.Licensed -eq "TRUE")

    {

    #Get vFiler info

    Get-NaVfiler | Out-String -Stream | Add-Content -Path $ControllerConfigFile

    $NFSiSCSIControllerList = Get-NaVfiler

   

    #Get NFS Export info running in memory

    Add-Content -Path $ControllerConfigFile -Value "

    NFS exports in memory

    "

    Invoke-NaSsh -Command "vfiler run * exportfs" | Out-String -Stream | Add-Content -Path $ControllerConfigFile

   

    #Run scripts against NFS/iSCSI controllers

    ForEach ($NFSiSCSIController in $NFSiSCSIControllerList)

        {

        #get NFS Exports info from the exports file

        Add-Content -Path $ControllerConfigFile -Value "

        NFS exports in the exports file -- Shoud match what is in memory...

        "

        Read-NaFile $("/vol/" + $NFSiSCSIController + "/etc/exports") | Out-String -Stream | Add-Content -Path $ControllerConfigFile

        }

   

    }

bsti
8,388 Views

Here is your issue:

$NFSiSCSIVerify = Get-NaLicense -Name multistore

    if ($NFSViSCSIVerify.Licensed -eq "TRUE")

You have an extra V in your second line. 

Also, I'd avoid comparing a boolean value to a string. It works, but can be confusing.  I'd try something like this:

if ($NFSiSCSIVerify.Licensed )

or

if ($NFSiSCSIVerify.Licensed -eq $true )

callowaydx
8,288 Views

That did the trick! Thanks! One little character...

I originally used the second of your suggestions, if ($NFSiSCSIVerify.Licensed -eq $true ), but it still wouldn't work so I tried other things just to get some results. I appreciate your help and I'm still looking into hash tags.

callowaydx
8,288 Views

Looking over the hash table link you sent me and I'm not quite sure how to incorporate it...

Would I hash this portion? Add-Content -Path $ControllerConfigFile

I'm not sure how to use it as described in the link. And there also seems to be issues with ordering?

bsti
8,288 Views

I agree with Vinith in spirit in that it's better to export your data as objects, so that you can re-read them back into Powershell later and re-use them, but that might take quite a bit of rework on your part.  This is how you would use this concept:

In our case, we have a script that does something very similar to what yours does.  Once per day, it grabs the options and values off of all controllers using Get-NaOption.  We then record that to a date stamped .xml file.  We then read in the previous days' file values and compare the two.  If there are differences, we get a report detailing what has changed.  Another useful function is we can read in the .xml files and quickly re-apply the option values.  Here is some example code:

$options = Get-NaOption

#  Export the options as object-oriented xml:

$options | Export-CliXML -path c:\temp\options1.xml

#  Import the same file back into a collection:

$optionsFromFile = Import-CliXml -path c:\temp\options1.xml

#  Reapply the options if I want (Be careful, this is dangerous):

$optionsFromFile | % { Set-NaOption -OptionName $_.Name -OptionValue $_.Value }

I'd probably never do this exact exercise, but it illustrates what you can do with object-oriented output and input.  XML works very nicely and the Export/Import-CliXML cmdlets make it very easy.  In your case, you are exporting quite a bit of unlike data, so I'm not sure this is practical with your approach.  I'd take a look and decide what works best for your application.

callowaydx
8,288 Views

Thank you for your advice. I'm going to look at your suggestion and try to incorporate it. It would be nice to be able to compare what has changed and be able to act on it.

callowaydx
8,288 Views

Where in your sample code do you compare the files from the previous day?

bsti
8,288 Views

I didn't, but it would look like this:

#  Get the current options:

$options = Get-NaOption

#  Get the previous day's options:

$optionsOld = Import-CliXml -Path C:\temp\previous_options.xml

Compare-Object -ReferenceObject $options -DifferenceObject $optionsOld -Property name,value

callowaydx
7,444 Views

I see... My challenge becomes grabbing the file with the correct date and time... I could strip the time out of the file since I plan to run this only once a day. That would solve my filename problem. Thanks again!

bsti
7,444 Views

If you just want to compare to the latest file, that is easy:

$file = get-childitem -path C:\temp | ? { $_.Name -imatch "\.xml" } | Sort-Object LastWriteTime -Descending | select-object -first 1

if ( $file )

{

#  Compare...

  $optionsOld = Import-CliXml -Path $file.FullName

}

else

{

   throw ("No files found to compare!")

}

callowaydx
7,444 Views

Thank you for the quick response! I've learned quite a bit since posting here! You guys are a great resource and I really appreciate the help.

Public