VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Add-Member : Cannot bind argument to parameter 'InputObject' because it is null.

Hi,

I am getting the error while I run the below script

$servs = Get-Content ".\McAfee_Server_List.txt"

$report = foreach ($serv in $servs) {
try
{
$session = New-SSHSession $serv -Credential $Cred -AcceptKey -ErrorAction Stop
$result = $session | Select-Object -ExpandProperty Connected
$output = $((Invoke-SSHCommand -SSHSession $session -Command 'hostname').output)
Invoke-SSHCommand -SSHSession $session -Command 'mkdir /tmp/MA'
Invoke-SSHCommand -SSHSession $session -Command 'chmod 755 /tmp/MA'
Set-SCPItem -ComputerName $serv -Credential $Cred -Path 'D:\McAfeeSmartInstall.sh' -Destination '/tmp/MA' -Verbose -AcceptKey
Invoke-SSHCommand -SSHSession $session -Command 'chmod 755 /tmp/MA/McAfeeSmartInstall.sh'
Invoke-SSHCommand -SSHSession $session -Command '/tmp/MA/McAfeeSmartInstall.sh'
Get-SSHSession | Remove-SSHSession | Out-Null
}
catch
{
$result = 'False'
}
$row | Add-Member -MemberType NoteProperty -Name 'VM' -Value $output -PassThru | Add-Member -MemberType NoteProperty -Name 'root Login' -Value $result -PassThru
}
$report | Export-Excel -Path $reportlocation -WorksheetName Summary

 

Error

ganapa2000_0-1666603673527.png

 

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You can set the value in the catch block

$servs = Get-Content ".\McAfee_Server_List.txt"

$report = foreach ($serv in $servs) {
  try {
    $session = New-SSHSession $serv -Credential $Cred -AcceptKey -ErrorAction Stop
    $result = $session | Select-Object -ExpandProperty Connected
    $output = $((Invoke-SSHCommand -SSHSession $session -Command 'hostname').output)
    Invoke-SSHCommand -SSHSession $session -Command 'mkdir /tmp/MA'
    Invoke-SSHCommand -SSHSession $session -Command 'chmod 755 /tmp/MA'
    Set-SCPItem -ComputerName $serv -Credential $Cred -Path 'D:\McAfeeSmartInstall.sh' -Destination '/tmp/MA' -Verbose -AcceptKey
    Invoke-SSHCommand -SSHSession $session -Command 'chmod 755 /tmp/MA/McAfeeSmartInstall.sh'
    Invoke-SSHCommand -SSHSession $session -Command '/tmp/MA/McAfeeSmartInstall.sh'
    Get-SSHSession | Remove-SSHSession | Out-Null
  } catch {
    $result = 'False'
    $output = $serv
  }
  New-Object -TypeName PSObject -Property @{
    VM = $output
    'root Login' = $result
  }
}
$report | Export-Excel -Path $reportlocation -WorksheetName Summary


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
6 Replies
LucD
Leadership
Leadership
Jump to solution

There is no $row variable, hence the error.
You could do

$servs = Get-Content ".\McAfee_Server_List.txt"

$report = foreach ($serv in $servs) {
  try {
    $session = New-SSHSession $serv -Credential $Cred -AcceptKey -ErrorAction Stop
    $result = $session | Select-Object -ExpandProperty Connected
    $output = $((Invoke-SSHCommand -SSHSession $session -Command 'hostname').output)
    Invoke-SSHCommand -SSHSession $session -Command 'mkdir /tmp/MA'
    Invoke-SSHCommand -SSHSession $session -Command 'chmod 755 /tmp/MA'
    Set-SCPItem -ComputerName $serv -Credential $Cred -Path 'D:\McAfeeSmartInstall.sh' -Destination '/tmp/MA' -Verbose -AcceptKey
    Invoke-SSHCommand -SSHSession $session -Command 'chmod 755 /tmp/MA/McAfeeSmartInstall.sh'
    Invoke-SSHCommand -SSHSession $session -Command '/tmp/MA/McAfeeSmartInstall.sh'
    Get-SSHSession | Remove-SSHSession | Out-Null
  } catch {
    $result = 'False'
  }
  New-Object -TypeName PSObject -Property @{
    VM = $output
    'root Login' = $result
  }
}
$report | Export-Excel -Path $reportlocation -WorksheetName Summary


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD,

that worked but for failed logins VMs the VM name in the output shows the same name which was valid for previous sessions.

how can I get the VM Name as blank for failed VMs?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can set the value in the catch block

$servs = Get-Content ".\McAfee_Server_List.txt"

$report = foreach ($serv in $servs) {
  try {
    $session = New-SSHSession $serv -Credential $Cred -AcceptKey -ErrorAction Stop
    $result = $session | Select-Object -ExpandProperty Connected
    $output = $((Invoke-SSHCommand -SSHSession $session -Command 'hostname').output)
    Invoke-SSHCommand -SSHSession $session -Command 'mkdir /tmp/MA'
    Invoke-SSHCommand -SSHSession $session -Command 'chmod 755 /tmp/MA'
    Set-SCPItem -ComputerName $serv -Credential $Cred -Path 'D:\McAfeeSmartInstall.sh' -Destination '/tmp/MA' -Verbose -AcceptKey
    Invoke-SSHCommand -SSHSession $session -Command 'chmod 755 /tmp/MA/McAfeeSmartInstall.sh'
    Invoke-SSHCommand -SSHSession $session -Command '/tmp/MA/McAfeeSmartInstall.sh'
    Get-SSHSession | Remove-SSHSession | Out-Null
  } catch {
    $result = 'False'
    $output = $serv
  }
  New-Object -TypeName PSObject -Property @{
    VM = $output
    'root Login' = $result
  }
}
$report | Export-Excel -Path $reportlocation -WorksheetName Summary


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD,

that worked. one last thing, how can I order the display of objects in the output ?

 New-Object -TypeName PSObject -Property @{
    VM = $output
    'root Login' = $result
  }

 

Tags (1)
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Use an ordered object

  New-Object -TypeName PSObject -Property ([ordered]@{
    VM = $output
    'root Login' = $result
  })


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Thank you so much 🙂

Reply
0 Kudos