Automation

 View Only
  • 1.  Unable to get the output from invoke-vmscript command

    Posted 16 days ago

    Hi,

    I am trying to get the execute the below script and I am unable to get the output from below, 

    Please help!!

    $mydiskxtend=@"
    Get-WmiObject Win32_DiskDrive | ForEach-Object {
      $disk = $_
      $partitions = "ASSOCIATORS OF " +
                    "{Win32_DiskDrive.DeviceID='$($disk.DeviceID)'} " +
                    "WHERE AssocClass = Win32_DiskDriveToDiskPartition"
      Get-WmiObject -Query $partitions | ForEach-Object {
        $partition = $_
        $drives = "ASSOCIATORS OF " +
                  "{Win32_DiskPartition.DeviceID='$($partition.DeviceID)'} " +
                  "WHERE AssocClass = Win32_LogicalDiskToPartition"
        Get-WmiObject -Query $drives | ForEach-Object {
          New-Object -Type PSCustomObject -Property @{
        Name        = $env:computername
            Index       = $disk.Index
    Disk        = $disk.DeviceID
            Status      = $disk.Status
    DiskModel   = $disk.Model
    Serial      = $disk.SerialNumber
            Partition   = $partition.Name
            DriveLetter = $_.DeviceID
            VolumeName  = $_.VolumeName
            "Size(GB)"  = [math]::round($_.Size/1024/1024/1024,2)
            "FreeSpace(GB)" = [math]::round($_.FreeSpace/1024/1024/1024,2)
    "Free(%)" = [math]::Round(((100*($_.FreeSpace))/($_.Size)),0)
    "UsedSpace(GB)" = [math]::Round(((($_.Size / 1GB))-($_.FreeSpace / 1GB)),0)
    }
        }
      }
    } | Sort-Object Index | Select-Object Name, Index, Disk, DriveLetter, VolumeName, Serial, DiskModel, "Size(GB)", "UsedSpace(GB)", "FreeSpace(GB)", "Free(%)" | Format-Table * -AutoSize
    # Write the commands to a temporary file
    "RESCAN" | Out-File -FilePath "C:\DiskPart.txt" -Encoding ASCII
    "SELECT Volume D" | Out-File -FilePath "C:\DiskPart.txt" -Append -Encoding ASCII
    "EXTEND" | Out-File -FilePath "C:\DiskPart.txt" -Append -Encoding ASCII
    "EXIT" | Out-File -FilePath "C:\DiskPart.txt" -Append -Encoding ASCII
    # Run DiskPart with the script
    Start-Process "DiskPart.exe" -ArgumentList "/s C:\DiskPart.txt" -Wait
    # Delete the script file after execution
    Remove-Item -Path "C:\DiskPart.txt" -Force
    "@
    Invoke-VMScript -VM $serv -ScriptText $mydiskxtend -ScriptType Powershell -GuestUser "admin" -GuestPassword P@ssw1rd -Verbose


  • 2.  RE: Unable to get the output from invoke-vmscript command

    Posted 16 days ago

    When you use a here-string with double quotes (@") all variables will be replaced before the variable is used, in this case the Invoke-VMScript cmdlet.
    That is probably now what you want.
    Use single quotes (@') or escape all variables (`$disk).




    ------------------------------


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


    ------------------------------