That worked. I tried as below and worked.
Original Message:
Sent: Dec 17, 2024 11:30 AM
From: LucD
Subject: Issue get the output from invoke-vmscript variable
When you switch to double quotes all variables (all starting with $) will be substituted.
Those you don't want to be substituted you will need to escape with a back-tick.
------------------------------
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Original Message:
Sent: Dec 17, 2024 07:53 AM
From: ganapa2000
Subject: Issue get the output from invoke-vmscript variable
LucD,
If I change to double quotes, I am getting the below error
$myuser = "bsuresh"
$script = @"
net localgroup administrators | where {$_ -AND $_ -notmatch "command completed successfully"} | select -skip 4 | where {$_ -like $($myuser)}
"@
-AND : The term '-AND' 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 line:1 char:44
+ & {net localgroup administrators | where { -AND -notmatch "command c ...
+ ~~~~
+ CategoryInfo : ObjectNotFound: (-AND:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Original Message:
Sent: Dec 17, 2024 03:37 AM
From: LucD
Subject: Issue get the output from invoke-vmscript variable
You are using single quotes in the here-string, that means the variable $($myuser) will not be resolved in the $script variable.
------------------------------
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Original Message:
Sent: Dec 17, 2024 02:16 AM
From: ganapa2000
Subject: Issue get the output from invoke-vmscript variable
Hi,
I have issue getting the output from below script as variable filter is not working
Please help!!
$myuser = "bsuresh"
$script = @'
net localgroup administrators | where {$_ -AND $_ -notmatch "command completed successfully"} | select -skip 4 | where {$_ -like "$($myuser)"}
'@
$sInvoke = @{
VM = $server
GuestCredential = $Creds
ScriptTYpe = 'powershell'
ScriptText = $script
}
$result = Invoke-VMScript @sInvoke
$result.scriptoutput
If I run the command directly, this works but when used with the invoke-vmscript it is not working