{"id":726,"date":"2020-05-26T11:12:21","date_gmt":"2020-05-26T11:12:21","guid":{"rendered":"http:\/\/michaelstoica.com\/?p=726"},"modified":"2020-05-26T11:12:21","modified_gmt":"2020-05-26T11:12:21","slug":"how-to-configure-iscsi-targets-on-esxi-hosts-with-powercli","status":"publish","type":"post","link":"https:\/\/michaelstoica.com\/site\/how-to-configure-iscsi-targets-on-esxi-hosts-with-powercli\/","title":{"rendered":"How to configure iSCSI targets on ESXi hosts with PowerCLI"},"content":{"rendered":"<p>The following script\u00a0will automate the configuration of\u00a0 iSCSI targets on ESXi hosts. This is helpful especially when deploying multiple\u00a0ESXi clusters.<\/p>\n<p>When running the script you will need to connect to your vCenter, select the cluster on which you will make the configuration, set the iSCSI Targets IP and the CHAP passwords. If you are not using CHAP authentication you can remove the $chappassword and $mutualchap from parameters and as well from the\u00a0<em>New-IScsiHbaTarget.<\/em><\/p>\n<p>You need to have\u00a0the iSCSI software enable (see the previous <a href=\"https:\/\/michaelstoica.com\/site\/configure-esxi-path-selection-and-iscsi-software-adapter-with-powercli\/\">post<\/a>) before running the script. It will also check if the target already exists and if no it will add it.<\/p>\n<p>In the end it will\u00a0rescan the HBA and VMFS.<\/p>\n<pre class=\"powershell\" style=\"font-family: monospace;\"> <span style=\"color: #0000ff;\">param<\/span><span style=\"color: #000000;\">(<\/span>\r\n<span style=\"color: #800080;\">$vcenter<\/span><span style=\"color: pink;\">,<\/span>\r\n<span style=\"color: #800080;\">$clustername<\/span><span style=\"color: pink;\">,<\/span>\r\n<span style=\"color: #800080;\">$iSCSItarget<\/span><span style=\"color: pink;\">,<\/span>\r\n<span style=\"color: #800080;\">$chappassword<\/span><span style=\"color: pink;\">,<\/span>\r\n<span style=\"color: #800080;\">$mutualchap<\/span>\r\n<span style=\"color: #000000;\">)<\/span>\r\nConnect<span style=\"color: pink;\">-<\/span>VIServer <span style=\"color: #800080;\">$vcenter<\/span> \r\n<span style=\"color: #800080;\">$targetsIP<\/span> <span style=\"color: pink;\">=<\/span> <span style=\"color: #800080;\">$iSCSItarget<\/span>\r\n<span style=\"color: #800080;\">$Cluster<\/span> <span style=\"color: pink;\">=<\/span> Get<span style=\"color: pink;\">-<\/span>Cluster <span style=\"color: #800080;\">$clustername<\/span> <span style=\"color: pink;\">|<\/span> Get<span style=\"color: pink;\">-<\/span>VMhost\r\n\u00a0\r\n<span style=\"color: #0000ff;\">foreach<\/span> <span style=\"color: #000000;\">(<\/span><span style=\"color: #800080;\">$ESXiHost<\/span> <span style=\"color: #0000ff;\">in<\/span> <span style=\"color: #800080;\">$Cluster<\/span><span style=\"color: #000000;\">)<\/span> <span style=\"color: #000000;\">{<\/span>\r\n\u00a0\r\n<span style=\"color: #800080;\">$hba<\/span> <span style=\"color: pink;\">=<\/span> <span style=\"color: #800080;\">$ESXihost<\/span> <span style=\"color: pink;\">|<\/span> Get<span style=\"color: pink;\">-<\/span>VMHostHba <span style=\"color: pink;\">-<\/span><span style=\"color: #008080; font-weight: bold;\">Type<\/span> IScsi <span style=\"color: pink;\">|<\/span> <span style=\"color: #0000ff;\">Where<\/span> <span style=\"color: #000000;\">{<\/span><span style=\"color: #000080;\">$_<\/span>.Model <span style=\"color: #ff0000;\">-eq<\/span> <span style=\"color: #800000;\">\"iSCSI Software Adapter\"<\/span><span style=\"color: #000000;\">}<\/span>\r\n<span style=\"color: #0000ff;\">foreach<\/span> <span style=\"color: #000000;\">(<\/span><span style=\"color: #800080;\">$target<\/span> <span style=\"color: #0000ff;\">in<\/span> <span style=\"color: #800080;\">$targetsIP<\/span><span style=\"color: #000000;\">)<\/span> <span style=\"color: #000000;\">{<\/span>\r\n\u00a0\r\n     <span style=\"color: #008000;\"># Check to see if the SendTarget exist, if not add it<\/span>\r\n     <span style=\"color: #0000ff;\">if<\/span> <span style=\"color: #000000;\">(<\/span>Get<span style=\"color: pink;\">-<\/span>IScsiHbaTarget <span style=\"color: pink;\">-<\/span>IScsiHba <span style=\"color: #800080;\">$hba<\/span> <span style=\"color: pink;\">-<\/span><span style=\"color: #008080; font-weight: bold;\">Type<\/span> Send <span style=\"color: pink;\">|<\/span> <span style=\"color: #0000ff;\">Where<\/span> <span style=\"color: #000000;\">{<\/span><span style=\"color: #000080;\">$_<\/span>.Address <span style=\"color: #ff0000;\">-cmatch<\/span> <span style=\"color: #800080;\">$target<\/span><span style=\"color: #000000;\">}<\/span><span style=\"color: #000000;\">)<\/span> <span style=\"color: #000000;\">{<\/span>\r\n        <span style=\"color: #008080; font-weight: bold;\">Write-Host<\/span> <span style=\"color: #800000;\">\"The target $target does exist on $ESXihost\"<\/span> \r\n     <span style=\"color: #000000;\">}<\/span>\r\n     <span style=\"color: #0000ff;\">else<\/span> <span style=\"color: #000000;\">{<\/span>\r\n        <span style=\"color: #008080; font-weight: bold;\">Write-Host<\/span> <span style=\"color: #800000;\">\"The target $target doesn't exist on $ESXihost\"<\/span> \r\n        <span style=\"color: #008080; font-weight: bold;\">Write-Host<\/span> <span style=\"color: #800000;\">\"Creating $target on $host ...\"<\/span> \r\n       New<span style=\"color: pink;\">-<\/span>IScsiHbaTarget <span style=\"color: pink;\">-<\/span>IScsiHba <span style=\"color: #800080;\">$hba<\/span> <span style=\"color: pink;\">-<\/span>Address <span style=\"color: #800080;\">$target<\/span> <span style=\"color: pink;\">-<\/span>ChapType Required <span style=\"color: pink;\">-<\/span>ChapName <span style=\"color: #800000;\">\"user\"<\/span> <span style=\"color: pink;\">-<\/span>ChapPassword <span style=\"color: #800080;\">$chappassword<\/span> <span style=\"color: pink;\">-<\/span>MutualChapEnabled <span style=\"color: #800080;\">$true<\/span> <span style=\"color: pink;\">-<\/span>MutualChapName <span style=\"color: #800000;\">\"user\"<\/span> <span style=\"color: pink;\">-<\/span>MutualChapPassword <span style=\"color: #800080;\">$mutualchap<\/span>       \r\n     <span style=\"color: #000000;\">}<\/span>\r\n\u00a0\r\n Get<span style=\"color: pink;\">-<\/span>VMHostStorage <span style=\"color: pink;\">-<\/span>VMHost <span style=\"color: #800080;\">$ESXiHost<\/span> <span style=\"color: pink;\">-<\/span>RescanAllHba <span style=\"color: pink;\">-<\/span>RescanVmfs\r\n\u00a0\r\n  <span style=\"color: #000000;\">}<\/span>\r\n\u00a0\r\n<span style=\"color: #000000;\">}<\/span>\r\n\u00a0\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The following script\u00a0will automate the configuration of\u00a0 iSCSI targets on ESXi hosts. This is helpful especially when deploying multiple\u00a0ESXi clusters. When running the script you&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/michaelstoica.com\/site\/how-to-configure-iscsi-targets-on-esxi-hosts-with-powercli\/\">Continue reading<span class=\"screen-reader-text\">How to configure iSCSI targets on ESXi hosts with PowerCLI<\/span><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[31,11,45,52,51,12],"class_list":["post-726","post","type-post","status-publish","format-standard","hentry","category-general","tag-esxi","tag-powercli","tag-vcenter","tag-vcommunity","tag-vexpert","tag-vsphere","entry"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p63Lge-bI","_links":{"self":[{"href":"https:\/\/michaelstoica.com\/site\/wp-json\/wp\/v2\/posts\/726","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/michaelstoica.com\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michaelstoica.com\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michaelstoica.com\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/michaelstoica.com\/site\/wp-json\/wp\/v2\/comments?post=726"}],"version-history":[{"count":4,"href":"https:\/\/michaelstoica.com\/site\/wp-json\/wp\/v2\/posts\/726\/revisions"}],"predecessor-version":[{"id":733,"href":"https:\/\/michaelstoica.com\/site\/wp-json\/wp\/v2\/posts\/726\/revisions\/733"}],"wp:attachment":[{"href":"https:\/\/michaelstoica.com\/site\/wp-json\/wp\/v2\/media?parent=726"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaelstoica.com\/site\/wp-json\/wp\/v2\/categories?post=726"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaelstoica.com\/site\/wp-json\/wp\/v2\/tags?post=726"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}