I am working on a fairly small script that aims to download all files from a FTP folder. The script is a powershell script and uses the PSFTP module.
So for test purposes I created 3 randoms files in the ftp folder called "a", "b" and "c".
The script works like this
Creating PSCredentials
Creating a FTP connection
Connecting to session
Downloading all files from the root ftp folder
So, all is working except the last line. To take all files i try using the wildcard * but this creates an error : Exception calling ".ctor" with 2 argument(s)
The whole script looks like this :
Import-Module PSFTP
$pass = ConvertTo-SecureString "1234" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ('...2', $pass)
Set-FTPConnection -Credentials $cred -Server ... -Session CertFTP -IgnoreCert -UseBinary -KeepAlive
$Session1 = Get-FTPConnection -Session CertFTP
Get-FTPItem -Path * -LocalPath 'C:\Certificats' -Session $Session1
I tried the same thing but with -Path "a", and this is working.
Thus, what I don't know is : Am I doing something wrong in the syntax itself, or does Get-FTPItem doesn't support using * in -Path?
I ended up using Get-FTPChildItem in a foreach and then using Get-FTPItem on each object.
Related
Good evening,
I'm trying to write a Powershell script that will connect to a remote server via SCP and upload or download files/folders. Ultimately this is the script that I would like Jenkins to run.
So far I'm using Posh-SSH and having good success. The only issue is, no matter what I have tried so far, it will always prompt me for my credentials. This, obviously, makes it not entirely automatic.
I have attached a few things I've tried. Hopefully someone can help me out with this!
The basic command I'm testing with:
get-scpfolder -computername '111.111.111.111' -credential $credential
-remotefile "/var/myFolder" -localfile 'C:\Users\Me\destFolder'
Again, this works, but it requires me to enter my credentials.
I saw this command online:
$Password = "pass"
$User = "admin"
$ComputerName = "111.111.111.111"
$Command = "get-scpfolder -computername $ComputerName -credential $Credentials -localfolder 'C:\Users\Me' -remotefolder '/var/destFolder"
$secpasswd = ConvertTo-SecureString $Password -AsPlainText -Force
$Credentials = New-Object System.Management.Automation.PSCredential($User, $secpasswd)
$SessionID = New-SSHSession -ComputerName $ComputerName -Credential $Credentials #Connect Over SSH
Invoke-SSHCommand -Index $sessionid.sessionid -Command $Command # Invoke Command Over SSH
However this returns ExitStatus 1 and nothing happens. I have tried a few variations of the $Command including the credentials or not, for example, and I can't get any of it to work.
I am trying to copy a file from my local workspace to a remote server (not a network shared path) by using the powershell command through Inline Powershell" task in TFS vNext build definition.
FYI, destination path is not a network shared path
I tried with below commands
$Session = New-PSSession -ComputerName "remote server name" -Credential "domain\username"
Copy-Item "$(Build.SourcesDirectory)\Test.htm" -Destination "C:\inetpub\wwwroot\aspnet_client\" -ToSession $Session
But it's promoting for the password every time and I tried with entering the password manually and the result looks good.
How can we achieve this step without prompting password or credentials
Are you sure it's not on a network share? :)
Powershell only takes password as a secure string. You can use $credential = Get-Credential to render a really cool box to store those credentials for you, or if you want to store your login programmatically (not recommended for obvious security reasons) use this:
$passwd = ConvertTo-SecureString "<password>" -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential("<username>",$passwd)
There might be a way to inherit your current domain credentials, but that's way beyond me, and a quick google search turns up nothing.
EDIT: Sorry I forgot to post the whole thing:
$passwd = ConvertTo-SecureString "<password>" -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential("<username>",$passwd)
$Session = New-PSSession -ComputerName "remote server name" -Credential $credential
Copy-Item "$(Build.SourcesDirectory)\Test.htm" -Destination "C:\inetpub\wwwroot\aspnet_client\" -ToSession $Session
I am having an issue that I can't resolve. I am trying to run a script locally on a server, the script exists on a remote server. It is a script that I created that makes a series of domain credentials from a file with secure strings and a key file.
The script works fine locally if the ps1 file itself is on the system where I am running it. I am unable to run the script locally if the ps1 file is on the other machine.
Here is the script that I am trying to run... (this ps1 file is on "server1")
$creds = Import-Csv "\\server1\D$\Credentials\creds.csv"
$key = Get-Content "\\server1\D$\Resources\AES.key"
foreach ($cred in $creds) {
$user = $cred.User
$password = $cred.Hash | ConvertTo-SecureString -Key $key
#$password = "" | ConvertTo-SecureString -AsPlainText -Force
$i = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $user,$password
New-Variable -Name ($cred.Domain + "_Cred") -Value $i -Force -Verbose
}
I am trying to run it from server2 using the following command...
& '\\server1\D$\Credentials\BuildCreds.ps1'
Any thoughts? Is this a scope issue, or the fact that I am loading the files with UNC? I've tried everything that I can think of.
It seems like this issue only exists in PowerShell ISE. I ran the code in PowerShell and everything worked correctly.
I am not quite sure why it is like that.
I am running into a little issue, I have generated runbooks using powershell.
One of them connects to Vmware using a build up similar to this
The code
TempFunction{
connects to vmware using VMuser1 credentials
runs code etc...
All is well
return $TempVariable
}
$TempVariable = Invoke-Command -ScriptBlock ${function:TempFunction} -ComputerName localhost
This all works fine.
The problem is that when I introduce a global array named InfoArray that stores information along the way and at the end wish to write it to a different server using other credentials it fails stating "Invoke-Command: The Using variable is not supported in the script function or filter"
Here is my introduced code:
#Append gathered log information to log
$strScriptUser = "domain\FileUser"
$strPass = "Password01"
$PSS = ConvertTo-SecureString $strPass -AsPlainText -Force
$cred = new-object system.management.automation.PSCredential $strScriptUser, $PSS
Start-Job -ScriptBlock { Add-Content $using:Logfile $using:InfoArray } -Credential $cred
return $TempVariable
}
Any ideas as to how I could solve this issue?
The following does not work
Start-Job -ScriptBlock { Add-Content $Logfile $InfoArray } -Credential $cred
Regards
Akr
Pass them as arguments to this script block, since it doesn't support using.
Sample
Start-Job -ScriptBlock {
param($logfile,$infoArray)
Add-Content $Logfile $InfoArray
} -Credential $cred -ArgumentList #($logfile,$infoArray)
I attempted to Invoke without using -computername localhost, which resulted in it working via a PS script.
The challenge was that I cannot load PowerCLi pssnapin as a 64 bit directly in system center orchestrator, and must invoke for it to work.
Finally, what seemed to work was running the function using the server name rather than localhost.
$TempVariable = Invoke-Command -ScriptBlock ${function:TempFunction} -computerName *ServerName*
I am trying to access shared drive vai remote server in my Powershell script. My code is below:
$bypass1 = "config"
$bypass2 = "web.config"
$Username = "test\newtest"
$Password = "xxxxxxxxx"
$srv = "xxx.xxx.xxx.xxx"
$securePassword = ConvertTo-SecureString -AsPlainText -Force $Password
$cred = New-Object System.Management.Automation.PSCredential $Username, $securePassword
$session = New-PSSession -ComputerName $srv -port 22 -Credential $cred
Invoke-Command -Session $session -ScriptBlock {
$computer = "xxx.xxx.xxx.xxx"
test-path \\$computer\netlog\php
Get-ChildItem \\$computer\netlog\
}
Remove-PSSession -Session $session
When I tried to access shared from Remote Desktop Connection on the server it is working but through powershell it is throwing following error.
False
Cannot find path '\\xxx.xxx.xxx.xxx\netlog\' because it does not exist.
+ CategoryInfo : ObjectNotFound: (\\xxx.xxx.xxx.xxx\netlog\:String) [Get-Ch
ildItem], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCom
mand
+ PSComputerName : xxx.xxx.xxx.xxx
I am having Powershell 4 and remote server is windows 2008 R2.
Regards,
Vj
Did you try accessing the following path \\xxx.xxx.xxx.xxx\netlog\ locally? You have the creds to access the server. And also you say the drive is shared. Try accessing the path locally. Not via power-shell. But try the old fashioned way. Using Run. If you are prompted for any credentials to be entered. You may strike off any issues related to permissions. If not... The folder you are trying to access isn't shared at all. Try giving it the required permissions. If this isn't the case please leave a comment.
You might need to enable Multihop Remoting when you access a share from a remote machine and use CredSSP.
The credentials you use to create the remote session are not passed to the share access action by default. I think everyone runs into this problem at least once. :)
http://blogs.technet.com/b/heyscriptingguy/archive/2013/04/04/enabling-multihop-remoting.aspx
http://blogs.technet.com/b/heyscriptingguy/archive/2012/11/14/enable-powershell-quot-second-hop-quot-functionality-with-credssp.aspx