Hi Experts ,
I was trying to script the process of EMC PowerPath license check for all the ESXi servers in our environment.
The PowerPath is installed but in some servers it is not registered.The installation path is like this
/opt/emc/powerpath/bin/powermt check_registration
So i want to redirect the output to a file or just the word "unlicensed" with respect to that host.
$VMHosts = Get-VMHost
$VICreds = Get-VICredentialStoreItem -File D:RootCreds.xlsx
$LicenseChk = & '/opt/emc/powerpath/bin/powermt check_registration' host=$($VMHost.name) username=$($VICreds.User) password=$($VICreds.Password)
I tried to script in this way but it is failing with below errors.
If anyone has better suggestions than this way please advice.
Thanks in Advance.
Regards
Sourav
Ok, I simplified the script, espacially the RegEx part, even further.
Let me know if this produces the correct results ?
$ExportFilePath = "C:\HostNicInfo.csv" $PuttyUser = "root"
$PuttyPwd = "passwd123"
$hostlist = "esx-test-120" $Plink = "echo Y |C:\plink.exe"
$PlinkOptions = " -v -pw $PuttyPwd"
$cmd0 = "./opt/emc/powerpath/bin/powermt check_registration"
$Report = @() $HostInfo = {} | Select HostName,State
Write-Host "Connecting to: " $hostlist -ForegroundColor Green
$command = $Plink + " " + $PlinkOptions + " " + $PuttyUser + "@" + $hostlist + " " +$cmd0
$Message = Invoke-Expression -command $command
if($message -match "State\s*:\s*licensed"){ $HostInfo.State = "licensed" } else{ $HostInfo.State = "licensed" } $HostInfo.HostName = $hostlist
$Report += $HostInfo
$Report | Export-Csv $ExportFilePath -NoTypeInformation
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You will have to use a SSH session to the ESX console to execute the command, the PowerShell call operator (&) will not work.
Most users go for plink.exe that is part of the PuTTY suite.
Alan just posted about using plink for an SSH session, see SSH PowerShell tricks with plink.exe
The first error you get seems to indicate you have a problem with the credential file you are trying to access.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Luc ,
Thanks for pointing towards Plink. I was going through some of your's & Alan's old notes regarding executing cmds through plink.
I tried using this script for a single host & to grep the "State" status to a csv file for that particular host printing the hostname & "state" status.
Below is my script i tried to use :
$ExportFilePath = "C:\HostNicInfo.csv"
$PuttyUser = "root"
$PuttyPwd = "passwd"
$hostlist = "esx-test-120"
$Plink = "C:\plink.exe"
$PlinkOptions = " -v -batch -pw $PuttyPwd"
$cmd1 = '/opt/emc/powerpath/bin/powermt check_registration '
$RCommand = '"' + $cmd1 + '"'
$command = $Plink + " " + $PlinkOptions + " " + $PuttyUser + "@" + $hostlist + " " + $RCommand
$msg = Invoke-Expression -command $command
Write-Host "Connecting to: " $hostlist -ForegroundColor Green
$HostInfo.license = ($Message[0] -split "State: ")[1]
$Report += $HostInfo.license
$Report | Export-Csv $ExportFilePath -NoTypeInformation
The output of the $cmd1 if i execute manually gives me like this below :
Type : served (counted)
State : licensed
Registered To : ACTIVATED
Issue Date : xx-xx-xx
Feature : PowerPathMP
Feature Version : 5.4
Registering Svr : xxxxxxxx
License Count : xxxx
Overdraft Count : xxx
License Server : xxxxxxxx
In the above output i tried for "State" status. But i am getting the below errors while executing the above script.
Looking up host "esx-test-120"
Connecting to 10.182.79.63 port 22
Server version: SSH-2.0-OpenSSH_5.6
Using SSH protocol version 2
We claim version: SSH-2.0-PuTTY_Release_0.62
Doing Diffie-Hellman group exchange
Doing Diffie-Hellman key exchange with hash SHA-256
The server's host key is not cached in the registry. You
have no guarantee that the server is the computer you
think it is.
The server's rsa2 key fingerprint is:
ssh-rsa 2048 54:d5:1a:c3:61:b0:2e:c1:43:cc:eb:06:29:2c:5c:86
Connection abandoned.
Disconnected: User aborted at host key verification
Connecting to: esx-test-120
Cannot index into a null array.
At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\abc.ps1:13 char:31
+ $HostInfo.license = ($Message[ <<<< 0] -split "State: ")[1]
+ CategoryInfo : InvalidOperation: (0:Int32) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
Export-Csv : Cannot bind argument to parameter 'InputObject' because it is null.
At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\abc.ps1:15 char:21
+ $Report | Export-Csv <<<< $ExportFilePath -NoTypeInformation
+ CategoryInfo : InvalidData: (:) [Export-Csv], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ExportCsvCommand
Please share your advices how to make it work because in future i want it to import a server list instead of single server.
Thanks in Advance.
When you establish the first SSH connection to a host, plink asks you to accept the host key.
Now unfortunately, there is no switch on the plink command to automate this.
But Alan has a simple trick to do this, see his SSH PowerShell tricks with plink.exe post.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Luc ,
Thanks for your advice. The host key problem is resolved now.
But i am getting below error while extracting the output from the cmd :
Cannot index into a null array.
At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\abc.ps1:13 char:31
+ $HostInfo.license = ($Message[ <<<< 0] -split "State: ")[1]
+ CategoryInfo : InvalidOperation: (0:Int32) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
Export-Csv : Cannot bind argument to parameter 'InputObject' because it is null.
At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\abc.ps1:15 char:21
+ $Report | Export-Csv <<<< $ExportFilePath -NoTypeInformation
+ CategoryInfo : InvalidData: (:) [Export-Csv], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ExportCsvCommand
Thanks in Advance.
Is that variablename correct ?
...
$msg = Invoke-Expression -command $commandWrite-Host "Connecting to: "
$hostlist -ForegroundColor Green
$HostInfo.license = ($Message[0] -split "State: ")[1]...
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks Luc , i modified it , still now i am getting this below error :
Connecting to: esxtest-120 Property 'licensed' cannot be found on this object; make sure it exists and is settable.
At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\abc.ps1:12 char:11
+ $HostInfo. <<<< licensed = ($msg[0] -split "State: ")[1]
+ CategoryInfo : InvalidOperation: (licensed:String) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound
Export-Csv : Cannot bind argument to parameter 'InputObject' because it is null.
At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\abc.ps1:14 char:21
+ $Report | Export-Csv <<<< $ExportFilePath -NoTypeInformation
+ CategoryInfo : InvalidData: (:) [Export-Csv], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ExportCsvCommand
Where did you define the variable $HostInfo ?
Is the script above the complete script you are trying to run ?
Perhaps let us know from where you got it ? There seem to be parts missing.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Luc ,
I modified the script from Yuri which reports the NIC's firmware's from all the ESXi hosts . Please find the link below :
The script i am using is the below one :
$ExportFilePath = "C:\Hostpathinfo.csv"
$PuttyUser = "root"
$PuttyPwd = "passwd123"
$hostlist = "esx-test-123"
$Plink = "echo Y |C:\plink.exe"
$PlinkOptions = " -v -pw $PuttyPwd"
$cmd1 = "./opt/emc/powerpath/bin/powermt check_registration | grep licensed"
$RCommand = '"' + $cmd1 + '"'
$command = $Plink + " " + $PlinkOptions + " " + $PuttyUser + "@" + $hostlist + " " + $RCommand
$msg = Invoke-Expression -command $command
Write-Host "Connecting to: " $hostlist -ForegroundColor Green
$HostInfo.licensed = ($msg[0] -split "State: ")[1]
$Report += $HostInfo.licensed
$Report | Export-Csv $ExportFilePath -NoTypeInformation
Either of these can be used to get output .The actual output of the command when i am using manually is below :
# ./opt/emc/powerpath/bin/powermt check_registration | grep licensed
State : licensed
# ./opt/emc/powerpath/bin/powermt check_registration
PowerPath License Information:
------------------------------
Host ID : xx-xx-xx-xx
Type : served (counted)
State : licensed
Registered To : ACTIVATED
Issue Date : 21-Jul-2010
Feature : PowerPathMP
Feature Version : 5.4
Registering Svr : xx-xx-xx-xx
License Count : xx
Overdraft Count : xx
In this line
$HostInfo.licensed = ($msg[0] -split "State: ")[1]
you assign a value to the 'licensed' property of the object in the variable $HostInfo.
But that object in $HostInfo doesn't seem to be defined anywhere. That explains the error message.
You should add this line in your script
$HostInfo = {} | Select HostName,ESXVersion,Cluster,pNic,DriverName,DriverVersion,DriverFirmware,Licensed
That define the object and its properties, more specifically the Licensed property..
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Luc ,
I am not getting the expected output. The csv file is containing only the hostname & State not whether it is licensed or unlicensed.
I am suspecting may be the format of the output which i am getting is responsible for that.There is lot blank space on the left side.I am attaching
the manual output from the command.
# ./opt/emc/powerpath/bin/powermt check_registration
PowerPath License Information:
------------------------------
Host ID : xx-xx-xx-xx
Type : served (counted)
State : licensed
Registered To : ACTIVATED
Issue Date : 11-Jul-2011
Feature : PowerPathMP
Feature Version : 5.4
Registering Svr : xx-xx-xx-xx
License Count : xx
Overdraft Count : xx
License Server : xx-xx-xx-xx
Days until expiration : (non-expiring)
License search path: /etc/emc:/etc/emc/licenses:/opt/EMCpower:/opt/EMCpower/licenses
===========================================Script===========================================
$ExportFilePath = "C:\HostNicInfo.csv"
$PuttyUser = "root"
$PuttyPwd = "passwd123"
$hostlist = "esx-test-120"
$Plink = "echo Y |C:\plink.exe"
$PlinkOptions = " -v -pw $PuttyPwd"
$cmd0 = "./opt/emc/powerpath/bin/powermt check_registration"
$Report = @()
$Message = ""
$HostInfo = {} | Select HostName,State
$HostInfo.HostName = $hostlist
Write-Host "Connecting to: " $hostlist -ForegroundColor Green
$command = $Plink + " " + $PlinkOptions + " " + $PuttyUser + "@" + $hostlist + " " +$cmd0
$Message = Invoke-Expression -command $command
$HostInfo.State = ($Message[1] -split "State : ")[1]
$Report += $HostInfo
$Report | Export-Csv $ExportFilePath -NoTypeInformation
Also i have an query if i used 'grep' command in $cmd0 it says it is not a valid cmdlet. Is there any way i can use this way
$cmd0 = "./opt/emc/powerpath/bin/powermt check_registration | grep State"
State : licensed
Thanks in Advance.
Try this version that uses a RegEx expression to find the State field.
$mask = [regex]"State\s*:\s(?<status>\w+?)\s" $ExportFilePath = "C:\HostNicInfo.csv"
$PuttyUser = "root"
$PuttyPwd = "passwd123"
$hostlist = "esx-test-120"
$Plink = "echo Y |C:\plink.exe"
$PlinkOptions = " -v -pw $PuttyPwd"
$cmd0 = "./opt/emc/powerpath/bin/powermt check_registration"
$Report = @() $Message = ""
$HostInfo = {} | Select HostName,State
$HostInfo.HostName = $hostlist
Write-Host "Connecting to: " $hostlist -ForegroundColor Green
$command = $Plink + " " + $PlinkOptions + " " + $PuttyUser + "@" + $hostlist + " " +$cmd0
$Message = Invoke-Expression -command $command
$mask.Matches($command) | Out-Null
$HostInfo.State = $Matches["status"] $Report += $HostInfo
$Report | Export-Csv $ExportFilePath -NoTypeInformation
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Luc ,
I am getting below error , i am executing in the below format :
./Scripts/pp.ps1 -status licensed
Cannot index into a null array.
At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\pp.ps1:17 char:28
+ $HostInfo.State = $Matches[ <<<< "status"]
+ CategoryInfo : InvalidOperation: (status:String) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
Regards
Sourav
Try this updated version
$mask = "State\s*:\s(?<status>\w+?)\s"
$ExportFilePath = "C:\HostNicInfo.csv" $PuttyUser = "root"
$PuttyPwd = "passwd123"
$hostlist = "esx-test-120" $Plink = "echo Y |C:\plink.exe"
$PlinkOptions = " -v -pw $PuttyPwd"
$cmd0 = "./opt/emc/powerpath/bin/powermt check_registration"
$Report = @() $Message = ""$HostInfo = {} | Select HostName,State
$HostInfo.HostName = $hostlist
Write-Host "Connecting to: " $hostlist -ForegroundColor Green
$command = $Plink + " " + $PlinkOptions + " " + $PuttyUser + "@" + $hostlist + " " +$cmd0
$Message = Invoke-Expression -command $command
$result = $Message | Select-String -Pattern $mask
if($result){ $HostInfo.State = $result.Matches[0].Groups["status"].Value
} else{ $HostInfo.State = "Status not found"
} $Report += $HostInfo
$Report | Export-Csv $ExportFilePath -NoTypeInformation
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Luc ,
It is not going inside the "if " loop. It is always printing the "Sate" as Status not found.
HostName State
esx-test-120 Status not found
esx-test-127 Status not found
Hi Luc ,
I am executing the script as .\Scripts\pp.ps1. I tried with one server with PowerPath license installed & another one with licensed installed. But both are showing State as "Status not found".
Regards
Sourav
Can you attach the output from the command in a file ?
I might be using an incorrect regex expression.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Slight change, try it like this
$mask = [regex]"State\s*:\s(?<status>\w+?)\s"
$ExportFilePath = "C:\HostNicInfo.csv" $PuttyUser = "root"
$PuttyPwd = "passwd123"
$hostlist = "esx-test-120" $Plink = "echo Y |C:\plink.exe"
$PlinkOptions = " -v -pw $PuttyPwd"
$cmd0 = "./opt/emc/powerpath/bin/powermt check_registration"
$Report = @() $Message = ""$HostInfo = {} | Select HostName,State
$HostInfo.HostName = $hostlist
Write-Host "Connecting to: " $hostlist -ForegroundColor Green
$command = $Plink + " " + $PlinkOptions + " " + $PuttyUser + "@" + $hostlist + " " +$cmd0
$Message = Invoke-Expression -command $command
$HostInfo.State = $Message | Select-String -Pattern $mask | Select -ExpandProperty Matches | %{$_.Groups["status"].Value} $Report += $HostInfo
$Report | Export-Csv $ExportFilePath -NoTypeInformation
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Luc ,
No luck , there is no output for the State status.
Do you think of any other way around we can try to get this status. Or can we use
$cmd0 = "./opt/emc/powerpath/bin/powermt check_registration | grep -i State"
Because when i use grep cmd manually i get the output like
State : licensed
Please let me know your thoughts.
Regards
Sou