Command Encoded in Powershell FAIL - powershell

I discovered that the command uncoded bad, but I do not understand what is wrong:
My Code :
$command = "(New-Object System.Net.WebClient).DownloadFile('http://localhost/update_program.exe','updater.exe'); Start-Process 'updater.exe'"
$bytes = [Text.Encoding]::Unicode.GetBytes($command)
$encodedCommand = [Convert]::ToBase64String($bytes)
echo $encodedCommand
Return
KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGU
AbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQARgBpAGwAZQAoACcAaAB0AHQAcAA6AC8ALwBsAG8AYwBhAG
wAaABvAHMAdAAvAHUAcABkAGEAdABlAF8AcAByAG8AZwByAGEAbQAuAGUAeABlACcALAAnAHUAcABkA
GEAdABlAHIALgBlAHgAZQAnACkAOwAgAFMAdABhAHIAdAAtAFAAcgBvAGMAZQBzAHMAIAAnAHUAcABk
AGEAdABlAHIALgBlAHgAZQAnAA==
I try :
powershell -encodedCommand KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQARgBpAGwAZQAoACcAaAB0AHQAcAA6AC8ALwBsAG8AYwBhAGwAaABvAHMAdAAvAHUAcABkAGEAdABlAF8AcAByAG8AZwByAGEAbQAuAGUAeABlACcALAAnAHUAcABkAGEAdABlAHIALgBlAHgAZQAnACkAOwAgAFMAdABhAHIAdAAtAFAAcgBvAGMAZQBzAHMAIAAnAHUAcABkAGEAdABlAHIALgBlAHgAZQAnAA==
Return :
Editor's note: The symptom below is inconsistent with the code above, which is correct. There may not be a problem left to solve.
Missing expression after unary operator '-'.
In line: 1 Character: 2
+ - < <<< encodeCommand KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUA
dAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQARgBpAGwAZQAoACcAaAB0AHQ
AcAA6AC8ALwBsAG8AYwBhAGwAaABvAHMAdAAvAHUAcABkAGEAdABlAF8AcAByAG8AZwByAGEAbQAuAG
UAeABlACcALAAnAHUAcABkAGEAdABlAHIALgBlAHgAZQAnACkAOwAgAFMAdABhAHIAdAAtAFAAcgBvA
GMAZQBzAHMAIAAnAHUAcABkAGEAdABlAHIALgBlAHgAZQAnAA==
+ CategoryInfo : ParserError: (-:String) [], ParentContainsErrorR
ecordException
+ FullyQualifiedErrorId : MissingExpressionAfterOperator
Because my command coded fails?

Okay, new answer as the question has changed quite drastically.
Try
#Encode Command
$command = "(New-Object System.Net.WebClient).DownloadFile('http://localhost/update_program.exe','updater.exe'); Start-Process 'updater.exe'"
$bytes = [Text.Encoding]::Unicode.GetBytes($command)
$encodedCommand = [Convert]::ToBase64String($bytes)
echo $encodedCommand
#Execute command
powershell.exe -EncodedCommand $encodedcommand
This should work so long as the statements in $command are correct.

Sorry, I think I misunderstood your question at first. I think you were trying to ask how to stop Powershell from executing the string you assign to $command. If so then try the following code
powershell -command {
$command = [string] $null; $command += '(New-Object System.Net.WebClient).DownloadFile(';
$command += "'http://localhost/update_program.exe','updater.exe') ;
Start-Process 'updater.exe'"; $bytes = [Text.Encoding]::Unicode.GetBytes($command);
$encodedCommand = [Convert]::ToBase64String($bytes); echo $encodedCommand
}
Microsoft wanted smart processing of variables to enable greater compatibility between variables and functions. I think it got confused when parsing your command. What I tried was to break up the string assignation so that Powershell didn't assume that the string was a set of instructions. The single statement above calls Powershell, assigns a null string to $command, twice concatenates with strings which Powershell cannot confuse and then outputs $encodedCommand at the end. Is this what you were after?

Related

powershell start-process doesn't accept two commands in ArgumentList because they have a same parameter

I want to launch a admin powershell with two commands :
$command1 = "Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\{example}' -Name DHCPClassId -Value National"
$command2 = "Restart-NetAdapter -Name Ethernet"
Start-Process powershell -Verb 'RunAs' -ArgumentList "-NoExit","-Command &{$command1,$command2}"
And i have an error : he don't want to execute these two because they have a same parameter (-Name), so, what can i do ?
PowerShell's statement separator is ;, not , (the latter constructs arrays).
Also:
Even though passing arguments individually, as elements of an array to Start-Process is conceptually clean, it should be avoided due to a long-standing bug: pass a single string encoding all arguments to -ArgumentList - see this answer for more information.
When using PowerShell's CLI, -Command & { ... } is never necessary - just use -Command ...; that is, there is no reason to construct a script block ({ ... }) that you must then invoke with & - all commands given are directly executed by default.
To put it all together:
$command1 = "Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\{example}' -Name DHCPClassId -Value National"
$command2 = "Restart-NetAdapter -Name Ethernet"
Start-Process powershell -Verb RunAs "-NoExit -Command $command1; $command2"

Powershell Start-Process with Splatting

I want to call a "PS App Deployment Toolkit"-package (Link) from a PowerShell-Script with arguments.
The mentioned "PS App Deployment Toolkit"-package is a powershell-script, which I want to call with parameters. (Call a .ps1 from a .ps1)
I want to use splatting for the parameters.
I want to wait for the script to end.
I want to get the exit-code from the script.
Here is my code, which is not working:
$PSADToolKitInstallWrapper = "C:\Temp\MyPackage\PS-AppDeploy.ps1"
$PSADToolKitParameters = #{
"DeploymentType" = "Uninstall";
"DeployMode" = "Interactive";
"AllowRebootPassThru" = $True;
"TerminalServerMode" = $False;
"DisableLogging" = $False;
}
$InstallStatus = Start-Process -FilePath "PowerShell.exe" -ArgumentList $PSADToolKitInstallWrapper #PSADToolKitParameters -Wait -PassThru
Write-Host "Exit-Code: $( $InstallStatus.ExitCode )"
This Line would work fine, but I want to set the Parameters like in the example above:
$InstallStatus = Start-Process -FilePath "PowerShell.exe" -ArgumentList "$PSADToolKitInstallWrapper","-DeploymentType Install -DeployMode Silent -AllowRebootPassThru -TerminalServerMode" -Wait -PassThru
Could you please assist me to get this working?
Thank you!
I don't think you need to try so hard. Why run powershell.exe from inside a PowerShell script? You're already running PowerShell. Just run the command line you want:
$PSADToolKitParameters = #{
"DeploymentType" = "Uninstall"
"DeployMode" = "Interactive"
"AllowRebootPassThru" = $True
"TerminalServerMode" = $False
"DisableLogging" = $False
}
C:\Temp\MyPackage\PS-AppDeploy.ps1 #PSADToolKitParameters
If the path and/or filename to the script you want to run contains spaces, then call it with the invocation operator (&) and quote the filename; example:
& "C:\Temp\My Package\PS-AppDeploy.ps1" #PSADToolKitParameters
Checking the results of the script depends on what the script returns. If it returns an output object, then you can simply assign it:
$output = C:\Temp\MyPackage\PS-AppDeploy.ps1 ...
If the script runs an executable that sets an exit code, you check the value of the $LASTEXITCODE variable (this is analogous to the %ERRORLEVEL% dynamic variable in cmd.exe).

Execute powershell script in SSIS with parameters not successful

I have the following Powershell script:
#Set up server and AC parameters
param([string]$DBServer, [string]$ACString1, [string]$AcString2, [string]$ACString3, [string]$ACString4, [string]$ACString5, [string]$ACString6, [string]$ACString7, [string]$ACString8, [string]$ACString9, [string]$ACString10)
$ACString1 = $ACString1 -replace "'", ""
$ACString2 = $ACString2 -replace "'", ""
$ACString3 = $ACString3 -replace "'", ""
$ACString4 = $ACString4 -replace "'", ""
#Set up new session object for accessing production
$sess = new-pssession -computer $DBserver
#Execute AC
#invoke-command -session $sess -ScriptBlock {param($scriptACString)& 'C:\Program Files (x86)\LexisNexis\InterAction\IAAppCol\INTIATBC.EXE' Our Personnel/Personnel Bus Addr} -args $ACString invoke-command -session $sess -ScriptBlock {param($a, $b, $c, $d, $e, $f, $g, $h, $i, $j)& 'C:\Program Files (x86)\LexisNexis\InterAction\IAAppCol\INTIATBC.EXE' $a $b $c $d $e $f $g $h $i $j} -ArgumentList($ACString1, $AcString2, $AcString3, $AcString4, $AcString5, $AcString6, $AcString7, $AcString8, $AcString9, $AcString10)
This is set up this way because the executable it calls will not function if a single or double quote is passed along with any parameter. So I pass each token separately since passing it as one parameter would result in single quotes around every token.
When executing from powershell command prompt it is successful:
PS C:\users\mp071663_e> ./executeac.ps1 stvsqld08 our personnel
When I try to execute this in SSIS I can't get it to execute successfully. Using execute process task. Configured as follows:
Executable: C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe
Arguments: -ExecutionPolicy ByPass -command ". 'C:\Users\mp071663_e\ExecuteAC.ps1' stvsqld08 Our Personnel"
I've also tried the following:
Arguments: -ExecutionPolicy ByPass -command ". 'C:\Users\mp071663_e\ExecuteAC.ps1' 'stvsqld08' 'Our' 'Personnel'"
Arguments: -ExecutionPolicy ByPass -command ". 'C:\Users\mp071663_e\ExecuteAC.ps1' -$DBServer 'stvsqld08' -$ACString1 'Our' -$ACString2 'Personnel'"
Arguments: -ExecutionPolicy ByPass -command ". 'C:\Users\mp071663_e\ExecuteAC.ps1' -$DBServer stvsqld08 -$ACString1 Our -$ACString2 Personnel"
Any help is greatly appreciated.
Possibly a 32/64 bit issue? SSIS packages run from Visual Studio run as a 32 bit app, does your script work in 32 bit PowerShell?
Also have you set the WorkingDirectory?

Call PowerShell script with arguments from another powershell script

How do you call a PowerShell script which takes named arguments from within a PowerShell script?
foo.ps1:
param(
[Parameter(Mandatory=$true)][String]$a='',
[Parameter(Mandatory=$true)][ValidateSet(0,1)][int]$b,
[Parameter(Mandatory=$false)][String]$c=''
)
#stuff done with params here
bar.ps1
#some processing
$ScriptPath = Split-Path $MyInvocation.InvocationName
$args = "-a 'arg1' -b 2"
$cmd = "$ScriptPath\foo.ps1"
Invoke-Expression $cmd $args
Error:
Invoke-Expression : A positional parameter cannot be found that accepts
argument '-a MSFT_VirtualDisk (ObjectId =
"{1}\\YELLOWSERVER8\root/Microsoft/Windo...).FriendlyName -b 2'
This is my latest attempt - I've tried multiple methods from googling none seem to work.
If I run foo.ps1 from the shell terminal as ./foo.ps1 -a 'arg1' -b 2 it works as expected.
After posting the question I stumbled upon the answer. For completeness here it is:
bar.ps1:
#some processing
$ScriptPath = Split-Path $MyInvocation.InvocationName
$args = #()
$args += ("-a", "arg1")
$args += ("-b", 2)
$cmd = "$ScriptPath\foo.ps1"
Invoke-Expression "$cmd $args"
Here is something that might help future readers:
foo.ps1:
param ($Arg1, $Arg2)
Make sure to place the "param" code at the top before any executable code.
bar.ps1:
& "path to foo\foo.ps1" -Arg1 "ValueA" -Arg2 "ValueB"
That's it !

Run Executable from Powershell script with parameters

So I have a powershell script that is supposed to run an executable with an argument to pass to set which method I want to run, and I need to pass a parameter, which is a directory to a config file. So this is what I have
Start-Process -FilePath "C:\Program Files\MSBuild\test.exe" -ArgumentList /genmsi/f $MySourceDirectory\src\Deployment\Installations.xml
/f is the shortname and file is the long name for my attribute... I get an error in powershell telling me that a positional parameter cannot be found for /f or /file.
Any thoughts?
Try quoting the argument list:
Start-Process -FilePath "C:\Program Files\MSBuild\test.exe" -ArgumentList "/genmsi/f $MySourceDirectory\src\Deployment\Installations.xml"
You can also provide the argument list as an array (comma separated args) but using a string is usually easier.
Here is an alternative method for doing multiple args. I use it when the arguments are too long for a one liner.
$app = 'C:\Program Files\MSBuild\test.exe'
$arg1 = '/genmsi'
$arg2 = '/f'
$arg3 = '$MySourceDirectory\src\Deployment\Installations.xml'
& $app $arg1 $arg2 $arg3
I was able to get this to work by using the Invoke-Expression cmdlet.
Invoke-Expression "& `"$scriptPath`" test -r $number -b $testNumber -f $FileVersion -a $ApplicationID"
Just adding an example that worked fine for me:
$sqldb = [string]($sqldir) + '\bin\MySQLInstanceConfig.exe'
$myarg = '-i ConnectionUsage=DSS Port=3311 ServiceName=MySQL RootPassword= ' + $rootpw
Start-Process $sqldb -ArgumentList $myarg