VMware Cloud Community
plaman
Enthusiast
Enthusiast

Invoke-VMScript

hi,

I am trying to run the invoke command to scan for new disk and add them to the vm,

but somehow this is not working, i know the script text works. please help

$createDisk=@"

$disks = Get-Disk |Where-Object {$_.PartitionStyle -eq 'RAW'}                                                                                                                                                                                                             

foreach ($disk in $disks){Get-Disk |Where PartitionStyle -eq 'RAW' |Initialize-Disk -PartitionStyle MBR -PassThru|New-Partition -AssignDriveLetter -UseMaximumSize |Format-Volume -FileSystem NTFS -NewFileSystemLabel "Data $i"  -Confirm:$false}

"@

Invoke-VMScript -VM $vmname  -ScriptText $createDisk -GuestUser $Lcredentials.UserName  -GuestPassword $Lcredentials.Password -ScriptType Powershell  -Verbose

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

|  + & { 41 42 43 = Get-Disk |Where-Object {.PartitionStyle -eq 'RAW'}

|  +        ~~

|  Unexpected token '42' in expression or statement.

|  At line:1 char:11

|  + & { 41 42 43 = Get-Disk |Where-Object {.PartitionStyle -eq 'RAW'}

|  +           ~~

|  Unexpected token '43' in expression or statement.

|  At line:2 char:11

|  +  foreach (3 in 41 42 43){Get-Disk |Where PartitionStyle -eq 'RAW' |In ...

|  +           ~

|  Missing variable name after foreach.

|  At line:2 char:13

|  +  foreach (3 in 41 42 43){Get-Disk |Where PartitionStyle -eq 'RAW' |In ...

|  +             ~~

|  Unexpected token 'in' in expression or statement.

|  At line:1 char:3

|  + & { 41 42 43 = Get-Disk |Where-Object {.PartitionStyle -eq 'RAW'}

|  +   ~

|  Missing closing '}' in statement block or type definition.

|  At line:2 char:24

|  +  foreach (3 in 41 42 43){Get-Disk |Where PartitionStyle -eq 'RAW' |In ...

|  +                        ~

|  Unexpected token ')' in expression or statement.

|  At line:2 char:240

|  + ... olume -FileSystem NTFS -NewFileSystemLabel "Data 3"  -Confirm:False}}

|  +                                                                         ~

|  Unexpected token '}' in expression or statement.

|  At line:1 char:11

|  + & { 41 42 43 = Get-Disk |Where-Object {.PartitionStyle -eq 'RAW'}

|  +           ~~

|  The assignment expression is not valid. The input to an assignment operator must be an object that is able to accept

|  assignments, such as a variable or a property.

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

|      + FullyQualifiedErrorId : UnexpectedToken

|  

Tags (1)
0 Kudos
2 Replies
LucD
Leadership
Leadership

You placed your inline string between double quotes.

As a result, substitution will take place.

To avoid this substitution there are 2 options:

1) place the inline string between single quotes, but note that for example $i (I can't see where you initialised it btw) will not be substituted neither.

$createDisk=@'

$disks = Get-Disk |Where-Object {$_.PartitionStyle -eq 'RAW'}                                                                                                                                                                                                            

foreach ($disk in $disks){

    Get-Disk |Where PartitionStyle -eq 'RAW' |

    Initialize-Disk -PartitionStyle MBR -PassThru|

    New-Partition -AssignDriveLetter -UseMaximumSize |

    Format-Volume -FileSystem NTFS -NewFileSystemLabel "Data $i"  -Confirm:$false

}

'@


Invoke-VMScript -VM $vmname  -ScriptText $createDisk -GuestUser $Lcredentials.UserName  -GuestPassword $Lcredentials.Password -ScriptType Powershell  -Verbose

2) escape all $ sign for which you do not want substitution to take place

$createDisk=@"

`$disks = Get-Disk |Where-Object {`$_.PartitionStyle -eq 'RAW'}                                                                                                                                                                                                            

foreach (`$disk in $disks){

    Get-Disk |Where PartitionStyle -eq 'RAW' |

    Initialize-Disk -PartitionStyle MBR -PassThru|

    New-Partition -AssignDriveLetter -UseMaximumSize |

    Format-Volume -FileSystem NTFS -NewFileSystemLabel "Data `$i"  -Confirm:$false

}

"@


Invoke-VMScript -VM $vmname  -ScriptText $createDisk -GuestUser $Lcredentials.UserName  -GuestPassword $Lcredentials.Password -ScriptType Powershell  -Verbose


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

0 Kudos
plaman
Enthusiast
Enthusiast

thank you LucD for your prompt response, it worked straight away

0 Kudos