{"id":1842,"date":"2023-09-05T07:00:22","date_gmt":"2023-09-05T11:00:22","guid":{"rendered":"https:\/\/enterpriseadmins.org\/blog\/?p=1842"},"modified":"2023-09-05T07:00:22","modified_gmt":"2023-09-05T11:00:22","slug":"content-library-item-validation-with-power-cli","status":"publish","type":"post","link":"https:\/\/enterpriseadmins.org\/blog\/virtualization\/content-library-item-validation-with-power-cli\/","title":{"rendered":"Content Library item validation with Power CLI"},"content":{"rendered":"\n<p>Have you ever had a new site come online and want to validate that the items in a subscriber content library are in sync?  Or experience some storage issues\/corruption and want to make sure your sync&#8217;d library is still intact?  This post will use a PowerCLI script to quickly validate items in a content library.  <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$contentLibraryName = 'h114-sub'\r\n$testHost = 'lab-esxi-02.example.org'\r\n$testDatastore = 'local-hdd'\r\n\r\nforeach ($clitem in ( Get-ContentLibraryItem -ContentLibrary $contentLibraryName -ItemType ovf | Sort-Object Name ) ) {\r\n  \"Starting deploy of item $($clitem.name)...\"\r\n  $thisNewVM = $clitem | New-VM -Name \"Test_$($clitem.Name)\" -VMHost $testHost -Datastore $testDatastore\r\n  if ($thisNewVM) {\r\n    \"  New VM created. Will attempt power on.\"\r\n    &#91;void]($thisNewVM | Get-NetworkAdapter | Set-NetworkAdapter -StartConnected:$false -confirm:$false)\r\n    $thisVMstarted = $thisNewVM | Start-VM | Wait-Tools\r\n    if ($thisVMstarted) {\r\n      \"  Startup success, doing cleanup.\"\r\n      &#91;void]($thisVMstarted | Stop-VM -Confirm:$false)\r\n      &#91;void]($thisVMstarted | Remove-VM -DeletePermanently -Confirm:$false)\r\n    } else {\r\n      \"  There was an issue with a test VM for content library item $($clitem.Name).  Cleanup not completed.\"\r\n    }\r\n  } else {\r\n    write-warning \"  Failure occured creating new VM based on $($clitem.Name)!\"\r\n  }\r\n}<\/code><\/pre>\n\n\n\n<p>This script has three variables that must be defined &#8212; the name of the subscriber content library, a test host in the site\/environment you wish to test, and a datastore from that test host to use for provisioning.<\/p>\n\n\n\n<p>The script will retrieve a list of OVF templates, deploy a new VM from each using the creative name Test + the name of the content library item.  Once the item is deployed, the VM will be powered on and wait for tools to start.  If Tools start successfully, we can likely assume that the VM is in working order, so the script proceeds to power off\/delete the test VM, otherwise it will throw a warning to the screen and continue.<\/p>\n\n\n\n<p>During testing I encountered\/created a few issues which I&#8217;ll describe below.<\/p>\n\n\n\n<p>The first was caused by an &#8217;empty&#8217; VM which didn&#8217;t have an OS installed.  It failed with an error that VMware Tools was not installed.  The script didn&#8217;t delete this VM, but after reviewing an confirming this was expected behavior for the VM, I deleted it anyway.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Wait-Tools : 8\/21\/2023 6:53:45 AM       Wait-Tools              The specified VM 'Test_emptyVM-notools' does not have VMware Tools\r\ninstalled.\r\nAt line:7 char:46\r\n+     $thisVMstarted = $thisNewVM | Start-VM | Wait-Tools\r\n+                                              ~~~~~~~~~~\r\n    + CategoryInfo          : InvalidOperation: (Test_emptyVM-notools:VirtualMachineImpl) &#91;Wait-Tools], ViError\r\n    + FullyQualifiedErrorId : Client20_VmGuestServiceImpl_WaitTools_ToolsNotInstalledOrVersionIncorrect,VMware.VimAuto\r\n   mation.ViCore.Cmdlets.Commands.WaitTools\r<\/code><\/pre>\n\n\n\n<p>To contrive the next error, I ssh&#8217;d to the test host, found the content library item on the filesystem, and intentionally corrupted the VMDK by running <code>echo blah > emptyVM-notools-1_248c256e-cab1-4512-b7a0-48322ad4a7bf-flat.vmdk<\/code>.  This resulted in the following error (read below the next error for the resolution to this):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>New-VM : 8\/21\/2023 6:15:29 AM   New-VM          Transfer failed: Error during transfer of\r\nds:\/\/\/vmfs\/volumes\/639245e5-399bba0d-e1e8-000c2927a169\/\/Test_emptyVM-notools\/Test_emptyVM-notools.vmdk: The virtual\r\ndisk is either corrupted or not a supported format..\r\nAt line:3 char:26\r\n+ ... = $clitem | New-VM -Name \"Test_$($clitem.Name)\" -VMHost $testHost -Da ...\r\n+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n    + CategoryInfo          : NotSpecified: (:) &#91;New-VM], ViError\r\n    + FullyQualifiedErrorId : ViCore_VMServiceImpl_DeployFromLibraryItem_ViNetException,VMware.VimAutomation.ViCore.Cm\r\n   dlets.Commands.NewVM<\/code><\/pre>\n\n\n\n<p>After capturing this error, I deleted the files associated with the content library item.  I had assumed that these missing files would trigger the content library to re-sync the content.  Instead, I received the following error:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>New-VM : 8\/21\/2023 6:22:32 AM   New-VM          Transfer failed: Error during transfer of ds:\/\/\/vmfs\/volumes\/639245e5-399bba0d-e1e8-000c2927a169\/\/contentlib-21c270a1-11\r\n7e-4982-a014-ac122fdb74eb\/088a872b-1098-441b-807a-251f76402991\/emptyVM-notools-1_248c256e-cab1-4512-b7a0-48322ad4a7bf.vmdk?serverId=bad2b868-9a1e-4d68-ba7e-26\r\n0f397cc0ca: Failed to resolve source datastore file URI(ds:\/\/\/vmfs\/volumes\/639245e5-399bba0d-e1e8-000c2927a169\/\/contentlib-21c270a1-117e-4982-a014-ac122fdb74e\r\nb\/088a872b-1098-441b-807a-251f76402991\/emptyVM-notools-1_248c256e-cab1-4512-b7a0-48322ad4a7bf.vmdk?serverId=bad2b868-9a1e-4d68-ba7e-260f397cc0ca).\r\nAt line:3 char:26\r\n+ ... = $clitem | New-VM -Name \"Test_$($clitem.Name)\" -VMHost $testHost -Da ...\r\n+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n    + CategoryInfo          : NotSpecified: (:) &#91;New-VM], ViError\r\n    + FullyQualifiedErrorId : ViCore_VMServiceImpl_DeployFromLibraryItem_ViNetException,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewVM\r\r<\/code><\/pre>\n\n\n\n<p>To address the previous two items, I found the &#8216;bad&#8217; content library item in the web interface, right clicked and selected <code>delete item content<\/code>.  I had expected this operation to fail, as I assumed that content could only be deleted from the publisher repo and not the subscriber repo.  However, this command was successful, removing the &#8216;size&#8217; property of the item from the content library as if it had never sync&#8217;d.  Rerunning the script to deploy this content library item caused the item to replicate and the deployment was successful.<\/p>\n\n\n\n<p>Hopefully this script can help you validate content library items if needed.  If you have any suggestions on how to improve this script, please leave a comment.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have you ever had a new site come online and want to validate that the items in a subscriber content library are in sync? Or experience some storage issues\/corruption and want to make sure your sync&#8217;d library is still intact? &hellip; <a href=\"https:\/\/enterpriseadmins.org\/blog\/virtualization\/content-library-item-validation-with-power-cli\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[9,4],"tags":[],"class_list":["post-1842","post","type-post","status-publish","format-standard","hentry","category-lab-infrastructure","category-virtualization"],"_links":{"self":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/1842","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/comments?post=1842"}],"version-history":[{"count":2,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/1842\/revisions"}],"predecessor-version":[{"id":1844,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/1842\/revisions\/1844"}],"wp:attachment":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/media?parent=1842"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/categories?post=1842"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/tags?post=1842"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}