COS
Expert
Expert

PowerCLI script to take arguments

I have a script called "checkifvmexists.ps1" with the below contents that checks if a VM with the name 'yomama' exists....

param([bool]$promptForCEIP = $false)

# List of modules to be loaded
$moduleList = @(
    "VMware.VimAutomation.Core",
    "VMware.VimAutomation.Vds",
    "VMware.VimAutomation.Cloud",
    "VMware.VimAutomation.PCloud",
    "VMware.VimAutomation.Cis.Core",
    "VMware.VimAutomation.Storage",
    "VMware.VimAutomation.HorizonView",
    "VMware.VimAutomation.HA",
    "VMware.VimAutomation.vROps",
    "VMware.VumAutomation",
    "VMware.DeployAutomation",
    "VMware.ImageBuilder",
    "VMware.VimAutomation.License"
    )

$productName = "PowerCLI"
$productShortName = "PowerCLI"

$loadingActivity = "Loading $productName"
$script:completedActivities = 0
$script:percentComplete = 0
$script:currentActivity = ""
$script:totalActivities = `
   $moduleList.Count + 1

function ReportStartOfActivity($activity) {
   $script:currentActivity = $activity
   Write-Progress -Activity $loadingActivity -CurrentOperation $script:currentActivity -PercentComplete $script:percentComplete
}
function ReportFinishedActivity() {
   $script:completedActivities++
   $script:percentComplete = (100.0 / $totalActivities) * $script:completedActivities
   $script:percentComplete = [Math]::Min(99, $percentComplete)
  
   Write-Progress -Activity $loadingActivity -CurrentOperation $script:currentActivity -PercentComplete $script:percentComplete
}

# Load modules
function LoadModules(){
   ReportStartOfActivity "Searching for $productShortName module components..."
  
   $loaded = Get-Module -Name $moduleList -ErrorAction Ignore | % {$_.Name}
   $registered = Get-Module -Name $moduleList -ListAvailable -ErrorAction Ignore | % {$_.Name}
   $notLoaded = $registered | ? {$loaded -notcontains $_}
  
   ReportFinishedActivity
  
   foreach ($module in $registered) {
      if ($loaded -notcontains $module) {
   ReportStartOfActivity "Loading module $module"
        
   Import-Module $module
  
   ReportFinishedActivity
      }
   }
}

LoadModules

# Update PowerCLI version after snap-in load
$powerCliFriendlyVersion = [VMware.VimAutomation.Sdk.Util10.ProductInfo]::PowerCLIFriendlyVersion
$host.ui.RawUI.WindowTitle = $powerCliFriendlyVersion

Connect-VIServer -Server "Lucifer.someplace.com"
Write-Output `n
Clear


    $Exists = get-vm -name 'yomama' -ErrorAction SilentlyContinue 
    If ($Exists){ 
        $VMExists = "TRUE"
         Write $VMExists
    } 
    Else { 
          $VMExists = "FALSE"
          Write $VMExists
    } 

How can I set it to take the argument for the VM name kind of like below...

./checkifvmexists.ps1 "yomama"

Thanks

0 Kudos
1 Reply
LucD
Leadership
Leadership

Change the 1st line into

param([bool]$promptForCEIP = $false, [string]$vmName)

and then change the  line with the Get-VM into

$Exists = Get-VM -Name $vmName -ErrorAction SilentlyContinue 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos