Suresh Kumar
asked on
convert from shell script to powershell
echo "[{" > data.txt
for i in `cat pending.txt`
do
timeout 2 telnet $i 22 > test.txt 2>&1
cat test.txt |grep "Connected to" >/dev/null 2>&1
if [[ $? == 0 ]]; then
echo $i >> completed.txt
echo " \"status\" : \"Completed\", " >> data.txt
echo " \"comments\" : \"Server is reachable at $(date +"%m/%d/%Y %T") EST\", " >> data.txt
echo " \"assignee\" : \"WebServices\", " >> data.txt
echo " \"serverName\" : \"$i\" " >> data.txt
echo "}, { " >> data.txt
fi
done
head -n -1 data.txt > data.txt.tmp
mv data.txt.tmp data.txt
echo "}" >> data.txt
echo "]" >> data.txt
diff -a --suppress-common-lines -y pending.txt completed.txt | awk {'print $1'} |grep -v ">" > test1.txt
mv test1.txt pending.txt
the above script is written in shell script ..can i get the same in powershell
for i in `cat pending.txt`
do
timeout 2 telnet $i 22 > test.txt 2>&1
cat test.txt |grep "Connected to" >/dev/null 2>&1
if [[ $? == 0 ]]; then
echo $i >> completed.txt
echo " \"status\" : \"Completed\", " >> data.txt
echo " \"comments\" : \"Server is reachable at $(date +"%m/%d/%Y %T") EST\", " >> data.txt
echo " \"assignee\" : \"WebServices\", " >> data.txt
echo " \"serverName\" : \"$i\" " >> data.txt
echo "}, { " >> data.txt
fi
done
head -n -1 data.txt > data.txt.tmp
mv data.txt.tmp data.txt
echo "}" >> data.txt
echo "]" >> data.txt
diff -a --suppress-common-lines -y pending.txt completed.txt | awk {'print $1'} |grep -v ">" > test1.txt
mv test1.txt pending.txt
the above script is written in shell script ..can i get the same in powershell
This script generates a JSON file (data.txt) based on a file containing machine names (pending.txt) to check for SSH responses, and keeps pending.txt up to date with failing machines. Is that correct?
ASKER
yes..correct
One issue is that you cannot use telnet that way in Windows, it does not create redirectable output. It is also a very rough method to test for an open port ... I'll have to think about alternatives here.
ASKER
sure..please let me know if you have any alternatives
Hi
you could try the following the pending.txt for my testing looked like this
using the following script assuming that pending.txt exist in the same folder as the script. you can choose the port , so you could try this with port 22 instead of 135 ( line 51 )
you could try the following the pending.txt for my testing looked like this
[
{
"ServerName": "localhost",
"Status": "Completed",
"comments": "Server is reachable 04/28/2017 14:11 in 4.1152 Milliseconds",
"assignee": "WebServices"
},
{
"ServerName": "localhost",
"Status": "Completed",
"comments": "Server is reachable 04/28/2017 14:11 in 1.6805 Milliseconds",
"assignee": "WebServices"
}
]
using the following script assuming that pending.txt exist in the same folder as the script. you can choose the port , so you could try this with port 22 instead of 135 ( line 51 )
######################################################
Function Test-Port
######################################################
{ # ref site http://poshcode.org/85
Param([string]$srv,$port=135,$timeout=500,[switch]$verbose)
# Does a TCP connection on specified port (135 by default)
$ErrorActionPreference = "Continue"
# Create TCP Client
$tcpclient = new-Object system.Net.Sockets.TcpClient
# Tell TCP Client to connect to machine on Port
$iar = $tcpclient.BeginConnect($srv,$port,$null,$null)
# Set the wait time
$wait = $iar.AsyncWaitHandle.WaitOne($timeout,$false)
# Check to see if the connection is done
if(!$wait)
{
# Close the connection and report timeout
$tcpclient.Close()
if($verbose){Write-Host "Connection Timeout"}
Return $false
}
else
{
# Close the connection and report the error if there is one
$error.Clear()
$tcpclient.EndConnect($iar) | out-Null
if(!$?){if($verbose){write-host $error[0]};$failed = $true}
$tcpclient.Close()
}
# Return $true if connection Establish else $False
if($failed){"false";return $false}else{"true";return $true}
# usage: test-port -srv localhost -verbose
} # End Function
####---------------------------------------------------####
$computers = get-content pending.txt | convertfrom-json
$Completed =@()
$failed = @()
foreach ($computer in $computers )
{
$timetaken = measure-command { $result1 = test-port -srv $computer.servername -port 135}
if ($result1 -eq $true)
{
$Completed += "" | select @{n="ServerName";e={$computer.ServerName}},
@{n="Status";e={"Completed"}},
@{n="comments";e={"Server is reachable $(get-date -f ""MM/dd/yyyy HH:mm"") in $($timetaken.TotalMilliseconds) Milliseconds"}},
@{n="assignee";e={"WebServices"}}
}
Else
{
$failed += "" | select @{n="ServerName";e={$computer.ServerName}},
@{n="Status";e={"Failed"}},
@{n="comments";e={"Server unreachable $(get-date -f ""MM/dd/yyyy HH:mm"") in $($timetaken.TotalMilliseconds) Milliseconds"}},
@{n="assignee";e={"WebServices"}}
}
}
$completed += $failed
$completed | convertto-json | out-file pending.txt
My code is similar but not using ASync sockets, relying on the default timeout which is around the same 2 seconds as the original script.
Also, pending.txt is not JSON.
And you get the progress on-screen.
Also, pending.txt is not JSON.
And you get the progress on-screen.
cls
$failed = @()
$socket = New-Object System.Net.Sockets.TcpClient
Get-Content pending.txt |
% {
[string] $server = $_
write-host -f yellow "Checking $server ..." -NoNewline
try {
$socket.Connect($server, 23) | Out-Null
$socket.Close()
[PsCustomObject] @{status = 'Completed'
comments = 'Server is reachable at '+(get-date -f 'g')+' EST'
assignee = 'WebServices'
servername = $server
}
write-host -f yellow " - success"
} catch {
$failed += $server
write-host -f yellow " - failed"
}
} | ConvertTo-Json | Out-File data.txt
Set-Content pending.txt -Value $failed
ASKER
Thanks!
but i have a query ..but where do i find the output if it is success.
when i go the Pending.tst after running the script...i can see the failure servers
What abt the success servers and where do i find the data
but i have a query ..but where do i find the output if it is success.
when i go the Pending.tst after running the script...i can see the failure servers
What abt the success servers and where do i find the data
my script updates both success and failure to the pending.txt file
Your original script contained only failed servers in (plain text) pending.txt after running it, and so does my PowerShell translation.
Successful attempts are stored in the JSON file data.txt, again the same way as in the original script.
Successful attempts are stored in the JSON file data.txt, again the same way as in the original script.
ASKER
Your original script contained only failed servers in (plain text) pending.txt after running it, and so does my PowerShell translation.
Successful attempts are stored in the JSON file data.txt, again the same way as in the original script
Script works fine only for one server..if i put a list of valid servers in the pending.txt
it gives the results first server as success and the rest as failed
Successful attempts are stored in the JSON file data.txt, again the same way as in the original script
Script works fine only for one server..if i put a list of valid servers in the pending.txt
it gives the results first server as success and the rest as failed
Hi Ramesh ,
Are your last comments aimed @Qlemo ? let me know if you would like to me /change / update my solution
Are your last comments aimed @Qlemo ? let me know if you would like to me /change / update my solution
ASKER
yes..please...
this is the scirpt to check the server is reachable or not
i need to do a add some more modification to the script
$Gfile = get-content "Pending1.txt"
foreach ($data in $Gfile){
$data = $data -split(",") -replace '^\s*|\s*$'
if ( $data[1] -eq "unix") { $port = 22} else { $port =3389 }
write-host "Connecting to " $data[0] " on port $port"
$connection = (new-object Net.Sockets.Tcpclient($dat a[0], $port))
$connection.sendtimeout = 500
if ($connection.Connected) { add-content reachable.txt $data[0] } else { add-content notreachable.txt data[0] }
$connection.close()
}
this is the scirpt to check the server is reachable or not
i need to do a add some more modification to the script
Need to call a function separately if the server is reachable and the output should look like
[
{
"status": "Completed",
"comments": "Server is reachable at 5/2/2017 7:06 AM EST",
"assignee": "WebServices",
"servername": "Server Name"
},
{
"status": "Completed",
"comments": "Server is reachable at 5/2/2017 7:06 AM EST",
"assignee": "WebServices",
"servername": "Server Name"
]
and another function if the server is not reachable
finally need to move the nonreachable servers to pending.txt
this is the scirpt to check the server is reachable or not
i need to do a add some more modification to the script
$Gfile = get-content "Pending1.txt"
foreach ($data in $Gfile){
$data = $data -split(",") -replace '^\s*|\s*$'
if ( $data[1] -eq "unix") { $port = 22} else { $port =3389 }
write-host "Connecting to " $data[0] " on port $port"
$connection = (new-object Net.Sockets.Tcpclient($dat
$connection.sendtimeout = 500
if ($connection.Connected) { add-content reachable.txt $data[0] } else { add-content notreachable.txt data[0] }
$connection.close()
}
this is the scirpt to check the server is reachable or not
i need to do a add some more modification to the script
Need to call a function separately if the server is reachable and the output should look like
[
{
"status": "Completed",
"comments": "Server is reachable at 5/2/2017 7:06 AM EST",
"assignee": "WebServices",
"servername": "Server Name"
},
{
"status": "Completed",
"comments": "Server is reachable at 5/2/2017 7:06 AM EST",
"assignee": "WebServices",
"servername": "Server Name"
]
and another function if the server is not reachable
finally need to move the nonreachable servers to pending.txt
You really should stay with one suggestion, as input and output are different. I also do not understand your added requests.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks!