VMware Cloud Community
leomahesh
Enthusiast
Enthusiast
Jump to solution

Posh-SSH error - Specified method is not supported

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
}

 

Reply
0 Kudos
2 Solutions

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You seem to be working in a limited PS environment.
It only allows base cmdlets and some classes.

You will have to talk to the administrators of your environment.


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

View solution in original post

leomahesh
Enthusiast
Enthusiast
Jump to solution

Thanks for the reply.

 

Changed the registry value for below key from 4 to 8 and the script is working now.

HKLM\System\CurrentControlSet\Control\SESSION MANAGER\Environment\_ __PSLockdownPolicy

 

View solution in original post

Reply
0 Kudos
16 Replies
LucD
Leadership
Leadership
Jump to solution

Are you really trying to connect to 'localhost'?
Can you add a -Verbose switch on the New-SshSession lines?


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

Reply
0 Kudos
leomahesh
Enthusiast
Enthusiast
Jump to solution

Hi Luc,

I am not trying to connect to localhost. I am trying to connect to the remote xenserver hosts.

The only additional output I get with the verbose switch is below.

VERBOSE: Using SSH Username and Password authentication for connection.
WARNING: Host key is not being verified since Force switch is used.
New-SshSession : Specified method is not supported.
At C:\Scripts\XenServersPerformanceData\QueryXenHosts_v1.2.ps1:138 char:13
+ $temp = New-SshSession -ComputerName $XenHost -Credential $credN ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (Renci.SshNet.SshClient:SshClient) [New-SSHSession], PSNotSupportedException
+ FullyQualifiedErrorId : SSH.NewSshSession

Invoke-SSHCommand : Cannot bind argument to parameter 'SSHSession' because it is null.
At C:\Scripts\XenServersPerformanceData\QueryXenHosts_v1.2.ps1:142 char:46
+ $result = (Invoke-SSHCommand -SSHSession $temp -Command $checkcom ...
+ ~~~~~
+ CategoryInfo : InvalidData: (:) [Invoke-SSHCommand], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Invoke-SSHCommand

 

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

Those first errors showed PSComputerName: localhost, which I find very strange.


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

Reply
0 Kudos
leomahesh
Enthusiast
Enthusiast
Jump to solution

All this while this was working. I guess windows update wiped out the Posh-ssh module directory from the PSModules directory.

I copied the Poss-SSH module directory back to the c:\windows\system32\WindowsPowershell\1.0\modules directory, but it is not working.

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

Are you saying you didn't install it with Install-Module?


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

Reply
0 Kudos
leomahesh
Enthusiast
Enthusiast
Jump to solution

No. I did not do install-module after copying the file. Thought if I am copying it to the system wide directory it would import when I relaunch the session.

Do I need to do Install-module???

Reply
0 Kudos
leomahesh
Enthusiast
Enthusiast
Jump to solution

No. I did not do install-module after copying the file. Thought if I am copying it to the system wide directory it would import when I relaunch the session.

Do I need to do Install-module?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That would in any case be the more reliable method.


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

Reply
0 Kudos
leomahesh
Enthusiast
Enthusiast
Jump to solution

Did an import-module and facing below issue.

 

PS C:\WINDOWS\system32> Get-Module -ListAvailable -name *ssh*


Directory: C:\Program Files\WindowsPowerShell\Modules


ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Manifest 2.2 Posh-SSH {Get-SCPFile, Get-SCPFolder, Get-SCPItem, Get-SFTPFile...}


PS C:\WINDOWS\system32> Import-Module -name Posh-SSH
New-Object : Cannot create type. Only core types are supported in this language mode.
At C:\Program Files\WindowsPowerShell\Modules\Posh-SSH\2.2\Posh-SSH.psm1:5 char:27
+ $global:SshSessions = New-Object System.Collections.ArrayList
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (:) [New-Object], PSNotSupportedException
+ FullyQualifiedErrorId : CannotCreateTypeConstrainedLanguage,Microsoft.PowerShell.Commands.NewObjectCommand

New-Object : Cannot create type. Only core types are supported in this language mode.
At C:\Program Files\WindowsPowerShell\Modules\Posh-SSH\2.2\Posh-SSH.psm1:10 char:28
+ $global:SFTPSessions = New-Object System.Collections.ArrayList
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (:) [New-Object], PSNotSupportedException
+ FullyQualifiedErrorId : CannotCreateTypeConstrainedLanguage,Microsoft.PowerShell.Commands.NewObjectCommand

PS C:\WINDOWS\system32> Get-Module -ListAvailable
PS C:\WINDOWS\system32> $ExecutionContext.SessionState.LanguageMode
ConstrainedLanguage
PS C:\WINDOWS\system32> $Env:__PSLockdownPolicy
4

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

What does this return?

$ExecutionContext.SessionState.LanguageMode


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

Reply
0 Kudos
leomahesh
Enthusiast
Enthusiast
Jump to solution

PS C:\WINDOWS\system32> $ExecutionContext.SessionState.LanguageMode
ConstrainedLanguage

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You seem to be working in a limited PS environment.
It only allows base cmdlets and some classes.

You will have to talk to the administrators of your environment.


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

leomahesh
Enthusiast
Enthusiast
Jump to solution

Thanks for the reply.

 

Changed the registry value for below key from 4 to 8 and the script is working now.

HKLM\System\CurrentControlSet\Control\SESSION MANAGER\Environment\_ __PSLockdownPolicy

 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

So your issue is solved?


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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Really?
I tell you what is wrong, and you select your answer as the Solution?

Don't count on a reply from me on your future questions.


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

Reply
0 Kudos
leomahesh
Enthusiast
Enthusiast
Jump to solution

Apologies if it came that way.

I just wanted to highlight the actual change done to get the issue resolved. So in future if someone refernces this thread, they know what the actual solution is.

I have selected your comments as the answer as well.

Reply
0 Kudos