How can I provide arguments to powershell.exe in order to spawn a message box? The key phrase here is arguments to powershell.exe, not from within a .ps1 script and also not from within the Powershell prompt itself. I currently have this but it is producing errors:
powershell.exe -Command "[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms"); [System.Windows.Forms.MessageBox]::Show("Test!!!")"
I have also tried without -Command and with Invoke-Expression, with and without double quotes surrounding.
Errors created:
At line:1 char:51
+ [System.Reflection.Assembly]::LoadWithPartialName(System.Windows.Form ...
+ ~
Missing ')' in method call.
At line:1 char:51
+ ... eflection.Assembly]::LoadWithPartialName(System.Windows.Forms); [Syst ...
+ ~~~~~~~~~~~~~~~~~~~~
Unexpected token 'System.Windows.Forms' in expression or statement.
At line:1 char:71
+ ... flection.Assembly]::LoadWithPartialName(System.Windows.Forms); [Syste ...
+ ~
Unexpected token ')' in expression or statement.
At line:1 char:114
+ ... stem.Windows.Forms); [System.Windows.Forms.MessageBox]::Show(Test!!!)
+ ~
Missing ')' in method call.
At line:1 char:114
+ ... stem.Windows.Forms); [System.Windows.Forms.MessageBox]::Show(Test!!!)
+ ~~~~~~~
Unexpected token 'Test!!!' in expression or statement.
At line:1 char:121
+ ... stem.Windows.Forms); [System.Windows.Forms.MessageBox]::Show(Test!!!)
+ ~
Unexpected token ')' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingEndParenthesisInMethodCall
This is a quotation problem. Using the same double quote " in both argument and its contents messes up the content. As a work-around, use single quotes within the Powershell command and double quotes around the whole -Command parameter. Like so,
powershell.exe -Command "[System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms'); [System.Windows.Forms.MessageBox]::Show('Test!!!')"
That being said, Add-Type -AssemblyName is IMAO prettier way to load assemblies. Like so,
powershell.exe -Command "add-type -assemblyname System.Windows.Forms; [System.Windows.Forms.MessageBox]::Show('Test!!!')"
Related
I'm trying to pass a variable to powershell script as parameter. Variable has some special characters and the call to ps1 script fails.
Here I just created a sample to show the problem.
PS C:\>$pass1 = '?q$*9-W$wcx)O.Ra)X-&6'
PS C:\>powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -Command 'echo '$pass1''
ERROR:
At line:1 char:26
+ echo ?q$*9s8ubhD8c-W$wcx)O.Ra9)DX-D&6
+ ~
Unexpected token ')' in expression or statement.
At line:1 char:32
+ echo ?q$*9s8ubhD8c-W$wcx)O.Ra9)DX-D&6
+ ~
Unexpected token ')' in expression or statement.
At line:1 char:37
+ echo ?q$*9s8ubhD8c-W$wcx)O.Ra9)DX-D&6
+ ~
The ampersand (&) character is not allowed. The & operator is reserved for future use; wrap an ampersand in double quotation
marks ("&") to pass it as part of a string.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
enter image description here
Your script has a syntax error, it doesn't appear to be a problem with the actual value of the parameter/variable.
This is the line that has a syntax error:
powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -Command 'echo '$pass1''
This is where your problem is:
'echo '$pass1''
You're actually passing 3 things here...
'echo '
$pass1
''
The second single quote is exiting the string.
You need to alternate between single and double strings depending on how you need to use them:
$pass1 = '?q$*9-W$wcx)O.Ra)X-&6'
powershell -Command "echo '$pass1'"
I am trying to run a PowerShell command (DesktopAppConverter) and I'm getting an error saying it is finding an unknown positional parameter.
DesktopAppConverter -AppInstallPath 'C:\Program Files (x86)\Search Deflector' -Destination '.\AppxPackage\' -Installer '.\ClassicInstaller\SearchDeflector-Installer.exe' -InstallerArguments '/COMPONENTS="main"','/VERYSILENT','/DIR="C:\Program Files (x86)\Search Deflector"' -MakeAppx -PackageName '3945spikespaz.SearchDeflector' -Publisher 'CN=69331A0A-1F10-4A10-8A28-3627A09E25FD' -Version '0.0.3.0' -AppId 'SearchDeflector' -AppDisplayName 'Search Deflector' -AppDescription 'A small program that forwards searches from Cortana to your preferred browser and search engine.' -PackagePublisherDisplayName 'spikespaz' -PackageArch 'x86' -Sign -Verbose
I also tried replacing the single quotes with double quotes and escaping the quotes in the InstallerArguments array with backticks. No dice.
C:\Program Files\WindowsApps\Microsoft.DesktopAppConverter_2.1.4.0_x64__8wekyb3d8bbwe\DesktopAppConverter.ps1 : A positional parameter cannot be found that accepts
argument '/VERYSILENT'.
At line:1 char:1
+ &'C:\Program Files\WindowsApps\Microsoft.DesktopAppConverter_2.1.4.0_ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [DesktopAppConverter.ps1], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,DesktopAppConverter.ps1
My guess is that it's splitting the parameters at the vert first space in the AppInstallPath string.
I am trying to invoke the following the command which contains the single quotation, but I am not able to execute and returns as an error:
$expression = $snapshot.properties.activities[1].typeProperties.parameters.rawinputlocation = '$$Text.Format(`'wasb://document.blob.co
re.windows.net/{0:yyyy}/{0:MM}/{0:dd}/DocumentActivity/raw/{{*}}.csv'`, SliceEnd)'
Invoke-Expression $expression
Error:
Invoke-Expression $expression
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : ParserError: (:) [Invoke-Expression], ParseException
FullyQualifiedErrorId : UnexpectedToken,Microsoft.PowerShell.Commands.InvokeExpressionCommand
This happens as the single quote, ', is escaped with a backtick, `.
The first one works, but the latter one is in the wrong order: the backtick is after the single quote. Consider the difference:
`'wasb://...csv'`
`'wasb://...csv`'
I have a batch file with lot of stuff. I there is one Alert Window with info for user.
On Windows Pro I'm using Msg command for it and it works fine.
On Windows Home there is no Msg, so I got the idea to use PowerShell instead:
[System.Windows.Forms.MessageBox]::Show("my text")
which works fine in PowerShell.
-However, when I try to use it in batch or execute it directly in Cmd, I only get the text:
C:\Windows\System32>powershell {[System.Windows.Forms.MessageBox]::Show("\""my text"\"")}
[System.Windows.Forms.MessageBox]::Show("my text")
Or I get errors:
C:\Windows\System32>powershell -command [System.Windows.Forms.MessageBox]::Show("my text")
At line:1 char:41
+ [System.Windows.Forms.MessageBox]::Show(my text)
+ ~
Missing ')' in method call.
At line:1 char:41
+ [System.Windows.Forms.MessageBox]::Show(my text)
+ ~~
Unexpected token 'my' in expression or statement.
At line:1 char:48
+ [System.Windows.Forms.MessageBox]::Show(my text)
+ ~
Unexpected token ')' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingEndParenthesisInMethodCall
or
C:\Windows\System32>powershell -command "& {[System.Windows.Forms.MessageBox]::Show('my text')}"
Unable to find type [System.Windows.Forms.MessageBox].
At line:1 char:4
+ & {[System.Windows.Forms.MessageBox]::Show('my text')}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Windows.Forms.MessageBox:TypeName) [],
RuntimeException
+ FullyQualifiedErrorId : TypeNotFound
What should I do to get it to work?
(without rewriting the whole script to PowerShell, that is)
As TheMadTechnician stated, you may need to load it first.
This is effectively the same answer as theirs just over a couple of lines:
#Echo Off
PowerShell -Command^
"[Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')|Out-Null;"^
"[System.Windows.Forms.MessageBox]::Show(\"my text\")"
Pause
…and whilst double quotes around my text is not necessary, I've used them to show you the escapes.
You need to load the type before you can invoke it. You can do this:
powershell -command "[reflection.assembly]::LoadWithPartialName('System.Windows.Forms')|out-null;[windows.forms.messagebox]::Show('my message')"
I get the below time stamp from API response how do i convert this to human readable text in power shell, i tried below but it is throwing error.
PS C:\Users\foobar\ddd> [datetime]::ParseExact('20100804T104413+0100','
yyyyMMdd'T'HHmmsszzz',[System.Globalization.CultureInfo]::InvariantCulture)
At line:1 char:57
+ [datetime]::ParseExact('20100804T104413+0100','yyyyMMdd'T'HHmmsszzz', ...
+ ~
Missing ')' in method call.
At line:1 char:57
+ ... me]::ParseExact('20100804T104413+0100','yyyyMMdd'T'HHmmsszzz',[System ...
+ ~~~~~~~~~~~~
Unexpected token 'T'HHmmsszzz'' in expression or statement.
At line:1 char:69
+ ... e]::ParseExact('20100804T104413+0100','yyyyMMdd'T'HHmmsszzz',[System. ...
+ ~
Missing argument in parameter list.
At line:1 char:122
+ ... dd'T'HHmmsszzz',[System.Globalization.CultureInfo]::InvariantCulture)
+ ~
Unexpected token ')' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingEndParenthesisInMethodCall
20170125T153341-050020170125T153344-0500
Your only problem seems to be the errant (lack of) quoting of T in your code; removing it seems to work fine:
[datetime]::ParseExact('20100804T104413+0100','yyyyMMddTHHmmsszzz', $null)
Also, since you're providing a format string in which all characters are specified individually and numerically, you needn't specify a culture (passing $null, which defaults to the current culture, will do).