VMware Cloud Community
Developer_PK
Enthusiast
Enthusiast

Using invoke-vmscript in a script the variables inside the script block should be stored in csv

Hi Team,

I am using invoke-vmscript but then got this issue.
VERBOSE: Performing the operation "Invoke-VMScript" on target "ComputerName".
VERBOSE: 5/5/2022 11:42:39 AM Invoke-VMScript Finished execution
Invoke-VMScript : 5/5/2022 11:42:40 AM Invoke-VMScript The operation is not allowed in the current state.
At C:\Users\pdp\Desktop\Prechecks.ps1:196 char:21
+ ... Invoke-VMScript -VM $VM -ScriptType Powershell -ScriptTex ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Invoke-VMScript], InvalidState
+ FullyQualifiedErrorId : Client20_VmGuestServiceImpl_GetProcessOutputInGuest_ViError,VMware.VimAutomation.ViCore.Cmdlets.Commands.InvokeVmScript

Script:

$script =@'
$cdrive = Get-WmiObject -Class Win32_logicaldisk -Filter "DeviceID = 'C:'"
$cdrive_feespace = $cdrive.freespace
$cdrive_size = $cdrive.size
$cdrive_feespace
$cdrive_size
$IEProxydetails = netsh winhttp import proxy sourc=ie
if($IEproxydetails -contains "* Direct access (no proxy server).*"){
$IEproxy = "No Proxy"
}
else{
$IEProxydetails
$ie = ($IEProxydetails -split ':')
$exm = $IE -replace ' ',''
$port = $exm.split("`r`n")[6]
$exm1 = ($exm.split("`r`n")[8])
$IEproxy = $exm.split("`r`n")[5] + ("($exm1)")
}
$gpParams = @{
Path = 'Enter path'
ErrorAction = 'SilentlyContinue'
}
$release = Get-ItemProperty @gpParams | Select-Object -ExpandProperty Release
$dotnet = ".NET Framework$(
switch ($release) {
({ $_ -ge 528040 }) { ' 4.8'; break }
({ $_ -ge 461808 }) { ' 4.7.2'; break }
({ $_ -ge 461308 }) { ' 4.7.1'; break }
({ $_ -ge 460798 }) { ' 4.7'; break }
({ $_ -ge 394802 }) { ' 4.6.2'; break }
({ $_ -ge 394254 }) { ' 4.6.1'; break }
({ $_ -ge 393295 }) { ' 4.6'; break }
({ $_ -ge 379893 }) { ' 4.5.2'; break }
({ $_ -ge 378675 }) { ' 4.5.1'; break }
({ $_ -ge 378389 }) { ' 4.5'; break }
default { ': 4.5+ not installed.' }
}
)"
$dotnetversion = $dotnet-replace ".NET Framework ",""
$Groups_userdata = Get-LocalGroup -Name "Administrators"
$Groups_userdata
New-Object -TypeName PSCustomObject -Property @{
ServerName = $vmname
CdriveSize = $cdrive_size
Cdrivefreespace = $cdrive_feespace
Proxy = $IEproxy
PortNumber = $port
DOtnetversion = $dotnetversion
Usergroupsdata = $Groups_userdata
} | Export-Csv -Path "C:\temp\Precheck.csv" -NoTypeInformation
'@
Invoke-VMScript -VM $VM -ScriptType Powershell -ScriptText $script -GuestCredential $VMCreds -Verbose
$AllExportedFiles = "C:\temp\Precheck.csv","C:\temp\backup"
$DestinationFolderPath = "C:\temp\bkp"

Copy-VMGuestFile -Source $AllExportedFiles -Destination $DestinationFolderPath -VM $VM -GuestToLocal -GuestCredential $VMcreds
}


Reply
0 Kudos
8 Replies
LucD
Leadership
Leadership

Is port 902 open between the station where you run this script and the ESXi node that hosts the targetted VM?


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

Developer_PK
Enthusiast
Enthusiast

Port is not in open state

 

Reply
0 Kudos
LucD
Leadership
Leadership

Can you open the port (UDP/TCP)?


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

Reply
0 Kudos
Developer_PK
Enthusiast
Enthusiast

Invoke-web request is working. But the thing is while doing copy-vmguest. it is throwing the error.
I am unable to copy the file from guest to local.

 

Reply
0 Kudos
LucD
Leadership
Leadership

What does Invoke-WebRequest have to do with Invoke-VMScript?


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

Reply
0 Kudos
Developer_PK
Enthusiast
Enthusiast

It's Invoke-vmscript typed it by mistake.

 

Reply
0 Kudos
LucD
Leadership
Leadership

Did you open port 902?


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

Reply
0 Kudos
Developer_PK
Enthusiast
Enthusiast

Hi @LucD 

Yes, Now it is working.

Thanks much for the help.

Reply
0 Kudos