skyline-helper.ps1


function powercliinstall {
    if (get-module -listavailable -name vmware.powercli) { 
    write-host "...Powercli has already been installed..."    
    }
    else {
    install-module vmware.powercli -force
    Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $false -confirm:$false
    Set-PowerCLIConfiguration -InvalidCertificateAction ignore -confirm:$false
    write-host "...install Powercli..."
    }
}

function nsxinstall {
    if (get-module -listavailable -name PowerNSX) { 
    write-host "...PowerNSX has already been installed..."    
    }
    else {
    install-module PowerNSX -force
    write-host "...install PowerNSX..."
    }
}

function vropsinstall {
    if (get-module -listavailable -name Vmware.VimAutomation.vROps) { 
    write-host "...Vmware.VimAutomation.vROps has already been installed..."    
    }
    else {
    install-module Vmware.VimAutomation.vROps -force
    write-host "...install Vmware.VimAutomation.vROps..."
    }
}


function skyline-helper {
[CmdletBinding()] 
param(
 [string]$CHOICE1,
 [string]$CHOICE2,
 [string]$CHOICE3,
 [string]$CHOICE4,
 [string]$CHOICE5
)

switch ( $CHOICE1 )
{
prep-all {
    powercliinstall
    nsxinstall
    vropsinstall
    }

create-role { 
    connect-viserver -server $CHOICE2
    tagset
    new-virole -name $CHOICE3 -privilege (get-viprivilege -id global.diagnostics, global.health, global.licenses, global.settings, system.anonymous, system.view, system.read, storageviews.view)
    disconnect-viserver -confirm:$false
    }

check-role { 
    connect-viserver -server $CHOICE2
    tagset
    get-virole $CHOICE3 | get-viprivilege | select Id
    disconnect-viserver -confirm:$false
    }

add-2-role { 
    connect-viserver -server $CHOICE2
    tagset
    new-vipermission -entity (get-folder -norecursion) -principal $CHOICE3 -role $CHOICE4 -propagate:$true 
    disconnect-viserver -confirm:$false
    }
 
check-account { 
    connect-viserver -server $CHOICE2
    tagset
    get-vipermission -principal $CHOICE3
    disconnect-viserver -confirm:$false
    }

stop-ssh { 
    connect-viserver -server $CHOICE2
    get-vmhost -name $CHOICE3 | get-vmhostservice | Where Key -EQ "TSM-SSH" | stop-vmhostservice -confirm:$false
    disconnect-viserver -confirm:$false
    }

start-ssh { 
    connect-viserver -server $CHOICE2
    get-vmhost -name $CHOICE3 | get-vmhostservice | Where Key -EQ "TSM-SSH" | start-vmhostservice -confirm:$false
    disconnect-viserver -confirm:$false
    }

skyline-prep {
     Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
     choco feature enable -n=allowGlobalConfirmation
    choco install putty
    choco install curl
    }

check-update {plink -ssh root@$CHOICE2 -no-antispoof "/opt/vmware/bin/vamicli update --check" }

install-update {plink -ssh root@$CHOICE2 -no-antispoof "/opt/vmware/bin/vamicli update --install latest --accepteula" }

check-version {plink -ssh root@$CHOICE2 -no-antispoof "/opt/vmware/bin/vamicli version --appliance" }

nsx-prep { nsxinstall }

reset-adminpw {
    plink -ssh root@$CHOICE2 -no-antispoof "cp /usr/local/skyline/ccf/config/generated/credentials.json /usr/local/skyline/ccf/config/generated/credentials.old" 
    write-host ''
    write-host 'Please reboot skyline appliance now. The admin password is now "default"'
    write-host ''
}

reset-adminpw2 {
    ssh oot@$CHOICE2 'cp /usr/local/skyline/ccf/config/generated/credentials.json /usr/local/skyline/ccf/config/generated/credentials.old' 
    write-host ''
    write-host 'Please reboot skyline appliance now. The admin password is now "default"'
    write-host ''
}

check-disconnected {
    $DISCONNECT = plink -ssh root@$CHOICE2 -no-antispoof "grep offlineMode /usr/local/skyline/ccf/config/generated/CollectorConfig.json | grep false | wc -l" 
    
    if ($DISCONNECT -eq 0)
    {write-output "Disconnected Mode: Disabled"}
    else
    {write-output "Disconnected Mode: Enabled"}
}

check-autoupdate {
    $AUTOUP = plink -ssh root@$CHOICE2 -no-antispoof "grep false /usr/local/skyline/ccf/config/generated/AutoUpdateConfig.json | wc -l" 
    
    if ($AUTOUP -eq 0)
    {write-output "AutoUpdate: Disabled"}
    else
    {write-output "AutoUpdate: Enabled"}
}

check-proxy {
    $PROXY = plink -ssh root@$CHOICE2 -no-antispoof "grep 0 /usr/local/skyline/ccf/config/generated/NetworkConfig.json | wc -l" 
    
    if ($PROXY -eq 0)
    {write-output "Proxy: Enabled"}
    else
    {write-output "Proxy: Disabled"}
}

check-nsxaccount { 
    connect-nsxserver -vCenterServer $CHOICE2
    get-nsxuserrole $CHOICE3 
    disconnect-nsxserver -confirm:$false
    }

vrops-prep { vropsinstall }

check-vropsaccount { 
    connect-omserver $CHOICE2
    get-omuser $CHOICE3
    disconnect-omserver -confirm:$false
    }

skyline-vm-check { 
    connect-viserver -server $CHOICE2
    tagset
    get-vm $CHOICE3
    disconnect-viserver -confirm:$false
    }

skyline-vm-turnon { 
    connect-viserver -server $CHOICE2
    tagset
    start-vm $CHOICE3
    disconnect-viserver -confirm:$false
    }

powercli-prep { powercliinstall }

ova-prep {
$file = checkfile
    . $file

    if ($OVAPATH -eq $null)
    {
        $SAMPLEOVAPATH = select-string -path $file -pattern OVAPATH
        if ( $SAMPLEOVAPATH.Matches.Count -lt 1)
        {
            add-content $file '#OVAPATH = "NEED-OVA-PATH"'
            add-content $file '#OVANAME = "NEED-OVA-NAME"'
            add-content $file '#OVAIP = "NEED-OVA-IP"'
            add-content $file '#OVANETMASK = "NEED-OVA-NETMASK"'
            add-content $file '#OVADNS = "NEED-OVA-DNS"'
            add-content $file '#OVAGW = "NEED-OVA-GATEWAY"'
            add-content $file '#OVAPASSWD = "NEED-OVA-PASSWORD"'

            write-host ''
            write-host "please update OVA entries in $file"
            write-host ''
        }
    }
}

ova-deploy {
$file = checkfile
    . $file

    if ($OVAPATH -ne $null)
    {

    connect-viserver -server $CHOICE2
        tagset
        $ovfConfig = Get-OvfConfiguration $OVAPATH
        $ovfConfig.NetworkMapping.Network_1.Value = $CHOICE5
        $ovfConfig.vami.VMware_Skyline_Appliance.gateway.value = $OVAGW
        $ovfConfig.vami.VMware_Skyline_Appliance.DNS.value = $OVADNS
        $ovfConfig.vami.VMware_Skyline_Appliance.ip0.value = $OVAIP
        $ovfConfig.vami.VMware_Skyline_Appliance.netmask0.value = $OVANETMASK
        $ovfConfig.Common.varoot_password.Value = $OVAPASSWD
        Import-VApp -source $OVAPATH -name $OVANAME -OvfConfiguration $ovfConfig -VMHost $CHOICE3 -datastore $CHOICE4 -diskstorageformat thin
        start-vm -vm $OVANAME -confirm:$false
    disconnect-viserver -confirm:$false    
    }
    else
    {
            write-host ''
            write-host "ERROR - cannot execute, please update OVA entries in $file"
            write-host ''
    }
}

enable-start-docker {plink -ssh root@$CHOICE2 -no-antispoof "systemctl enable docker && systemctl start docker" }

get-vcsusers {
$file = checkfile
    . $file

connect-vcs -apitoken $APITOKEN
get-vcsuser
disconnect-vcs -confirm:$false
}

check-vcsrole {
$file = checkfile
    . $file

connect-vcs -apitoken $APITOKEN
get-vcsorganizationrole -user "$CHOICE2"
disconnect-vcs -confirm:$false
}

new-invitation {
$file = checkfile
    . $file

connect-vcs -apitoken $APITOKEN
new-vcsuserinvitation -email "$CHOICE2" -organizationrole "organization member"
disconnect-vcs -confirm:$false
}

check-invitation {
$file = checkfile
    . $file

connect-vcs -apitoken $APITOKEN
get-vcsuserinvitation
disconnect-vcs -confirm:$false
}

remove-invitation {
$file = checkfile
    . $file

connect-vcs -apitoken $APITOKEN
remove-vcsuserinvitation -invitation "$CHOICE2"
disconnect-vcs -confirm:$false
}

remove-vcsuser {
$file = checkfile
    . $file

connect-vcs -apitoken $APITOKEN
remove-vcsuser -user "$CHOICE2"
disconnect-vcs -confirm:$false
}

backup-config {
if (-not(Test-Path -Path $CHOICE2)) {
    new-item -name $CHOICE2 -itemtype "directory"
    pscp -r root@"$CHOICE2":"/usr/local/skyline/ccf/config/generated" $CHOICE2
} else {
    rename-item $CHOICE2 -newname "$CHOICE2-OLD"            

    new-item -name $CHOICE2 -itemtype "directory"
    pscp -r root@"$CHOICE2":"/usr/local/skyline/ccf/config/generated" $CHOICE2
}

}

reset-config {
plink -ssh root@$CHOICE2 -no-antispoof "mv /usr/local/skyline/ccf/config/generated /usr/local/skyline/ccf/config/generated.bak | reboot" 
}

reset-lockout {
plink -ssh root@$CHOICE2 -no-antispoof "/usr/sbin/pam_tally2 --reset --user root" 
}

default { 
    ''
    'USAGE: skyline-help.ps1 ARG VARIABLE' 
    ' (client arg): [powercli-prep | prep-all]'
    ' (vcenter arg): [create-role | check-role | add-2-role | check-account]'
    ' (esx arg): [start-ssh | stop-ssh]'
    ' (skyline arg1): [ova-prep | ova-deploy | skyline-prep]'
    ' (skyline arg2): [check-update | install-update | check-version]'
    ' (skyline arg3): [check-autoupdate | check-disconnected | check-proxy]'
    ' (skyline arg4): [skyline-vm-check | skyline-vm-turnon]'
    ' (skyline arg5): [reset-adminpw | reset-lockout]'
    ' (skyline arg6): [backup-config | reset-config]'
    ' (nsx arg): [nsx-prep | check-nsxaccount]'
    ' (vrops arg): [vrops-prep | check-vropsaccount]'
    ' (docker arg): [enable-start-docker]'
    ' (advisor arg1): [get-vcsusers | remove-vcsuser | check-vcsrole]'
    ' (advisor arg2): [new-invitation | check-invitation | remove-invitation]'
    ''
     createsource
    }
}
} #skyline-helper