VMware Cloud Community
IKirill
Enthusiast
Enthusiast
Jump to solution

Script Output compare when use Invoke-Vmscript

Hello! have a script to check, If the file exists.

The problem is that I can't compare Script output with true or false. In this script, even if the file exists, the value NO is returned. How to fix it?

$vmNames = "Collector"
$results = @()

foreach ($vmName in $vmNames) {
# Use Invoke-VMScript to check if the file exists
$script = {
$filePath = "C:\Soft\lastrun-restart-services.txt"
$fileExists = Test-Path $filePath
$fileExists
}

$result = Invoke-VMScript -VM $vmName -ScriptText $script -GuestUser admin -GuestPassword pass

$result.ScriptOutput
if ($result.ScriptOutput -eq "True") {
$fileResult = "YES"
} else {
$fileResult = "NO"
}

$obj = New-Object PSObject -Property @{
VMName = $vmName
FileExists = $fileResult
}

$results += $obj
}
$results | Format-Table -AutoSize

 

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The ScriptOutput property also contains a CR-LF.
With the -match operator you don't have that issue

 if ($result.ScriptOutput -match "True") {

 


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

View solution in original post

0 Kudos
1 Reply
LucD
Leadership
Leadership
Jump to solution

The ScriptOutput property also contains a CR-LF.
With the -match operator you don't have that issue

 if ($result.ScriptOutput -match "True") {

 


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

0 Kudos