I am trying run a command on multiple XenServer hosts using posh-ssh module. The below script was working fine earlier. For some reason it stopped working couple of months back.
Apologies if it seems out of context since it is related to XenServer. I just want help regarding the error the posh-ssh module is throwing.
The error is as below.
WARNING: Host key is not being verified since Force switch is used.
Specified method is not supported.
+ CategoryInfo : InvalidOperation: (Renci.SshNet.SshClient:SshClient) [New-SSHSession], PSNotSupportedException
+ FullyQualifiedErrorId : SSH.NewSshSession
+ PSComputerName : localhost
Cannot bind argument to parameter 'SSHSession' because it is null.
+ CategoryInfo : InvalidData: (:) [Invoke-SSHCommand], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Invoke-SSHCommand
+ PSComputerName : localhost
Cannot bind argument to parameter 'SSHSession' because it is null.
+ CategoryInfo : InvalidData: (:) [Invoke-SSHCommand], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Invoke-SSHCommand
+ PSComputerName : localhost
The script is as below:
#region VARIABLES
#$cred = Get-Credential -Credential domain\username
#$cred.Password | ConvertFrom-SecureString | Set-Content C:\Scripts\XenServersPerformanceData\encryptedpassword.txt
$password = Get-Content C:\scripts\XenServersPerformanceData\encryptedpassword.txt | ConvertTo-SecureString
$credN = New-object System.Management.Automation.PsCredential("domain\username",$password)
#endregion
#region Functions
function Get-TimeStamp {
$timestamp = "[" + (Get-Date).ToShortDateString() + " " + ((Get-Date).ToShortTimeString()) + "]"
Return $timestamp
}
function Write-Log ($message){
Write-Output "$(Get-TimeStamp) $Message" | Out-File $LogFileName -Append
}
#endregion
#region Prepare
$logdir = "\\testserver\XenServer$\Logs"
$LogFileName = $logdir+"\QueryXenHosts_v12_"+(get-date -Format yyyyMMdd)+".txt"
$errorlogfile = $logdir+"\QueryXenHosts_v12_Error_"+(get-date -Format yyyyMMdd)+".txt"
$SuccessXenHosts = $null
$FailedXenHosts = $null
#check if the logfile already created, if not run the script
if (Test-Path $LogFileName)
{
Write-Host "Script invoked on other server, aborting"
Break
}
else
{
#checking if log directory is created, creates if not
if(Test-Path $logdir){
write-log " Log directory confirmed: $logdir"
}
else{
New-Item $logdir -ItemType Directory
write-log " Created log directory $logdir"
}
#region MAIN
write-log "Script executed by: $env:userdomain\$env:username"
write-log " Getting XenServerList content from '\\testserver\XenServer$\XenServerList.txt'"
#Pulling list of XenHosts into variable
$XenHosts = Get-Content "\\testserver\XenServer$\XenServerList.txt"
$xenhostscount = $XenHosts.count
write-log " Pulled $xenhostscount Starting processing $xenhostscount XS hosts"
$timestamp = Get-Date -Format dd-MM-yyyy
#invoking the query
foreach ($XenHost in $XenHosts)
{
write-log " Processing $XenHost"
#preparing the XenHost output file
$XenHostOutputFile = "/tmp/" + $XenHost + $timestamp + ".csv"
#Preparing the xenhost performance gathering command
$command = "rrd2csv -n -s 900 :host:: >" + " /tmp/" + $XenHost + $timestamp + ".csv"
#running multiply jobs at the same time
Start-Job -Name $XenHost -ArgumentList $credN,$XenHost,$command -ScriptBlock{
param(
$param1,
$param2,
$param3
)
$temp = New-SshSession -ComputerName $param2 -Credential $param1 -Force -ConnectionTimeout 240
Invoke-SSHCommand -SSHSession $temp -Command 'pkill rrd2csv' -Verbose
Start-Sleep -Seconds 2
Invoke-SSHCommand -SSHSession $temp -Command $param3 -Verbose
$temp|Remove-SSHSession
}
}
#As previous loop was invoked as a job, all of the jobs needs to complete before proceeding with verification
write-log " Waiting 5 minutes for all jobs to finish "
Start-Sleep -Seconds 300
write-log " Checking if all jobs completed "
#checking if there are still some jobs running, if so, wait another 5 minutes, if not, continue to verification
$runningjobs = get-job|?{$_.State -eq "Running"}
if ($runningjobs.count -eq 0)
{
write-log " All jobs completed "
}
else
{
write-log " NOT All jobs completed. Waiting another 5 minutes"
Start-Sleep -Seconds 300
}
#Verification if files are generated
write-log " Veryfying if the files are generated "
#starting loop for verification
foreach ($xenhost in $XenHosts)
{
#this is the file that verification will look for
$XenHostOutputFile = "/tmp/" + $XenHost + $timestamp + ".csv"
#this is the command which XenHost will execute to verify if the file exists and return 'File exists' upon result
$checkcommand = "test -f " + $XenHostOutputFile + " && echo 'File exists'"
#connecting to XS
$temp = $null
$temp = New-SshSession -ComputerName $XenHost -Credential $credN -Force -ConnectionTimeout 240
#checking the result
$result = $null
$result = (Invoke-SSHCommand -SSHSession $temp -Command $checkcommand).output
#if the results is "File exists' means invoking command was executed correctly
if ($result -eq "File exists")
{
write-log " File exists for $XenHost"
[array]$SuccessXenHosts += $XenHost
}
else
{
#If file not exists, means that invoking command did not run correctly
write-log " File NOT exists for $XenHost"
[array]$FailedXenHosts += $XenHost
$jobresult = Receive-Job $XenHost
$jobresult | Tee-Object $errorlogfile -Append
}
$temp|Remove-SSHSession
}
#Summary
$SuccessXenHostscount = $SuccessXenHosts.count
write-log " Sript executed correctly for $SuccessXenHostscount hosts"
$FailedXenHostscount = $FailedXenHosts.count
if ($FailedXenHosts.count -gt 0)
{
write-log " Sript failed for $FailedXenHostscount hosts"
write-log " Failed hosts $FailedXenHosts"
}
#endregion
#region Stripdown
#housekeeping of logs
Write-Log " Proceeding with houskeeping of logs. Deleteing logs older than 30 days."
$LogsToClean = gci $logdir |?{$_.Name -like "Queryxenhost*" -and $_.CreationTime -le (get-date).AddDays(-30)}
if ($LogsToClean.count -gt 0)
{
foreach ($LogToClean in $LogsToClean)
{
$logfiletoremove = $LogToClean.fullname
Write-Log " Removing $logfiletoremove "
Remove-Item -Path $LogToClean.FullName -Confirm:$false
}
}else
{
write-log " No logs to delete. Exiting"
}
#endregion
}