I am trying to connect to our ftp using WinSCP. But how can i define a port in PowerShell using the .net assembly!
I am trying to make a solution where I download the recent files from the server, delete it on the server and then import it to a MSSQL Database.
But my issue now is connecting to the ftp using WinSCP.
It's hard to say without any code but try something like this:
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
$sessionOptions.PortNumber = 2222
$sessionOptions.HostName = "example.com"
$sessionOptions.UserName = "user"
$sessionOptions.Password = "mypassword"
WinSCP SessionOptions LINK
WinSCP Session Options default to SFTP and PORT 22 so a normal session object just needs a few things like this...
Note: I'm going to display in Visual Basic 6 for anyone having trouble using the library in this manner, but the logic is similar for VB.net, C#, and PowerShell as the question asks.
Dim sessionOptions
Set sessionOptions = CreateObject("WinSCP.SessionOptions")
With sessionOptions
.HostName = SftpUrl
.UserName = UserName
.Password = UserPassword
.SshHostKeyFingerprint = "ssh-rsa 2048 Ue9.................................="
End With
Dim sftpSession
Set sftpSession = CreateObject("WinSCP.Session")
On Error GoTo YOURERROR
sftpSession.open sessionOptions
If sftpSession.opened Then
'Do stuff...
End If
The above code is working and connecting to a real server.
In your question, you originally asked for FTP, although you did correct and say SFTP. However, I'll also display a FTP request too as WinSCP supports it and the example WOULD need to set at least the protocol.
WinSCP sets the port based on the protocol used, so in the below example we still do not need to set the port.
One would only need to set the port if it differed from default server ports for the protocol used.
Dim sessionOptions
Set sessionOptions = CreateObject("WinSCP.SessionOptions")
With sessionOptions
.Protocol = Protocol_Ftp
.HostName = FtpUrl
.UserName = UserName
.Password = UserPassword
End With
Dim FtpSession
Set FtpSession = CreateObject("WinSCP.Session")
On Error GoTo YOURERROR
FtpSession.open sessionOptions
If FtpSession.opened Then
'Do stuff...
End If
Related
My friend has a CentOS v7.3 server running on VMWare. It's an old server, so he can't upgrade CentOS (hardware). Hid programmer is still off work and he's asked me to setup his Samba to allow access to the CentOS server from a Windows 7 workstation in a domain.
He also would like to be able to connect to a Windows Workstation from the CentOS server as well.
What I know about Samba is dangerous. He admitted that they never got it to work! I need the money, so it would ne nice if I can get it working.
I copied the Samba script and change the name of Domain etc., Can anyone help please?
[global]
log file = /var/log/samba/log.%m
load printers = no
domain master = no
hosts allow = 127. 192.168.0.0/24
encrypt passwords = yes
realm = xxxxxxxx.com
passdb backend = tdbsam
netbios name = RHServer1
cups options = raw
server string = Samba Server Version %v
password server = ADServer.xxxxxxxx.com
default = netlogon
os level = 20
printcap name = /dev/null
preferred master = no
max log size = 5
domain logons = yes
; security = user
# log files split per-machine:
# enable the following line to debug:
# log level =3
# maximum size of 50KB per log file, then rotate:
; id map config * : backend -tdb
# Not interested in printers
[homes]
comment = Home Directories
browseable = yes
writable = yes
# valid users = %S
valid users = DOMAIN\%S
[Web]
comment = Web Servers
browseable = yes
writable = yes
path = /var/www/
guest ok = no
# valid users = %S
valid users = DOMAIN\%S
# Un-comment the following and create the netlogon directory for Domain Logons:
[netlogon]
comment = Network Logon Service
path = /var/lib/samba/netlogon
guest ok = no
writable = no
# Un-comment the following to provide a specific roaming profile share.
# The default is to use the user's home directory:
[Profiles]
path = /var/lib/samba/profiles
browseable = no
guest ok = yes
I'm going to be traveling for the next month, and I'd like to automate the VPN connection process so that on X event, the script fires and automatically connects me. I've already configured the [L2TP/IPSec] VPN connection in ms-settings:network-vpn & verified it works, but it's automation step that's proving problematic.
Windows GUI: The credentials have been saved.
PowerShell: The RememberCredential property is set to True
VBScript: Curiously, the VPN connection is hidden:
Dim oShell : Set oShell = CreateObject("Shell.Application")
Dim NetConn : Set NetConn = oShell.Namespace(49)
Dim Connections : Set Connections = NetConn.Items
wscript.echo "Connection Count [" & Connections.Count & "]"
For i = 0 to Connections.Count - 1
wscript.echo "Connections.Item(" & i & ").Name: [" & Connections.Item(i).Name & "]"
next
rasdial <entry>: Expectedly returns error 691.
rasphone -d <entry>: Displays the Connection dialog whereas I'd prefer it to just connect automatically and hidden.
Is this even possible in Windows 10? Or am I just overlooking some small yet key detail?
I ended up leveraging Add-VpnConnectionTriggerApplication to trigger an automatic connection of the VPN on the launch of specific executables/UWP applications. The downside is that when doing this, PoSh warns that SplitTunneling must be enabled which is less than ideal.
However after playing around with it for a while (just 2 or so hours now) to ensure the VPN keys off specific executables/UWP's, I ended up disabling SplitTunneling and, paradoxically, it appears to continue working as I would hope/expect. I rebooted a few times, logged on and sure enough by the time the desktop loaded the VPN had been established.
I need to do more testing to confirm, but this is sufficient to help save me from myself.
I do this by checking the Remember my sign-in info checkbox when I created the VPN connection.
You can check this in your PowerShell script by ensuring that Get-VpnConnection returns RememberCredential : True.
If this is the case, then rasdial should automatically connect it.
I do it with this:
<#
.SYNOPSIS
Ensures vpn connection (assumed to have saved credentials) is connected.
#>
function Connect-Vpn
{
[CmdletBinding()]
param (
[object]
$Settings
)
$rr1 = Get-VpnConnection -Verbose:$false | where {$_.ServerAddress -imatch $Settings.VpnConnectionPattern -and $_.RememberCredential} | Select -First 1
if ($rr1.ConnectionStatus -ne 'Connected')
{
rasdial.exe $rr1.Name
If (-not $LASTEXITCODE)
{
throw "Cannot connect to '$($rr1.Name)'."
}
}
else
{
Write-Verbose "Already connected to '$($rr1.Name)'."
}
}
You will have to massage this code to your needs as this uses some fields from my settings file...
I currently use the following code to connect to a database in my Perl script:
my $dsn = 'dbi:ODBC:MYDATABASE';
my $database = 'uat_env';
my $user = 'user';
my $auth = 'password';
my $dbh = DBI->connect($dsn, $user, $auth, {
RaiseError => 1,
AutoCommit => 1
}) or die("Couldn't connect to database");
$dbh->do('use '.$database);
Now the port has changed from 1433 to 40450.
I am having trouble changing the port in the DSN. I thought this change would work but I am receiving a "DSN not found" error:
my $dsn = 'dbi:ODBC:MYDATABASE;Port=40450';
Any idea why this isn't working?
There are two formats for a DBI data source string for ODBC. You can say either
dbi:ODBC:DSN=MYDATABASE
or you can abbreviate that to
dbi:ODBC:MYDATABASE
which is what you have. If you use just the DSN then you can't add any more parameters, so your dbi:ODBC:MYDATABASE;Port=40450 is looking for DSN MYDATABASE;Port=40450 which clearly doesn't exist
The proper way to do this is to set up a new DSN which has a copy of all the parameters of MYDATABASE, but with a different port name
At a guess, I would say you may be able to write
dbi:ODBC:DSN=MYDATABASE;Port=40450
but I can't be sure and I have no way of testing
If your requirements are simple then you can supply all of the parameters instead of a DSN, like this
dbi:ODBC:Driver={SQL Server};Server=11.22.33.44;Port=40450
but you will have to supply the correct driver if you aren't using a SQL Server ODBC connection, and other parameters may be necessary
You should start by examining the values in the MYDATABASE DSN and go from there
I have a problem with connection from VisualBasic (Excel) to postgresql.
I define my connection like that:
Set ObjMyConn = New ADODB.Connection
ObjMyConn.ConnectionString = "Driver={PostgreSQL Unicode};Server=ip;Port=port;Database=db_name;Uid=user;Pwd=pass;"
ObjMyConn.Open
When I want to load 443532 rows into Excel with
Set objMyCmd = New ADODB.Command
Set objMyCmd.ActiveConnection = ObjMyConn
objMyCmd.CommandText = "select * from table"
objMyCmd.CommandType = adCmdText
objMyCmd.Execute
It keeps showing me run-time error Out of memory error while reading tuples.
I have already upgraded my ODBC driver to the latest version. I read here that I have to set Use declare/catch to true somewhere (odbc driver I guess).
Is it possible to set it in VB code?
I believe you can just add the UseDeclareFetch=1option to the connection string like this:
"Driver={PostgreSQL Unicode};Server=ip;Port=port;Database=db_name;Uid=user;Pwd=pass;UseDeclareFetch=1"
But, if that doesn't work, there's another method - if you have set up a ODBC system data source with the ODBC Database Connection Administrator you can modify the registry key directly (provided you have the sufficient permissions).
The key you want to change is located under HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\PostgreSQL35Wif you're using a System DSN and the following vba code will enable Use Declare/Fetch:
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\default:StdRegProv")
strKeyPath = "SOFTWARE\ODBC\ODBC.INI\PostgreSQL35W"
strValueName = "UseDeclareFetch"
strStringValues = "1"
oReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strStringValues
Running CentOS. xinetd.d/clhtest entry is as follows:
service clhtest
{
disable = no
port = 8020
socket_type = stream
protocol = tcp
wait = no
user = charrison
passenv = PATH
server = /home/charrison/bin/clhtest
}
In debugging this I need to write to a file. I set the server process up to open /home/charrison/log/foo.txt as one of the first steps (note user=charrison), but it doesn't - and I assume it tries to. When I launch the server program from command line it opens the file successfully.
I suspect the umask parameter may be needed, but I don't know what it defaults to.
Any hints?