Hi,
I am trying to validate,if RDP works using the below script and I am getting error while I run the below script and RDP Status is shows blank in the output file.
Please help
Connect-VIServer -Server 10.10.19.19
Get-Folder test | get-vm | Where{$_.'PowerState' -eq 'PoweredOn' -and $_.Guest.OSFullName -match 'windows'} | select Name | Export-Csv -Path .\WinOS_Info.csv -NoTypeInformation -UseCulture
disconnect-viserver -server * -force -confirm:$false
Import-Csv -Path .\WinOS_Info.csv -UseCulture -PipelineVariable row |
ForEach-Object -Process {
If (New-Object System.Net.Sockets.TCPClient -ArgumentList "$_",3389)
{
$result += New-Object PsObject -Property @{
RDP_Status = 'RDP is Working'
}
}
else
{
$result = 'RDP is not Working'
}
$row | Add-Member -MemberType NoteProperty -Name 'RDP_Status' -Value $rdp -PassThru
} | Export-Csv -Path .\WinOS_Info1.csv -NoTypeInformation -UseCulture
Error
New-Object : Exception calling ".ctor" with "2" argument(s): "A non-recoverable error occurred during a database
lookup"
At C:\poc\poc_rdp.ps1:17 char:8
+ If (New-Object System.Net.Sockets.TCPClient -ArgumentList "$_",3389)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
Try something like this
Get-Folder test | get-vm | Where{$_.'PowerState' -eq 'PoweredOn' -and $_.Guest.OSFullName -match 'windows'} |
select Name |
Export-Csv -Path .\WinOS_Info.csv -NoTypeInformation -UseCulture
Disconnect-VIServer -server * -force -confirm:$false
Import-Csv -Path .\WinOS_Info.csv -UseCulture -PipelineVariable row |
ForEach-Object -Process {
try{
$rdp = New-Object System.Net.Sockets.TCPClient -ArgumentList $_.Name,3389
if($rdp.Connected){
$RDP_Status = 'RDP is Working'
}
else{
$RDP_Status = 'RDP is not Working'
}
}
catch{
$RDP_Status = 'RDP is not Working'
}
$row | Add-Member -MemberType NoteProperty -Name 'RDP_Status' -Value $RDP_Status -PassThru
} | Export-Csv -Path .\WinOS_Info1.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
SHouldn't that line be
If (New-Object System.Net.Sockets.TCPClient -ArgumentList $_.Name,3389)
The column in the CSV is named Name
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
still the same error
New-Object : Exception calling ".ctor" with "2" argument(s): "A non-recoverable error occurred during a database
lookup"
At C:\poc.ps1:17 char:8
+ If (New-Object System.Net.Sockets.TCPClient -ArgumentList "$_.Name",3389)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
That is not what I posted.
But if you absolutely want it between quotes, you should do
If (New-Object System.Net.Sockets.TCPClient -ArgumentList "$($_.Name)",3389)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
That worked, but I am getting as below, how can I get only value as "RDP is Working" instead of "@{RDP_Status=RDP is Working}"
Also, I think, validation is not working, for all VMs, I am getting same status
"Name","RDP_Status"
"Demobackup","@{RDP_Status=RDP is Working}"
"APP27","@{RDP_Status=RDP is Working}"
"DB01","@{RDP_Status=RDP is Working}"
Try something like this
Get-Folder test | get-vm | Where{$_.'PowerState' -eq 'PoweredOn' -and $_.Guest.OSFullName -match 'windows'} |
select Name |
Export-Csv -Path .\WinOS_Info.csv -NoTypeInformation -UseCulture
Disconnect-VIServer -server * -force -confirm:$false
Import-Csv -Path .\WinOS_Info.csv -UseCulture -PipelineVariable row |
ForEach-Object -Process {
try{
$rdp = New-Object System.Net.Sockets.TCPClient -ArgumentList $_.Name,3389
if($rdp.Connected){
$RDP_Status = 'RDP is Working'
}
else{
$RDP_Status = 'RDP is not Working'
}
}
catch{
$RDP_Status = 'RDP is not Working'
}
$row | Add-Member -MemberType NoteProperty -Name 'RDP_Status' -Value $RDP_Status -PassThru
} | Export-Csv -Path .\WinOS_Info1.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
that worked perfectly...Superb..Thank you very much LucD