Microsoft Virtualization Discussions

Powershell Script Help

ramkump12345
6,374 Views

Hi all

I have modified a script written by the forum member Chris Lionetti to check the options of an array of target servers and report on the diffrences compared to the Master server, Ive imported the module but the script will not run as a scheduled task and keeps throwing up this message below, not sure if the credentials loop is not right, if I invoke it manually it does work and creates the xlsx file but I have to run the script twice after manually running import-moduel DataONTAP?

Any ideas anyone?

Error

Connect-NaController : Cannot bind argument to parameter 'Name' because it is n
ull.
At C:\Windows\System32\WindowsPowerShell\v1.0\Modules\DataONTAP\script2.ps1:19
char:21
+ Connect-NaController <<<<  $TargetController -Credential $Tcred
    + CategoryInfo          : InvalidData&colon; (:) [Connect-NaController], Paramet
   erBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,D
   ataONTAP.PowerShell.SDK.ConnectNaController

Get-NaOption : Value cannot be null.
Parameter name: Controller parameter neither specified nor set in $global:Curre
ntNaController
At C:\Windows\System32\WindowsPowerShell\v1.0\Modules\DataONTAP\script2.ps1:21
char:36
+ $suppress = $Targops = get-naoption <<<<
    + CategoryInfo          : InvalidArgument: (:) [Get-NaOption], ArgumentNul
   lException
    + FullyQualifiedErrorId : ControllerNotSpecified,DataONTAP.PowerShell.SDK.
   Cmdlets.Options.GetNaOption

Script File

import-module DataONTAP

$workingController = "Masterserverfiler"
$targetControllers= @("server1","server2")
$User = "xxx"
$Pass = "xxx"

$WCPass  = ConvertTo-SecureString $Pass -AsPlainText –Force
$Wcred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User,$WCPass
$TCPass  = ConvertTo-SecureString $Pass -AsPlainText –Force
$Tcred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User,$WCPass
$workops=new-object system.collections.arraylist
Connect-NaController $TargetController -Credential $Tcred

$suppress = $Targops = get-naoption
Write-host "------------------------------------------------"

$excel=new-object -comobject excel.application
$excel.Visible=$true
$workbooks=$excel.workbooks.add()
$worksheet=$workbooks.worksheets.item(1)

$worksheet.cells.item(1,1)="Option Name"
$worksheet.cells.item(1,1).font.bold=$true
$worksheet.cells.item(1,1).font.size=22
$worksheet.cells.item(2,1)="Controller Name"
$worksheet.cells.item(2,1).font.bold=$true
$worksheet.cells.item(2,1).font.size=22
$worksheet.cells.item(1,2)="Master FAS"
$worksheet.cells.item(1,2).font.bold=$true
$worksheet.cells.item(1,2).font.size=22
$worksheet.cells.item(2,2)=$WorkingController
$cellR=3
foreach($Controller in $TargetControllers)

{          Connect-NaController $Controller -Credential $Wcred
          $Workops.add( (get-naoption  ) )
           $worksheet.cells.item(1,$cellr)="Target FAS"
           $worksheet.cells.item(1,$cellr).font.bold=$true
           $worksheet.cells.item(1,$cellr).font.size=22
           $worksheet.cells.item(2,$cellr++)=$controller
}
$worksheet.cells.item(1,$cellr)="Command to fix"


Write-host "------------------------------------------------"
$coun=2
foreach ($workop in $workops)
{     $coun=$coun+1
      $line=2
      $founditem=$false
      foreach ($item in $workop)
      {      $workname=$item.name
            $workval =$item.value
            foreach($titem in $targops)
            {     if ($titem.name -eq $item.name)
                  {      $line=$line+1
                        $foundvalue=$true
                        $worksheet.cells.item($line,1)=$titem.name
                        $worksheet.cells.item($line,2)=$titem.value
                        $worksheet.cells.item($line,$coun)=$item.value
                        write-host "   "$item.name"="$item.value    
                        if ($titem.value -ne $item.value)
                        {      write-host "   "$item.name"="$item.value" -Values Mismatch ="$titem.value
                              $worksheet.cells.item($line,$cellr)=("set-naoption"+$titem.name+" "+$item.value)
                              write-host "        To fix this issue, command is as follows;"
                              write-host "           set-naoption"$titem.name""$item.value
                        }
                  }
            }
      }
}
$workdoc=$worksheet.UsedRange
$workdoc.EntireColumn.Autofit() | out-null

6 REPLIES 6

OMARR1124
6,374 Views

I have not tried the rest of the script, but try this to correct the first error that you are getting.

Change:

Connect-NaController $TargetController -Credential $Tcred

to:

ForEach ($Controller in $TargetController){Connect-NaController $Controller -Credential $Tcred}

Also, it may be an issue of trying to load Excel if the script is not running in an active session

ramkump12345
6,374 Views

Hi

i tried it thanks for that, and tried some of other variations but its still doesnt play ball, it only seems to work when I load the powershell shell command window  then type import-module DataONTAP and then execute the script, if I double click the script it just comes up with the same error.

If I put a single value in the array it does work and doesnt come up with the error so it seems it doesnt pass the credentials when there is more tha  one controller

This doesnt work

$targetControllers= @("server1","server2")

This works

$targetControllers= ("server1")

mscarpi
6,374 Views

Hi

How do you schedule the script? Check this out......http://dmitrysotnikov.wordpress.com/2011/02/03/how-to-schedule-a-powershell-script/

regards

Marco

mscarpi
6,374 Views

btw. if you want to automaticaly load the ontap module follow the instructions.

Howto auto load DataONTAP Powershell:

open powershell

type "echo $profile"

output example: C:\Users\mscarpi\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

create the directory WindowsPowerShell and the Microsoft.PowerShell_profile.ps1 file

open the Microsoft.PowerShell_profile.ps1

add import-module DataONTAP

save and close the file

close powershell and start it again

check to verify that the module is loaded "get-module"

output should list DataONTAP

ramkump12345
6,374 Views

Hi

Thanks I already imported the module

PS C:\Windows\System32\WindowsPowerShell\v1.0> get-module

ModuleType Name                      ExportedCommands
---------- ----                      ----------------
Manifest   DataONTAP                 {Invoke-NaSnapmirrorThrottle, Resume-Nc...

But the script seems to ignore it, as I said It only works when you run it from the powershell console twice I think somethings is not right with the credential code passthrough when you have more than one target controller as it doesnt complain at all when there is only one controller

ramkump12345
6,374 Views

Hi All

Its now working like a treat, i removed the variable for the username and password for traget controllers and passed on the currently logged on credentials which has rights and now it runs as a task and no errors and corrected the varaible for the target controllers

changed

ForEach ($Controller in $TargetController){Connect-NaController $Controller -Credential $Tcred}

to

ForEach ($Controller in $TargetControllers){Connect-NaController $Controller}

Thanks A lot for all your help

Public