{"id":2047,"date":"2014-06-11T23:08:37","date_gmt":"2014-06-11T21:08:37","guid":{"rendered":"http:\/\/michlstechblog.info\/blog\/?p=2047"},"modified":"2021-12-14T10:52:52","modified_gmt":"2021-12-14T09:52:52","slug":"vmware-powercli-scripts-in-native-powershell","status":"publish","type":"post","link":"https:\/\/michlstechblog.info\/blog\/vmware-powercli-scripts-in-native-powershell\/","title":{"rendered":"VMware: PowerCli Scripts in native Powershell"},"content":{"rendered":"<div class=\"twoclick_social_bookmarks_post_2047 social_share_privacy clearfix 1.6.4 locale-en_US sprite-en_US\"><\/div><div class=\"twoclick-js\"><script type=\"text\/javascript\">\/* <![CDATA[ *\/\njQuery(document).ready(function($){if($('.twoclick_social_bookmarks_post_2047')){$('.twoclick_social_bookmarks_post_2047').socialSharePrivacy({\"services\":{\"flattr\":{\"uid\":\"Michl\",\"status\":\"on\",\"the_title\":\"VMware%3A%20PowerCli%20Scripts%20in%20native%20Powershell\",\"the_excerpt\":\"Hi%2C%0D%0A%0D%0Awhen%20you%20want%20to%20execute%20your%20PowerCli%20script%20you%20have%20to%20start%20the%20%22VMware%20vSphere%20PowerCLI%22%20shell%20before.%20But%2C%20for%20example%20Schedule%20Tasks%2C%20it%20whould%20be%20nice%20that%20the%20script%20load%20the%20PowerCli%20environment%20itself.%20%20Then%20you%20simply%20have%20to%20start%20your%20script%20like%20this%0D%0A%0D%0A%25Systemroot%25%5CSystem32%5CWindowsPowerShell%5Cv1.0%5Cpowershell.exe%20-File%20YourPowerCliScript.ps1%0D%0A%0D%0ATo%20load%20the%20PowerCli%20script%20%20...\",\"txt_info\":\"2 clicks for more data protection:\\r\\n\\r\\nOnly when you click here, the button will be come active and you can send your recommendation to Flattr. When activating, data are transmitted to third parties. \",\"perma_option\":\"off\"}},\"txt_help\":\"When you activate these fields by clicking, information to Flattr may be transferred abroad, and probably may also stored there.\",\"settings_perma\":\"Enable permanently and accept data transmission. \",\"info_link\":\"http:\\\/\\\/www.heise.de\\\/ct\\\/artikel\\\/2-Klicks-fuer-mehr-Datenschutz-1333879.html\",\"uri\":\"https:\\\/\\\/michlstechblog.info\\\/blog\\\/vmware-powercli-scripts-in-native-powershell\\\/\",\"post_id\":2047,\"post_title_referrer_track\":\"VMware%3A+PowerCli+Scripts+in+native+Powershell\",\"display_infobox\":\"on\"});}});\n\/* ]]> *\/<\/script><\/div><p>Hi,<\/p>\n<p>when you want to execute your PowerCli script you have to start the &#8220;VMware vSphere PowerCLI&#8221; shell before. But, for example Schedule Tasks, it whould be nice that the script load the PowerCli environment itself.  Then you simply have to start your script like this<\/p>\n<p><code>%Systemroot%\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -File YourPowerCliScript.ps1<\/code><\/p>\n<p>To load the PowerCli script Environent in your powershell script add this lines at the top of your script.<br \/>\n<!--more--><\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\nAdd-PSSnapin VMware.VimAutomation.Core\r\nAdd-PSSnapin VMware.VimAutomation.Vds\r\nif(get-item HKLM:\\SOFTWARE\\Microsoft\\PowerShell\\1\\PowerShellSnapIns\\VMware.VimAutomation.Core){\r\n\t. ((get-item HKLM:\\SOFTWARE\\Microsoft\\PowerShell\\1\\PowerShellSnapIns\\VMware.VimAutomation.Core).GetValue(&quot;ApplicationBase&quot;)+&quot;\\Scripts\\Initialize-PowerCLIEnvironment.ps1&quot;)\r\n}\r\nelse\r\n{\r\n\twrite-warning &quot;PowerCLI Path not found in registry, please set path to Initialize-PowerCLIEnvironment.ps1 manually. Is PowerCli aleady installed?&quot;\r\n\t. &quot;D:\\Programs (x86)\\VMware\\Infrastructure\\vSphere PowerCLI\\Scripts\\Initialize-PowerCLIEnvironment.ps1&quot;\r\n}\r\n\r\n# Connect to vCenter\r\n$sVCenterHost=&quot;vcenter.subdomain.domain.local&quot;\r\nWrite-Host -NoNewline &quot; Connecting to vCenter...&quot;\r\nConnect-VIServer $sVCenterHost -ErrorAction SilentlyContinue -WarningAction SilentlyContinue |out-null\r\nif(!$?){\r\n\tWrite-Host -ForegroundColor Red &quot; Could not connect to $sVCenterHost&quot;\r\n\texit 2\r\n}\r\nelse{\r\n\tWrite-Host &quot;ok&quot;\r\n}\r\n<\/pre>\n<p>The script reads the PowerCLI installation path from the registry, load the snapins VMware.VimAutomation.Core and VMware.VimAutomation.Vds and executes Initialize-PowerCLIEnvironment.ps1 in the current shell (the leading &#8220;.&#8221; is importent)<\/p>\n<p>Have fun<\/p>\n<p>Michael <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi, when you want to execute your PowerCli script you have to start the &#8220;VMware vSphere PowerCLI&#8221; shell before. But, for example Schedule Tasks, it whould be nice that the script load the PowerCli environment itself. Then you simply have to start your script like this %Systemroot%\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -File YourPowerCliScript.ps1 To load the PowerCli script Environent &hellip; <a href=\"https:\/\/michlstechblog.info\/blog\/vmware-powercli-scripts-in-native-powershell\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">VMware: PowerCli Scripts in native Powershell<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,4],"tags":[497,215,133,14],"class_list":["post-2047","post","type-post","status-publish","format-standard","hentry","category-vmware","category-windowsscripts","tag-execute-powercli-script-in-native-powershell","tag-powercli","tag-powershell","tag-vmware-2"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/2047","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/comments?post=2047"}],"version-history":[{"count":7,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/2047\/revisions"}],"predecessor-version":[{"id":8292,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/2047\/revisions\/8292"}],"wp:attachment":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/media?parent=2047"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/categories?post=2047"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/tags?post=2047"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}