PowerCLI

 View Only
Expand all | Collapse all

Posh-SSH error - Specified method is not supported

  • 1.  Posh-SSH error - Specified method is not supported

    Posted Oct 20, 2021 05:54 AM

    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
    }

     



  • 2.  RE: Posh-SSH error - Specified method is not supported

    Posted Oct 20, 2021 06:02 AM

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



  • 3.  RE: Posh-SSH error - Specified method is not supported

    Posted Oct 20, 2021 06:09 AM

    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

     



  • 4.  RE: Posh-SSH error - Specified method is not supported

    Posted Oct 20, 2021 06:17 AM

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



  • 5.  RE: Posh-SSH error - Specified method is not supported

    Posted Oct 20, 2021 06:23 AM

    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.



  • 6.  RE: Posh-SSH error - Specified method is not supported

    Posted Oct 20, 2021 06:27 AM

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



  • 7.  RE: Posh-SSH error - Specified method is not supported

    Posted Oct 20, 2021 06:29 AM

    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???



  • 8.  RE: Posh-SSH error - Specified method is not supported

    Posted Oct 20, 2021 06:30 AM

    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?



  • 9.  RE: Posh-SSH error - Specified method is not supported

    Posted Oct 20, 2021 06:44 AM

    That would in any case be the more reliable method.



  • 10.  RE: Posh-SSH error - Specified method is not supported

    Posted Oct 20, 2021 07:10 AM

    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



  • 11.  RE: Posh-SSH error - Specified method is not supported

    Posted Oct 20, 2021 07:15 AM

    What does this return?

    $ExecutionContext.SessionState.LanguageMode


  • 12.  RE: Posh-SSH error - Specified method is not supported

    Posted Oct 20, 2021 07:25 AM

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



  • 13.  RE: Posh-SSH error - Specified method is not supported
    Best Answer

    Posted Oct 20, 2021 07:28 AM

    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.



  • 14.  RE: Posh-SSH error - Specified method is not supported
    Best Answer

    Posted Oct 20, 2021 09:12 AM

    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

     



  • 15.  RE: Posh-SSH error - Specified method is not supported

    Posted Oct 20, 2021 09:34 AM

    So your issue is solved?



  • 16.  RE: Posh-SSH error - Specified method is not supported

    Posted Oct 20, 2021 09:57 AM

    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.



  • 17.  RE: Posh-SSH error - Specified method is not supported

    Posted Oct 20, 2021 11:16 AM

    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.