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