简体   繁体   中英

Invoke-vmscript diskextend issue

When attempting to run my script I got the following the error message.

Script:

$extenddisk= @"


 
$letter = Get-WmiObject -Class Win32_CDROMDrive | select -ExpandProperty Drive
Set-WmiInstance -InputObject ( Get-WmiObject -Class Win32_volume -Filter "DriveLetter = '$letter'" ) -Arguments @{DriveLetter='Z:'} 

$newdisk = @(get-disk | Where-Object partitionstyle -eq 'raw')

for($i = 0; $i -lt $newdisk.Count ; $i++)

{

 

  $disknum = $newdisk[$i].Number

  $dl = get-Disk $disknum |

  Initialize-Disk -PartitionStyle GPT -PassThru |

  New-Partition -AssignDriveLetter -UseMaximumSize

  Format-Volume -driveletter $dl.Driveletter -FileSystem NTFS -Confirm:$false

 

}

 

"@


Invoke-VMScript -ScriptText $extenddisk -VM $vm -GuestCredential $VMLocalCredential -ScriptType Powershell

Error:

WARNING: The version of VMware Tools on VM 'Hostname' is out of date and may cause Invoke-VMScript to work improperly.

 

 

 

 

 

ScriptOutput

-----------------------------------------------------------------------------------------------------------------------|  At line:9 char:27

|  + for( = 0;  -lt .Count ; ++)

|  +                           ~

|  Missing expression after unary operator '++'.

|      + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException

|      + FullyQualifiedErrorId : MissingExpressionAfterOperator

|  

| 

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

UPDATE:

$extenddisk= @'


 
`$letter = Get-WmiObject -Class Win32_CDROMDrive | select -ExpandProperty Drive
Set-WmiInstance -InputObject ( Get-WmiObject -Class Win32_volume -Filter "DriveLetter = '`$letter'" ) -Arguments @{DriveLetter='Z:'}

[string[]] `$DiskArray = [char[]] (68..89)
`$DiskArrayIndex = 0

`$Disks = Get-Disk |  Where partitionstyle -eq 'raw' 

Foreach  (`$D in `$Disks) {
        `$D | Initialize-Disk -PartitionStyle GPT -PassThru | 
        New-Partition -DriveLetter `$DiskArray[`$DiskArrayIndex++] -AssignDriveLetter -UseMaximumSize | 
        Format-Volume -FileSystem NTFS -Confirm:`$false

        
}

 

'@


Invoke-VMScript -ScriptText $extenddisk -VM $row.ServerName -GuestCredential $VMLocalCredential -ScriptType Powershell

When you use " (double-quotes) with here-strings, PowerShell will attempt to expand all variables and enclosed subexpressions in the content of it. Use ' (single-quotes) instead:

$extenddisk = @'
...
'@

I wanted to post my comment under Mathias, but my reputation is too small.. anyway. @Arbelac, Mathias is correct here. you should change your here string to @' '@ And it will be just find for you usage. It would not be fine if you would be doing some 'extra stuff' on your side, before it enters the VM inside teh invoke-vmscript, but in this case i see that you are just pass the stript to VM to execute inside, nothing special, therefore dont use the @" "@ like as mentioned all the $stuff will get translated to value, so it ruins your script. You just want to pass the whole script to the VM so no need for @" "@. Everything should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM