AWS Send-SSMcommand parameter variable

1

I am trying to use a variable inside my command list like this

$Commands = @( '$DiskNumber = (Get-Partition -DriveLetter $driveletter).DiskNumber', 'Update-Disk -Number "$DiskNumber"', '$Size = Get-PartitionSupportedSize -DriveLetter $driveletter', 'Resize-Partition -DriveLetter $driveletter -Size $Size.SizeMax' ) $Parameter = @{ commands = $Commands }

however when I run the ssm command, its not recognizing the $driveletter variable which i defined earlier in the script.

if I put in letter instead of $driveletter, it works

any idea how to pass the variable inside the $commands list?

已提問 2 年前檢視次數 545 次
2 個答案
0

you need to wrap variable with backticks. I also ran into this issue and to troubleshoot you just go to the location of the script on the host and look at the script, you'll notice with how you have it, if you look at the script (c:\programdata\amazon\ssm{instanceId}\document\orchestration{orchestrationID}\awsrunPowerShellScript\0.awsrunPowerShellscript_script.ps1) the variable doesnt even show up left of the equal sign.

so just wrap like `$DiskNumber` = (get-partition...).. Then open the script from path and look at it to troublshoot further.

已回答 2 年前
0

this is the error I am seeing. With or without back ticks

Get-Partition : Cannot validate argument on parameter 'DriveLetter'. The argument is null. Provide a valid value for 
the argument, and then try running the command again.
At C:\ProgramData\Amazon\SSM\InstanceData\i-0c02b9986c3c33057\document\orchestration\e4a27d78-e4ae-4fae-b45f-f528729b81
dc\awsrunPowerShellScript\0.awsrunPowerShellScript\_script.ps1:1 char:45
+ `$DiskNumber` = (Get-Partition -DriveLetter $driveletter).DiskNumber
+                                             ~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Get-Partition], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Get-Partition
 
$Size = : The term '$Size =' is not recognized as the name of a cmdlet, function, script file, or operable program. 
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\ProgramData\Amazon\SSM\InstanceData\i-0c02b9986c3c33057\document\orchestration\e4a27d78-e4ae-4fae-b45f-f528729b81
dc\awsrunPowerShellScript\0.awsrunPowerShellScript\_script.ps1:3 char:1
+ `$Size` = Get-PartitionSupportedSize -DriveLetter $driveletter
+ ~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: ($Size =:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
Resize-Partition : Cannot validate argument on parameter 'DriveLetter'. The argument is null. Provide a valid value 
for the argument, and then try running the command again.
At C:\ProgramData\Amazon\SSM\InstanceData\i-0c02b9986c3c33057\document\orchestration\e4a27d78-e4ae-4fae-b45f-f528729b81
dc\awsrunPowerShellScript\0.awsrunPowerShellScript\_script.ps1:4 char:31
+ Resize-Partition -DriveLetter $driveletter -Size $Size.SizeMax
+                               ~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Resize-Partition], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Resize-Partition
drago
已回答 1 年前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南