PowerCLI

 View Only
Expand all | Collapse all

Create variable inside here-string - powershell

  • 1.  Create variable inside here-string - powershell

    Posted Jul 02, 2019 09:16 AM

    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.



  • 2.  RE: Create variable inside here-string - powershell

    Posted Jul 02, 2019 09:34 AM


  • 3.  RE: Create variable inside here-string - powershell

    Posted Jul 02, 2019 09:36 AM

    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



  • 4.  RE: Create variable inside here-string - powershell

    Posted Jul 03, 2019 01:22 AM

    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



  • 5.  RE: Create variable inside here-string - powershell

    Posted Jul 03, 2019 02:02 AM

    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.



  • 6.  RE: Create variable inside here-string - powershell

    Posted Jul 03, 2019 06:04 AM

    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

    }



  • 7.  RE: Create variable inside here-string - powershell

    Posted Jul 08, 2019 06:59 AM

    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.



  • 8.  RE: Create variable inside here-string - powershell

    Posted Jul 08, 2019 07:02 AM

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



  • 9.  RE: Create variable inside here-string - powershell

    Posted Jul 08, 2019 07:11 AM

    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.?



  • 10.  RE: Create variable inside here-string - powershell

    Posted Jul 08, 2019 07:26 AM

    For the clarity - i am creating a different thread.



  • 11.  RE: Create variable inside here-string - powershell

    Posted Jul 08, 2019 07:32 AM

    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.