VMware Cloud Community
Dev_docker
Contributor
Contributor

Create variable inside here-string - powershell

Hi Team,

I am trying to workout the below example - unable to define variable inside the here-string and useit(variable $a). something basic am missing, could you please suggest.

is here-string nature that we cant define any variables inside ?

$passon = 'hello'

$string = {

@"

`$a = '123'

write-host [$a] $passon

"@

}

I have tried using double quotes, single quotes and escape character still no luck. When i try execute always the value of $a is empty.

Tags (2)
10 Replies
LucD
Leadership
Leadership

0 Kudos
Sreejesh_D
Virtuoso
Virtuoso

Hi,

Pls try the following.

$passon = 'hello'

$string = @"

$($a = 123)

write-host $a $passon

"@

$string

-SD [vExpert 14-19,VCIX6-NV&DCV,RHCE]

https://pingforinfo.com

https://twitter.com/sreejeshd

0 Kudos
Dev_docker
Contributor
Contributor

Thanks for the responses.

From the example, I am trying something like below to assign IP details for multiple VMs from the CSV data.

foreach ($data in $Boot)

{

$VM = $data.'Vm name'

$Oip = $data.OldAddress

$Ip = $data.IPAddress

$subnet = $data.SubnetMask

$gataeway = $data.Gateway

$meta = @"

$($IA=Get-NetIPAddress | where-object IPAddress -EQ $Oip | select -ExpandProperty interfacealias

Write-Host $IA)

"@

Invoke-VMScript -VM $VM -ScriptType Powershell -ScriptText $Meta -GuestCredential $GS

}

Somehow, still the variable inside the here-string ($IA) is not storing any data, because of that i am unable proceed further. Please suggest.

but the below example works without any issues. thanks for the clarification.

$passon = 'hello'

$string = @"

$($a = 123

write-host $a $passon)

"@

$string

0 Kudos
Dev_docker
Contributor
Contributor

also, if i put the herestring in script block the variable is getting stored but other variables outside herestring is not getting resolved.

$meta =

{

@"

$($IA=Get-NetIPAddress | where-object IPAddress -EQ $Oip | select -ExpandProperty interfacealias

Write-Host $IA)

}

$Oip - is not getting resolved.

0 Kudos
LucD
Leadership
Leadership

You obviously didn't read the posts I linked to earlier.
You will have to use single quotes for the here-string, that avoids variable substitution until you actually want it to happen

You have to escape the dollar sign of variables that you do not want to be substituted. This is done with a back-tick.

Something like this

$meta = @'

`$IA=Get-NetIPAddress | where-object IPAddress -EQ $Oip | select -ExpandProperty interfacealias

Write-Host `$IA)

'@


foreach ($data in $Boot)

{

   $VM = $data.'Vm name'

   $Oip = $data.OldAddress

   $metaCmd = $ExecutionContext.InvokeCommand.ExpandString($data)


   Invoke-VMScript -VM $VM -ScriptType Powershell -ScriptText $metaCmd -GuestCredential $GS

}


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

Dev_docker
Contributor
Contributor

Hi LucD. Thank you much for the suggestions. I indeed missed the link provided by you and only saw yezdi reply. My bad.

Now its working fine without any issues.

However, now i started getting access denied error for the invoke-script

$Gpassword = '123456' | ConvertTo-SecureString -asPlainText -Force

$Gusername = '.\luser'

$GS = New-Object System.Management.Automation.PSCredential($Gusername,$Gpassword)

$metadata = @'

`$Interface = Get-NetIPAddress | where-object IPAddress -EQ $Oip | select -ExpandProperty interfacealias

write-host [`$Interface]

Remove-NetRoute -InterfaceAlias `$interface -confirm:`$false

Remove-NetIPAddress -InterfaceAlias `$interface -confirm:`$false

New-NetIPAddress -InterfaceAlias `$interface -IPAddress $Ip -AddressFamily IPv4 -PrefixLength 24 -DefaultGateway $gataeway -confirm:`$false

'@

foreach ($data in $Boot)

{

$VM = $data.'Vm name'

$Oip = $data.OldAddress

$Ip = $data.IPAddress

$subnet = $data.SubnetMask

$gataeway = $data.Gateway

$scripttext = $ExecutionContext.InvokeCommand.ExpandString($metadata)

   

Invoke-VMScript -VM $VM -ScriptType Powershell -ScriptText $scripttext -GuestCredential $GS

}

the account which i am using is a local administrator account (part of local admin group). any suggestions ? is it due to UAC or Runas.

0 Kudos
LucD
Leadership
Leadership

That is indeed most probably a UAC issue.
Can you eventually test if it works on a station where you disabled UAC?


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

0 Kudos
Dev_docker
Contributor
Contributor

Hi LucD.

I have tested with the machine by disabling UAC its working fine.

Any suggestion, can this be bypassed through invoke-vmscript. Because i wont be able to change on all the machines due to certain restrictions.?

0 Kudos
Dev_docker
Contributor
Contributor

For the clarity - i am creating a different thread.

0 Kudos
LucD
Leadership
Leadership

If it would be easy to bypass UAC, the whole point of having UAC would be useless!

There are rumours that one could use Scheduled Tasks to run scripts that bypass UAC.

But I don't think the rules of this community allow documenting hacking methods.


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

0 Kudos