Reply to Message

View discussion in a popup

Replying to:
LucD
Leadership
Leadership

There are a couple of issues with your function.

Also, when doing an SSH to a VCSA, you first have to enter the 'shell' command, before you can actually use shell commands.
That is why I use a shell stream.

This is a version, including a call, that works for me.

function create-contentlib {

    [cmdletbinding()]

    param (

        [parameter(mandatory = $true,

            valuefrompipeline = $true,

            valuefrompipelinebypropertyname = $true)]

        [string]$viserver,

        [parameter(mandatory = $true)]

        [PSCredential]$Credential_vcenter,

        [parameter(mandatory = $true)]

        [PSCredential]$Credential_root,

        [parameter(mandatory = $true)]

        [string]$datastore

    )


    Connect-VIServer -server $viserver -Credential $Credential_vcenter | Out-Null


    $session = New-SSHSession -ComputerName $viserver -Credential $Credential_root

    $stream = New-SSHShellStream -SSHSession $session -TerminalName xterm


    # Open shell

    Invoke-SSHStreamShellCommand -ShellStream $stream -PrompPattern "Shell access is granted" -Command 'shell' | Out-Null


    # Run command

    $result = Invoke-SSHStreamShellCommand -ShellStream $stream -PrompPattern "]#" -Command 'service-control --status vmware-content-library'


    if (($result -join '') -notmatch 'Running:') {

        Write-Host "content lib service needs to be restarted " -ForegroundColor Blue

        Invoke-SSHCommand -Command "service-control --start vmware-content-library" -SessionId $session_vcsa_id

    }


    Write-Host "creating content lib" -ForegroundColor Cyan

    $conlib = New-ContentLibrary -Name "siteA con lib" -Datastore (Get-Datastore -Name  $datastore) -Published -Description "content lib for site A vcenter"


    $stream.Close()

    Remove-SSHSession -SSHSession $session | Out-Null

}


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

View solution in original post

Reply
0 Kudos