Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update snippet.ps1 #1

Open
wants to merge 1 commit into
base: master
from
Open
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.
+152 −74
Diff settings

Always

Just for now

@@ -1,85 +1,163 @@
Function Add-LibraryItem {
<#
.NOTES :
--------------------------------------------------------
Created by : LOKESH HK
Organisation : VMWARE
e-mail : lhulibelekemp@vmware.com
--------------------------------------------------------
.DESCRIPTION
This function uploads item to the Content library from URL location.
.PARAMETER LibraryName
Name of the libray to which item needs to be uploaded.
.PARAMETER LibType
Name of the file type. (File extension name)
.PARAMETER LibItemName
Name of the library item.
.PARAMETER LibItemURLPath
URL location of the file.
.EXAMPlE
Add-LibraryItem -LibraryName 'LibraryName' -LibType 'ova' -LibItemName 'LibItemName' -LibItemURLPath $URL
Add-LibraryItem -LibraryName 'LibraryName' -LibType 'vmdk' -LibItemName 'LibItemName' -LibItemURLPath $URL
Add-LibraryItem -LibraryName 'LibraryName' -LibType 'ovf' -LibItemName 'LibItemName' -LibItemURLPath $URL
<#
.NOTES :
--------------------------------------------------------
Created by : LOKESH HK
Organisation : VMWARE
e-mail : lhulibelekemp@vmware.com
Modified by: K. Chris Nakagaki
Organization : Virtustream
e-mail : Chris.Nakagaki@virtustream.com
--------------------------------------------------------
.DESCRIPTION
This function uploads item to the Content library from URL location.
.PARAMETER LibraryName
Name of the library to which item needs to be uploaded.
.PARAMETER LibType
Name of the file type. (File extension name)
.PARAMETER LibItemName
Name of the library item.
.PARAMETER LibItemURLPath
URL location of the file.
.PARAMETER SourceType
Pull = Has vCenter 'pull' file from URL. | Push = Pushes file from client to vCenter server. Default is pull.
.PARAMETER SkipCertificateCheck
Skips Certificate check.
.EXAMPLE
These are "PULL" examples. The syntax is made for vCenter's point of view.
$URL = file:///path
or
$URL = file:///C:/path
or
$URL = file://unc-server/path
or
$URL = ds:///vmfs/volumes/
#>
param(
[Parameter(Mandatory=$true)][string]$LibraryName,
[Parameter(Mandatory=$true)][string]$LibType,
[Parameter(Mandatory=$true)][string]$LibItemName,
[Parameter(Mandatory=$true)][string]$LibItemURLPath
)
Connect-CISServer myvCenterNameorIP
Add-LibraryItem -LibraryName 'LibraryName' -LibType 'ova' -LibItemName 'LibItemName' -LibItemURLPath $URL
Add-LibraryItem -LibraryName 'LibraryName' -LibType 'vmdk' -LibItemName 'LibItemName' -LibItemURLPath $URL
Add-LibraryItem -LibraryName 'LibraryName' -LibType 'ovf' -LibItemName 'LibItemName' -LibItemURLPath $URL
.EXAMPLE
These are "PUSH" examples. URL syntax is highly dependent upon your local system, whether it is Linux or Windows.
$URL="~/Documents/myOVF/myOVF.ovf"
or
$URL="C:\myOVF\myOVF.ovf"
$ContentLibraryService = Get-CisService com.vmware.content.library

$libaryIDs = $contentLibraryService.list()
foreach($libraryID in $libaryIDs) {
$library = $contentLibraryService.get($libraryID)
if($library.name -eq $LibraryName){
$library_ID = $libraryID
break
Connect-CISServer myvCenterNameorIP
Add-LibraryItem -LibraryName 'LibraryName' -LibType 'ovf' -LibItemName 'LibItemName' -LibItemURLPath $URL -SourceType 'PUSH'
#>
param(
[Parameter(Mandatory=$true)][string]$LibraryName,
[Parameter(Mandatory=$true)][string]$LibType,
[Parameter(Mandatory=$true)][string]$LibItemName,
[Parameter(Mandatory=$true)][string]$LibItemURLPath,
[Parameter(Mandatory=$false)][string][Validateset("Pull","Push")]$SourceType="PULL",
[Parameter(Mandatory=$false)][boolean]$SkipCertificateCheck
)

$ContentLibraryService = Get-CisService com.vmware.content.library

$libaryIDs = $contentLibraryService.list()
foreach($libraryID in $libaryIDs) {
$library = $contentLibraryService.get($libraryID)
if($library.name -eq $LibraryName){
$library_ID = $libraryID
break
}
}
}

if(!$library_ID){
write-host -ForegroundColor red $LibraryName " -- is not exists.."
} else {
$ContentLibraryItemService = Get-CisService "com.vmware.content.library.item"
$UniqueChangeId = [guid]::NewGuid().tostring()

$createItemSpec = $ContentLibraryItemService.Help.create.create_spec.Create()
$createItemSpec.type = $Libtype
$createItemSpec.library_id = $library_ID
$createItemSpec.name = $LibItemName
write-host "Creating Library Item -- " $LibItemName
$libraryItemId = $ContentLibraryItemService.create($UniqueChangeId,$createItemSpec)
write-host "Library item is created with ID -- " $libraryItemId

if(!$libraryItemId){
write-host -ForegroundColor red "Failed to create Library item"
if(!$library_ID){
write-host -ForegroundColor red $LibraryName " -- does not exist.."
} else {
$ConentLibraryUpdateSession = Get-CisService "com.vmware.content.library.item.update_session"
$SessionState = "ACTIVE"
$createSessionSpec = $ConentLibraryUpdateSession.Help.create.create_spec.Create()
$createSessionSpec.state = $SessionState
$createSessionSpec.library_item_id = $libraryItemId
$ContentLibraryItemService = Get-CisService "com.vmware.content.library.item"
$UniqueChangeId = [guid]::NewGuid().tostring()

write-host " Creating new Session ID "
$newSessionId = $ConentLibraryUpdateSession.create($UniqueChangeId,$createSessionSpec)
write-host " New Session ID is created -- " $newSessionId
$createItemSpec = $ContentLibraryItemService.Help.create.create_spec.Create()
$createItemSpec.type = $Libtype
$createItemSpec.library_id = $library_ID
$createItemSpec.name = $LibItemName
write-host "Creating Library Item -- " $LibItemName
$libraryItemId = $ContentLibraryItemService.create($UniqueChangeId,$createItemSpec)
write-host "Library item is created with ID -- " $libraryItemId

if(!$newSessionId){
write-host -ForegroundColor red " Failed to create Session ID"
if(!$libraryItemId){
write-host -ForegroundColor red "Failed to create Library item"
} else {
$ConentLibraryUpdateItem = Get-Cisservice "com.vmware.content.library.item.updatesession.file"
$createFileSpec = $ConentLibraryUpdateItem.Help.add.file_spec.Create()
$LibItemName = $LibItemName+'.'+$LibType
$createFileSpec.name = $LibItemName
$createFileSpec.source_type = "PULL"
$createFileSpec.source_endpoint.uri = $LibItemURLPath
$ConentLibraryUpdateItem.add($newSessionId,$createFileSpec)
$ConentLibraryUpdateSession.complete($newSessionId)
write-host "File upload session is intiated"
$ContentLibraryUpdateSession = Get-CisService "com.vmware.content.library.item.update_session"
$SessionState = "ACTIVE"
$createSessionSpec = $ContentLibraryUpdateSession.Help.create.create_spec.Create()
$createSessionSpec.state = $SessionState
$createSessionSpec.library_item_id = $libraryItemId

write-host " Creating new Session ID "
$newSessionId = $ContentLibraryUpdateSession.create($UniqueChangeId,$createSessionSpec)
write-host " New Session ID is created -- " $newSessionId

if(!$newSessionId){
write-host -ForegroundColor red " Failed to create Session ID"
} else {
$ContentLibraryUpdateItem = Get-Cisservice "com.vmware.content.library.item.updatesession.file"
$createFileSpec = $ContentLibraryUpdateItem.Help.add.file_spec.Create()
$LibItemName = $LibItemName+'.'+$LibType
$createFileSpec.name = $LibItemName
$SourceType = $SourceType.ToUpper()
$createFileSpec.source_type = $SourceType
Switch ($SourceType)
{
"PULL"
{
$createFileSpec.source_endpoint.uri = $LibItemURLPath
$ContentLibraryUpdateItem.add($newSessionId,$createFileSpec)
write-host "File upload session is initiated."
}
"PUSH"
{
$Data = $ContentLibraryUpdateItem.add($newSessionId,$createFileSpec)
If (Test-Path $LibItemURLPath)
{
Write-Host ("Uploading " + $LibItemURLPath + " to Library: " + $LibraryName)
Invoke-RestMethod -Method Put -Uri $data[0].upload_endpoint.uri.AbsoluteUri -InFile $LibItemURLPath -Credential $labcreds -SkipCertificateCheck:$SkipCertificateCheck
Write-Host ("Upload Complete, validating...")
$validate = $ContentLibraryUpdateItem.validate($newSessionId)
}
Else
{
Write-Host ("Path/File not found: " + $LibItemURLPath)
Throw
}
If ($validate.missing_files)
{
Write-Host ("Additional files required to upload successfully.")
Foreach ($missingfile in $validate.missing_files)
{
$ItemPath = Split-Path $LibItemURLPath
Write-Host ("Looking for: " + $MissingFile + " in " + $ItemPath)
$MissingFilePath = $ItemPath | Get-ChildItem | Where-Object {$_.Name -eq $missingfile} | Resolve-Path
$test = Test-Path $MissingFilePath
If ($test -eq $true)
{
Write-Host "File located."
$createFileSpec = $ContentLibraryUpdateItem.Help.add.file_spec.Create()
$createfilespec.name = $MissingFile
$createFileSpec.source_type = $SourceType
$Data = $ContentLibraryUpdateItem.add($newSessionId,$createFileSpec)
Write-Host ("Uploading file:" + $Missingfile)
Invoke-RestMethod -Method Put -Uri $data[0].upload_endpoint.uri.AbsoluteUri -InFile $missingfilepath.ProviderPath -Credential $labcreds -SkipCertificateCheck:$SkipCertificateCheck
Write-Host ("Upload Complete.")
}
Else
{
Write-Host ("File not found: " + $MissingFile)
Throw
}
}
}
}
}
$ContentLibraryUpdateSession.complete($newSessionId)
Write-Host $ContentLibraryUpdateSession.get($newSessionId).State
}
}
}
}
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.